# Why am I getting a “$inserted” error message? I'm using some almost trivially simple LaTeX code, running it through xelatex: \documentclass[border=2pt]{standalone} \begin{document} $B'(1)_n = B'(0)_{n+1}$ \end{document}  However, this generates the odd error: ! Missing$ inserted.
<inserted text>
$l.4 B'(1)_ n = B'(0)_{n+1} ?  There are no opening $ in my source that need closing, and the log seems to break the code at what should be perfectly normal subscript notation.

If I try to force math mode by using \ensuremath or I replace the $ and $ with explicit \begin{equasion} and \end{equasion} I still get this error, so I'm pretty sure I'm missing something super obvious but a decent amount of googling doesn't yield any information I can use to make this error disappear.

The standalone class, without the varwidth or preview option, just typesets the contents of the document environment in a single horizontal box.

If you try

\mbox{$a_1$}


you incur in exactly the same error, because $ issues  which, in restricted horizontal mode, just typesets an empty math formula and so _ is found outside of math mode. If you want to use standalone to get a printout of a single math formula in display style do \documentclass[border=2pt]{standalone} \begin{document} \displaystyle B'(1)_n = B'(0)_{n+1}  \end{document}  Note that the preview option will consider the formula to occupy a whole line. Compare the result with the suggested method and the output of \documentclass[border=2pt,preview]{standalone} \begin{document} \[ B'(1)_n = B'(0)_{n+1}$

\end{document}


Use the preview option. For displayed math one should use the preview package. It makes no sense to crop a displayed math expression. This is the reason why standalone doesn't support display math environments with the default crop option

\documentclass[border=2pt,preview]{standalone}

• Can you explain why that works? I'm using the standalone document type purely because examples on the web for "fitting the document to the content" say to use that package, without any mention of a preview option, and no mention anywhere in those tutorials and guides that standalone breaks math mode. – Mike 'Pomax' Kamermans Feb 26 '17 at 21:34
• @Mike'Pomax'Kamermans - If you don't want to use the preview option, you shouldn't use a display-math environment such as$...$. Instead, write $\displaystyle B'(1)_n = B'(0)_{n+1}$ between \begin{document} and \end{document}. – Mico Feb 26 '17 at 21:36
• it's not about "wanting to use the preview option", I literally have no idea what that option actually does, or why it fixes this particular problem. Given that tutorials that say to use standalone don't mention it, it would be quite helpful to me -and presumably others who find this question in the future- if the answer contained a bit more details in terms of why this works, rather than just "that" it works. – Mike 'Pomax' Kamermans Feb 26 '17 at 21:38
• For displayed math one should use the preview package. It makes no sense to crop a displayed math expression. This is the reason why standalone doesn't support display math environments wit the default crop option. – user2478 Feb 26 '17 at 21:49
• That's good to know. Could I request you edit that into the answer? – Mike 'Pomax' Kamermans Feb 26 '17 at 21:50

While not a solution in general, and the accepted answer is very much the "right" way to do things, in this particular case another viable option is to split the task up into two steps:

\documentclass{minimal}
\begin{document}
$B'(1)_n = B'(0)_{n+1}$
\end{document}


With the first step being to run this through xelatex, and the second step being to run the resulting PDF file through pdfcrop, which effects the same content cropping that the standalone package is typically recommended for, and used for in this case.

Also note that this is only a viable option because there is no "real text" surrounding the maths, making the minimal document class a better fit than the usual article. As part of a batch compilation process that runs on top of purely single blocks of mathematical expressions for embedding in larger content (specifically, LaTeX maths blocks presented as SVG images on a webpage) the lack of any kind of bootstrapping for typesetting text is actually desirable. However, for any variation on this theme involving real text prior to or following the math block, Why should the minimal class be avoided? should be taken into account and minimal should not be used.

• Better the article class with \pagestyle{empty}. – egreg Feb 26 '17 at 23:03
• Why better? Are there any specific macros from article that make the process work in a way that would break using minimal? (I mostly went with minimal here since sets \pagestyle{empty} already) – Mike 'Pomax' Kamermans Feb 27 '17 at 0:09
• – egreg Feb 27 '17 at 9:15
• Ah. Yes those objections make a lot of sense in the general case, but don't particularly apply to this specific one. The files in this case are individual blocks of maths for locale-based embedding in an HTML article (I'm giving up MathJax in favour of xelatex + pdf2svg), so the lack of anything to accommodate the normally surrounding "real text" is actually a benefit here. I did have to bootstrap the page to 12"x12" to make sure long blocks don't get clipped by the default page size, but there is no "too big" due to pdfcrop removing unused whitespace in step 2. – Mike 'Pomax' Kamermans Feb 27 '17 at 15:42