5

I want to generate an image from a Tex file. I use a template and then programmatically insert LaTeX inside it. For example, if I want to create an image for x^2 + \phi, this would be the resulting template file:

\documentclass[12pt]{minimal}
\usepackage[english]{babel}
\usepackage[intlimits]{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{wasysym}
\pagestyle{empty}
\parindent=0pt
\setbox0=\hbox{
x^2 + \phi
}
\textheight=\ht0
\textwidth=\wd0
\oddsidemargin=-1cm
\topmargin=-1.5cm
\advance\textheight by 1cm
\advance\textwidth by 1cm
\begin{document}
\vbox{\vss\hbox{\hss\copy0\hss}\vss}
\end{document}

I don't really know LaTeX, because I've only used it on Math.SE for equations. I've taken this template somewhere and if it can be made better I'm open for suggestions.

However, the problem is that this does not compile! Running latex source.tex produces

Missing character: There is no x in font nullfont!

! Missing $ inserted.
<inserted text> 
                $
l.10 x^
       2 + \phi

My understanding is that some packages are setting the nullfont mode, so I cannot input the equation correctly.

rubik
  • 771
  • 3
    Never use the minimal class and never set boxes before \begin{document}. The reason for the error message is that ^ is legal only in math mode. – egreg Dec 08 '14 at 09:42
  • @egreg Why not use the minimal class? I find it quite handy, especially for TikZ pictures. – Benedikt Bauer Dec 08 '14 at 09:59
  • 5
    @BenediktBauer The minimal class was not designed to be used for anything: it's there as it does the minimum necessary to allow LaTeX to process a \begin{document} line. Various core/expected definitions are not made by minimal as a result. It was set up for testing in the early 1990s in a very different computing situation. – Joseph Wright Dec 08 '14 at 10:26

2 Answers2

5

I don't understand the need for the \setbox, \vbox, and \hbox instructions. If all you want to do is to create an image file containing a certain math expression, in a font size of 12pt, you could achieve this objective by writing

\documentclass[12pt,preview,border=1pt]{standalone}
\begin{document}
$ x^2 + \phi $
\end{document}

More elaborate contents than $ x^2 + \phi $ can be created nearly as easily. E.g., to render the preceding content in displaymath mode and an equation number, you'd write

\documentclass[12pt,preview,border=1pt]{standalone}
\begin{document}
\begin{equation}
x^2 + \phi
\end{equation}
\end{document}
Mico
  • 506,678
1

use

\setbox0=\hbox{%
  $x^2 + \phi$%
}

or:

\documentclass[12pt,preview,border=1pt]{standalone}
\begin{document}

\[ x^2 + \phi \]

\end{document}
  • Could you please a little more detail, like, why this works, and what was the original problem. That will make it look like an answer, and not a comment. – Masroor Dec 08 '14 at 10:26