[][src]Crate latex

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, Align could easily be implemented with Element::Environment, where each equation is written in as-is and appended to the list of lines. However by pulling it into its own type you gain the ability to do equation-specific manipulations and have nice abstractions like an Equation's label() method.

Examples

Creating A Document

Here's how to create a reasonably complex document containing a title page, a table of contents, some equations, and two sections.

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

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...")
         .push(Align::from("y &= mx + c"));

doc.push(section_2);

let rendered = latex::print(&doc)?;

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());

Traversing A Document

Once you have created a document, you have the ability to walk it and do any transformation you want using the Visitor trait. All methods on the trait come with default impls which will recursively visit the various nodes in your Document. This means if you only care about the Paragraph nodes you can implement just the visit_paragraph() method and then inspect all Paragraph nodes in the document. Everything else should Just Work.

If you want to see how you can write your own Visitor, check out the source code for the Printer struct.

Structs

Align

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

Document

The root Document node.

Equation

A single equation.

Item

Wrapper around a single list item.

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, representing each type of possible node.

ListKind

Which kind of list should be used?

ParagraphElement

The various paragraph elements.

PreambleElement

An element of the document's preamble.

Traits

Visitor

A trait which uses the Visitor Pattern to recursively visit each node in a Document.

Functions

print

Print a document to a string.