DEV Community

Cover image for Why so much Latex?
Mustafif
Mustafif

Posted on

Why so much Latex?

I've recently got myself into yet another project, and again it's based upon producing Latex files. This project aptly named tex-rs is a Rust library that can be used to create Latex documents using Rust.

In all honesty, the answer to the post is simply I'm not that creative and I've been thinking of a back end to texcreate for quite a while now. So how does it work?

The idea of this library is attaching the elements, so relating to my last post on Latex Basics Part 2, the hierarchy of content is:

  • Part
  • Chapter
  • Section
  • Paragraph
  • Text

So the rule in this library is you can attach things to each other as long as they have less priority (Confusingly I have 0 as top priority), so how does this look like:

use std::path::Path;
use tex_rs::*;
fn main() {
    let mut latex = Latex::new();
    latex.set_class(Class::Article);
    latex.set_metadata(Metadata::new("A title", "An author", "What day is it?"));
    latex.add_package("dramatist".to_string());
    latex.add_package("listings".to_string());

    let mut part = Part::new("Part 1");
    let mut chapter = Chapter::new("Chapter 1");
    let mut section = Section::new("Section 1");
    let mut text = Text::new("Some text", TextType::Underline);

    section.attach(Element::from(text)).unwrap();
    chapter.attach(Element::from(section)).unwrap();
    part.attach(Element::from(chapter)).unwrap();

    latex.set_elements(&vec![Element::from(part)]);
    latex.write(Path::new("test.tex").to_path_buf()).unwrap();
}
Enter fullscreen mode Exit fullscreen mode

Note: 0.1.0 is a very early build and future builds will break this code, so I do recommend waiting until 0.2.0 has been released.

As you can see when you add part in latex.set_elements(), it brings everything that's inside it along as well.

I hope to do a lot more in this project like bringing elements and user-defined commands, so if you're interested in contributing contact me at mustafif0929@gmail.com or feel free to push pull requests to the repo

Oldest comments (0)