Skip to content

TeXForm

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

TeXForm[expr]

prints as a TeX version of expr.

Notes TeXForm produces AMS-LaTeX-compatible TeX output. When an input evaluates to TeXForm\[expr\], TeXForm does not appear in the output. TeXForm translates standard mathematical functions and operations. Following standard mathematical conventions, single-character symbol names are given in italic font, while multiple character names are given in roman font.

Examples (6)

Every input below was run against the current Mathilda build and its output recorded.

Applications (6)

In[1]:= TeXForm[x^2 + 1]
Out[1]= 1+x^{2}

In[2]:= TeXForm[1/2]
Out[2]= \frac{1}{2}

In[3]:= TeXForm[Sqrt[x]/y]
Out[3]= \frac{\sqrt{x}}{y}

In[4]:= TeXForm[Integrate[1/(1 + x^2), x]]
Out[4]= \tan ^{-1}(x)

In[5]:= TeXForm[Gamma[1/2]]
Out[5]= \sqrt{\pi }

In[6]:= TeXForm[D[Tan[x], x]]
Out[6]= \sec(x)^{2}

Implementation notes

Algorithm. TeXForm[expr] is a held wrapper: builtin_texform returns NULL, and the actual rendering is triggered by print_standard in src/print.c, which on seeing the TeXForm head dispatches to the recursive print_tex renderer. print_tex emits AMS-LaTeX, reusing the same precedence levels as get_expr_prec so parentheses are inserted consistently with the ordinary printer. Numbers go through print_tex_number (integers/bigints verbatim, reals via %g with a trailing dot, MPFR at precision-derived digit counts). Symbols go through print_tex_symbol, which maps named constants (Pi -> \pi, Infinity -> \infty, EulerGamma -> \gamma, Degree -> {}^{\circ}, booleans to \text{...}, etc.), emits single-character names bare (italicised by math mode) and wraps multi-character names in \text{...}. Trig and hyperbolic heads are mapped to \sin, \cosh, … (with inverse variants rendered as \sin^{-1} style) via the tex_trig_lookup table; structural forms (Power, Times, fractions, Sqrt, lists/matrices, SeriesData) have dedicated emitters. The output is printed directly to stdout rather than returned as a string expression.

Attributes: Protected.

References

Notes & additional examples

Notes

TeXForm renders an expression as a TeX/LaTeX string, mapping fractions to \frac, roots to \sqrt, and superscripts to ^{...}. Paste the output directly into a math environment.