# Standalone producing cropped / truncated formulae

I'm using standalone to produce, well, standalone formulae. However, sometimes they end up being cropped too close and parts get left off. I'm including a sample .tex file and output. What can I do to ensure that standalone makes the image as small as possible, but no smaller?

\documentclass[article]{standalone}
\usepackage{standalone}
\begin{document}
$2^5 = x_5 * y^8$
\end{document}


The border around the image is not present in the PDF, it's from my screen capture. I left it to show that the text bleeds over the edge.

-
Hi mrjf, welcome to TeX.sx! I included the image for you, soon you'll have enough reputation for that yourself. Also, we don't usually put a greeting or a "thank you" in our posts, so I removed that. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Have fun on the site! –  Jake Apr 25 '12 at 6:06
Explanation: To assure that the characters are "visually" aligned, especially those characters which "protude" slightly from the usual rectangular bounding box (like the "spikes" of the 5 or the arch of the "8"), the characters actually exceed their own bounding box, which can't be measured by the standalone package. btw, I'm getting a sufficient border by default, but maybe I have a different package version. –  Stephan Lehmke Apr 25 '12 at 6:13
Thank you Jake, I appreciate the image help and style pointers. Thanks for the explanation Stephan, I figured it was the subs-and-supers that were hard to measure. –  mrjf Apr 25 '12 at 6:19
Note that its class=article not article, but this class is the default anyway. –  Martin Scharrer Apr 25 '12 at 6:25
@StephanLehmke: Yes, the default border setting changed from v0.x to v1.0. It's a pain, I know, but I wanted to have suitable default values for the main use case of standalone. The default can be changed back in the global or a local standalone.cfg file. –  Martin Scharrer Apr 25 '12 at 6:30

You can use the border=<len> class option to increase the width of the border around the cropped output:

\documentclass[border=1pt]{standalone}% http://ctan.org/pkg/standalone
\begin{document}
$2^5 = x_5 \times y^8$
\end{document}


As in your case, the border around the image is not present in the PDF, it's from my screen capture.

It is also possible to modify this border for left/right and top/bottom border using border={<l/r> <t/b>} or for each border specifically using border={<l> <r> <b> <t>}. These options are described in the standalone documentation (section 2.2 Class options, p 6).

Incorporating Stephan Lehmke's comment, this is what the bounding boxes around the objects in the equations look like:

\documentclass[border=1pt]{standalone}% http://ctan.org/pkg/standalone
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\setlength{\fboxsep}{-\fboxrule}% Remove \fbox separation for tight bounding box
\newcommand{\bbox}[1]{%
\color{red!50}\rlap{\fbox{$\phantom{#1}$}}%
\color{black}#1%
}
\begin{document}
$\bbox{2^5} = \bbox{x_5} \times \bbox{y^8}$
\end{document}


By setting \fboxsep to -\fboxrule, the outer edge of the boxes represent the bounding boxes exactly. It is now noticeable that some elements protrude outside this, causing the problems when clipping.

-
Ahh that is doing the trick nicely. Thanks so much Werner, I really appreciate it. I had searched the docs for margin and padding, but not border. I guess I tend to think of a border as being visible. Very happy to have this working, thanks! –  mrjf Apr 25 '12 at 6:18
@mrjf: You need to have the border because standalone crops to the official size of the content, which is sometimes smaller than the characters. E.g. italic characters fall out to the right slightly so they can overlap with following characters etc. You can't detect that on the class level. Note that v1.0 of standalone changed the border to 0 and uses the crop option, while v0.x used the preview option which had a border of 0.50001bp by default. –  Martin Scharrer Apr 25 '12 at 6:27
@MartinScharrer: Maybe you could include padding or margin as synonyms for the border option? I had the same problem a while back, because I too think of a border as something thin and visible. –  Jake Apr 25 '12 at 6:33
@MartinScharrer I would make a slight border the default. I guess there are less people who want an "absolutely tight" clipping than vice versa. –  Stephan Lehmke Apr 25 '12 at 6:36
@Jake: I can do that. I originally used the same name as preview does. –  Martin Scharrer Apr 25 '12 at 6:50

Maybe this is not an answer to your question. In addition to be honest, I do not understand the philosophy of existence of standalone when pdfcrop exits. This is what I would do instead:

Create test.tex as follow:

  \documentclass{article}
\thispagestyle{empty}
\begin{document}
$2^5 = x_5 * y^8$
\end{document}


run pdflatex test.tex so that test.pdf is produced.

run pdfcrop test.pdf test-cropped.pdf

-
You've highlighted a (the?) reason in your answer: standalone does this in one step, while you have to perform this in two. In comparison, while making a stop-over en route to your destination still gets you there, some people would rather take the direct flight and avoid any overlays. –  Werner Apr 25 '12 at 14:06
It is easy to do it in one step. Just use write18 and few lines of TeX coding! –  Simurgh12 Apr 25 '12 at 14:43
Interesting, thanks Simburgh12. I'll keep that option in mind. My use case is embedding LaTeX in Markdown files to be processed into HTML (via ImageMagick conversion of PDFs). –  mrjf Apr 29 '12 at 8:35

TeX itself doesn't know anything about the actual extent of the visible characters/glyphs. It only uses rough box metrics (height, depth, width, italic correction, etc.) stored in TFM files to align the text. Since the glyphs may exceed their corresponding box -- as you can see in this example, it's usually impossible to compute a perfect bounding box using TeX/LaTeX. That's why you have to adapt it manually or approximate it.

Nonetheless, some DVI drivers like dvisvgm offer an option to analyze the glyphs and derive a tight box for the resulting image.

-