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.

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 rendered = Vec::new();
doc.render(&mut rendered)?;

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

Document

The root Document node.

Error

The Error type.

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.

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.