Print Julia objects in a form suitable for LaTeX mathematics mode.



Print Julia objects in LaTeX form.


Use Pkg.clone("https://github.com/scheinerman/LatexPrint.jl") (one time) to install and then using LatexPrint whenever you want to use these functions.

Key functions

This module provides functions for converting Julia objects into string representations for use in LaTeX mathematics mode. The primary function is laprintln which behaves precisely like println except Julia objects are first converted to a form suitable for LaTeX. Because laprintln is a lot to type, we also provide the abbreviation lap.

julia> using LatexPrint

julia> x = 2//6

julia> lap(x)

We also provide the function laprint which does not append a new line (just like print).

These functions rely on latex_form which converts a Julia object into an ASCIIString representation in its LaTeX form:

julia> latex_form(x)

The double backslash in the output of latex_form is converted to a single backslash when run through a print function.

Supported Types


Integers and floating point numbers

FloatingPoint and Integer numbers are printed unchanged.

julia> lap(sqrt(2))

julia> lap(23)

However, infinite and invalid values are printed as follows:

julia> lap(1/0)

julia> lap(-1/0)

julia> lap(0/0)

Julia's MathConst numbers are printed using their expected LaTeX form:

julia> lap(pi)

Rational numbers

Rational numbers are printed as fractions (unless the denominator happens to be 1, in which case we print as an integer).

julia> lap(10//4)

julia> lap(10//2)

Complex numbers

Complex numbers always include a real and an imaginary part, even if either part equals zero:

julia> z = 1+im
1 + 1im

julia> lap(z)

julia> lap(z*z)

julia> lap(im^im)

Boolean values

The Bool values true and false output like this:

julia> lap(true)

julia> lap(false)


The LaTeX version of an ASCIIString is wrapped in the command \text (which requires the amsmath package in LaTeX). The rationale is that we always want to able to paste the output of lap directly into mathematics mode in LaTeX.

julia> lap("Hello, world!")
\text{Hello, world!}


Vectors (one-dimensional arrays) and matrices (two-dimensional arrays) are converted into LaTeX array environments bounded by square brackets with centering alignment. (These default options can be changed; see "Customizing existing types" later in this document.)

julia> x = [1,2,3]
3-element Array{Int64,1}:

julia> lap(x)
1 \\
2 \\
3 \\

julia> A = eye(3)
3x3 Array{Float64,2}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

julia> lap(A)
1.0 & 0.0 & 0.0 \\
0.0 & 1.0 & 0.0 \\
0.0 & 0.0 & 1.0 \\

Vectors are, by default, rendered as a column. To typeset a vector as a row, simply take its transpose:

julia> x = [2//3, 4//3, 6//3]
3-element Array{Rational{Int64},1}:

julia> lap(x')
\frac{2}{3} & \frac{4}{3} & 2 \\


Julia Set and IntSet objects are rendered as a comma separated list between curly braces. The elements are sorted into ascending order (if possible). An empty set is returned as \emptyset (unless another form is specified using set_empty).

julia> A = Set({3.5, 2, -5})

julia> lap(A)

julia> B = IntSet(4,5,1)
IntSet([1, 4, 5])

julia> lap(B)

julia> C = Set()

julia> lap(C)

The tabular function

If A is a matrix (two-dimensional array), then laprintln(A) (or lap(A)) prints the LaTeX code for that matrix (complete with bounding delimeters) for inclusion in LaTeX's mathematics mode.

As an alternative, we also provide the function tabular that prints the array for inclusion in LaTeX's text mode in the tabular environment.

julia> A = Array(Any,(2,2));

julia> A[1,1] = 1; A[1,2] = 3+im; A[2,1]=5//2; A[2,2] = 1/0;

julia> tabular(A)
$1$ & $3+1i$\\
$\frac{5}{2}$ & $\infty$

Notice that each entry is encased in dollar signs.

By default, each column is center aligned. This can be modified in two ways. See the set_align function described below or by calling tabular with an optional second argument like this:

julia> tabular(A,"l|r")
$1$ & $3+1i$\\
$\frac{5}{2}$ & $\infty$


Customizing existing types

The LatexPrint module comes with default LaTeX representations for infinity, not-a-number, and so forth. Some of these can be modified by the following functions.

  • set_inf is used to set the representation of infinity. The default creates the output \infty but here's how it can be changed.

    julia> lap(1/0)
    julia> set_inf("\\text{inf}")  # note the double backslash
    julia> lap(1/0)
  • set_nan is used to set the LaTeX for not-a-number:

    julia> lap(0/0)
      julia> set_nan("\text{nan}")
      julia> lap(0/0)
  • set_bool is used to set the LaTeX form of true and false. By default, these output \textrm{T} and \textrm{F}. This is how these can be changed:

    julia> lap(true)

julia> set_bool("\textsf{true}", "\textsf{false}") ("\textsf{true}","\textsf{false}")

julia> lap(true) \textsf{true}

* `set_im` changes the symbol used for the imaginary unit. Some folks
  like *j* instead of *i*:

julia> z = 3+2im 3 + 2im

julia> lap(z) 3+2i

julia> set_im("j") "j"

julia> lap(z) 3+2j

* `set_emptyset` is used to specify how an empty set should be

julia> C = Set() Set{Any}({})

julia> lap(C) \emptyset

julia> set_emptyset("\{ \}") "\{ \}"

julia> lap(C) { }

  Users might like to try `\varnothing` as a nice alternative to
  `\empytset`. In that case, the Julia command would be

* `set_align` is used to specify the alignment character for
  arrays. By default elements of columns are aligned to their
  center. Use one of `l`, `r`, or `c` as the alignment character.

julia> lap(A) \left[ \begin{array}{c} 2 \ 10 \ -544 \ \end{array} \right]

julia> set_align("r")

julia> lap(A) \left[ \begin{array}{r} 2 \ 10 \ -544 \ \end{array} \right]

* `set_delims` is used to specify the left and right delimiters used
  for vectors and matrices. 

julia> A = int(eye(2)) 2x2 Array{Int64,2}: 1 0 0 1

julia> lap(A) \left[ \begin{array}{cc} 1 & 0 \ 0 & 1 \ \end{array} \right]

julia> set_delims("(", ")") # set delimiters to be open/close parens ("(",")")

julia> lap(A) \left( \begin{array}{cc} 1 & 0 \ 0 & 1 \ \end{array} \right)

### Adding new types

There are other Julia types (such as `UnitRange`) for which
we have not implemented a conversion to LaTeX. In this
case `lap` (and our other functions) simply convert the type
to an `ASCIIString`.

julia> lap(1:10) 1:10

If we want to create a LaTeX representation, then we
need to define a suitable version of `latex_form` like this:

julia> import LatexPrint.latex_form

julia> latex_form(x::UnitRange) = "[" * string(x.start) * "," * string(x.stop) * "]" latex_form (generic function with xxx methods)

julia> lap(1:10) [1,10]

``` html

First Commit


Last Touched

about 2 months ago


19 commits

Used By: