Melt: L A T EX with OCaml Romain Bardou GT ProVal June 11, 2010 - - PowerPoint PPT Presentation

melt l a t ex with ocaml
SMART_READER_LITE
LIVE PREVIEW

Melt: L A T EX with OCaml Romain Bardou GT ProVal June 11, 2010 - - PowerPoint PPT Presentation

Melt: L A T EX with OCaml Romain Bardou GT ProVal June 11, 2010 L A T EX versus OCaml L A T EX: OCaml: Beautiful documents Great programming language Lots of macros Lots of packages Motivations for Document Programming


slide-1
SLIDE 1

Melt: L

AT

EX with OCaml

Romain Bardou GT ProVal June 11, 2010

slide-2
SLIDE 2

L

AT

EX versus OCaml

L

AT

EX:

◮ Beautiful documents ◮ Lots of macros ◮ Lots of packages

OCaml:

◮ Great programming

language

slide-3
SLIDE 3

Motivations for Document Programming

Macros are good practise \newcommand{\ty}{\tau} \newcommand{\subst}[3]{#1[#2/#3]} Document-specific environments L

AT

EX libraries Compute results in the paper itself Science-fiction (or is it?):

◮ Type your theorems ◮ Check your proofs

slide-4
SLIDE 4

L

AT

EX as a Programming Language

\long\def\@makecaption#1#2{ \vskip \abovecaptionskip \setbox\@tempboxa \hbox{{\sf\footnotesize \textbf{#1.} #2}} \ifdim \wd\@tempboxa >\hsize {\sf\footnotesize \textbf{#1.} #2\par} \else \hbox to\hsize{\hfil\box\@tempboxa\hfil} \fi}

slide-5
SLIDE 5

OCaml as a Programming Language

Great:

◮ Typed ◮ Clear semantics ◮ Expressive (higher-order iterators, algebraic types...) ◮ Readable errors ◮ Nice syntax ◮ You already use it

But:

◮ Does not produce documents

slide-6
SLIDE 6

Melt

An attempt to combine

◮ the beauty of L AT

EX type-setting

◮ the expressivity of OCaml

slide-7
SLIDE 7

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-8
SLIDE 8

Hello, World!

hello.mlt: emit (document "Hello, world!") Compile: melt -pdf hello.mlt Obtain hello.pdf: Hello, world!

slide-9
SLIDE 9

Intermediate Files

After Melt pre-processor, hello.ml:

  • pen Latex;;
  • pen Melt;;

# 1 "../vide.mlt" emit (document (mode T ((text "tata")))) After compiling and running, hello.tex: \documentclass{article} \begin{document} Hello, world! \end{document}

slide-10
SLIDE 10

Text, Math and Code Modes

Text mode: "..." "Hello, world!" Math mode: $...$ $3.141592$ Code mode (default): {...} let x = "some macro" in "Some text with {x}"

slide-11
SLIDE 11

Arbitrary Nesting

"I know that $1+2={latex_of_int (1+2)}$" Produces: I know that 1 + 2 = 3

slide-12
SLIDE 12

Example: Recoding Enumerate

let enumerate items = let print_item i item = "{textbf "{latex_of_int i})"} {item}\\" in concat (list_mapi print_item items) ... enumerate ["first"; "second"; "third"] Result: 0) first 1) second 2) third

slide-13
SLIDE 13

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-14
SLIDE 14

The Melt Pre-Processor

Provides easy concatenation of text, math and code (optional) Adds open Latex;; open Melt;; MeltPP toto.mlt toto.ml

slide-15
SLIDE 15

The Latex Library

Provides bindings for:

◮ Many environments

◮ document, array, itemize, figure, center...

◮ Text type-setting commands

◮ section, tableofcontents, texttt, tiny, large...

◮ Mathematical symbols ◮ Γρǫǫκ letters, hebrew ℵג and

accents

◮ Beamer ◮ L AT

EX labels and references

◮ Low-level stuff (hfill, vspace, ...)

and more.

slide-16
SLIDE 16

The Melt Tool

Calls the pre-processor Compiles, links and executes the OCaml program Runs latex or pdflatex and bibtex Melt Tool toto.mlt toto.pdf All intermediate files in _melt directory

slide-17
SLIDE 17

The Melt Library

Easy integration of Mlpost figures E a s y i n t e g r a t i

  • n
  • f

M l p

  • s

t fi g u r e s Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures Easy integration of Mlpost figures E a s y i n t e g r a t i

  • n
  • f

M l p

  • s

t fi g u r e s Easy integration of Mlpost figures ...and some dirty stuff for the Melt tool

slide-18
SLIDE 18

Another Mlpost Diagram

meltpp toto.ml

  • camlbuild
  • r
  • camlc
  • r

mlpost toto.byte run toto.tex pdflatex Latex lib. Melt lib. toto.mlt toto.ml toto.pdf

slide-19
SLIDE 19

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-20
SLIDE 20

Mlpost Integration

val picture_of_latex: Latex.t → Mlpost.Picture.t val mlpost: Mlpost.Command.t → Latex.t Write your figures in your document: let fancy_text_rotation text = let pic = picture_of_latex text in ... let () = emit (document " Here is a figure: {mlpost (fancy_text_rotation "Text to rotate")} ")

slide-21
SLIDE 21

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-22
SLIDE 22

Basic Verbatim

Allows to print any symbol. "My webpage: <<http://www.lri.fr/~bardou>>" Generated L

AT

EX: My webpage: http\symbol{58}\symbol{47}\symbol{47} www\symbol{46}lri\symbol{46}fr\symbol{47} \symbol{126}bardou Produces: My webpage: http://www.lri.fr/˜bardou Much safer than \verb or \begin{verbatim}.

slide-23
SLIDE 23

Pretty-Printed Verbatim

let url (x: string) = texttt (Verbatim.verbatim x) in "My webpage: <:url:<http://www.lri.fr/~bardou>>" Produces: My webpage: http://www.lri.fr/~bardou In these slides:

◮ a L AT

EX pretty-printer

◮ an OCaml pretty-printer ◮ a Melt pretty-printer

slide-24
SLIDE 24

Using Verbatim to Ease Writing

A pretty-printer for boolean formulas: let bool = Verbatim.pseudocode ~symbols: [ \"/\\\\\", land_; \"\\\\/\", lor_; \"<=>\", iff; \"==>\", rightarrow_; \"<==\", leftarrow_; ] ~keyword_symbols: [\"xor\", oplus; \"xand\", otimes]

slide-25
SLIDE 25

Using Verbatim to Ease Writing

Let’s use our boolean formula pretty-printer:

"<:bool:%A /\ B \/ (C_1 xor C_2) <=> (D ==> E_1 xand E_2)%>"

Produces: A ∧ B ∨ (C1 ⊕ C2) ⇐ ⇒ (D ⇒ E1 ⊗ E2)

slide-26
SLIDE 26

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-27
SLIDE 27

Motivations for Variables

Collect data following document flow Use final value before the end Examples:

◮ theorem counters ◮ line numbers in code listings ◮ titles for a table of contents ◮ packages used by commands

slide-28
SLIDE 28

Variables: Interface

type α variable val variable: α → α variable val set: α variable → α → t val get: α variable → (α → t) → t val final: α variable → (α → t) → t

slide-29
SLIDE 29

Variables: Example

let sections = variable [] let section title = concat [ Latex.section title; get sections (fun s → set sections (title :: s)); ] let enumerate_sections = final sections enumerate

slide-30
SLIDE 30

Variables: Implementation

Compute a fixpoint on a heterogeneous list of variables = ⇒ a bit tricky

slide-31
SLIDE 31

Basic Documents The Melt Distribution Mlpost Integration Verbatim Modes Variables Conclusion

slide-32
SLIDE 32

Is it usable in practice?

Yes:

◮ all of my slides ◮ all of my research notes ◮ this very presentation ◮ the Melt documentation ◮ several full papers ◮ several PhD theses

are all written or being written with Melt.

slide-33
SLIDE 33

Will it suit your needs?

You won’t be stuck with Melt

◮ you can mix L AT

EX and Melt parts

◮ produced .tex files are readable unless lots of verbatim

Several possible programming styles Based on L

AT

EX

◮ use the styles and classes given by your publisher

slide-34
SLIDE 34

Try it now!

Webpage: http://melt.forge.ocamlcore.org/ Darcs repository: darcs get http://darcs.ocamlcore.org/repos/melt Mailing-list:

https://lists.forge.ocamlcore.org/cgi-bin/listinfo/melt-general

Melt Melt M e l t M e l t Melt Melt Melt Melt Melt Melt Melt M e l t M e l t Melt Melt