Crate latex [] [src]

A crate for generating LaTeX documents programatically.

The main purpose of this library is to make the job of programatically generating LaTeX reports and documents (which will probably then be compiled to PDF) as easy as possible.

This library tries to use Rust's powerful type system to give your document additional semantic meaning and compile-time typesafety. For example, [Element::ClearPage] could easily be implemented using Element::UserDefined(r"\clearpage"), however it is common enough to justify its own variant to make the generating code easier to read.

Examples

Here's how to create a basic document containing a title page, a table of contents, and two sections.

use latex::{DocumentClass, Element, Document, Section, Renderable};

let mut doc = Document::new(DocumentClass::Article);

// Set some metadata for the document
doc.preamble.title("My Fancy Document");
doc.preamble.author("Michael-F-Bryan");

doc.push(Element::TitlePage)
    .push(Element::ClearPage)
    .push(Element::TableOfContents)
    .push(Element::ClearPage);

let mut section_1 = Section::new("Section 1");
section_1.push("Here is some text which will be put in paragraph 1.")
         .push("And here is some more text for paragraph 2.");
doc.push(section_1);

let mut section_2 = Section::new("Section 2");
section_2.push("More text...");
doc.push(section_2);

let mut buffer = Vec::new();
doc.render(&mut buffer)?;

let rendered = String::from_utf8(buffer)?;

This will generate the LaTeX source for you, so all you need to do now is write it to a file and then run your favourite tex build tool on it (I personally use latexmk).

use std::fs::File;
use std::io::Write;
use std::process::Command;

// Write our rendered text to a file
let mut f = File::open("report.tex")?;
write!(f, "{}", rendered)?;

// Then call latexmk on it
let exit_status = Command::new("latexmk").arg("report.tex").status()?;

assert!(exit_status.success());

Structs

Align

A list of equations to be used in an align environment.

Document

The root Document node.

Equation

A single equation.

Error

The Error type.

List

A list (either dot points or numbered).

Paragraph

A single paragraph.

Preamble

A node representing the document's preamble.

Section

A document Section.

Enums

DocumentClass

The kind of Document being generated.

Element

The major elements in a Document.

ErrorKind

The kind of an error.

ListKind

Which kind of list should be used?

ParagraphElement

The various paragraph elements.

Traits

Renderable

A generic trait for rendering AST nodes to some Writer.

ResultExt

Additional methods for Result, for easy interaction with this crate.

Type Definitions

Result

Convenient wrapper around std::Result.