\documentclass[convert]{standalone} ignores linebreak
By default the standalone
class doesn't make paragraphs. However, with the varwidth
option it does; so
\documentclass[varwidth,convert]{standalone}
will do what you want, adjusting the width to the longest line.
There are several possibilities:
Table
\documentclass{standalone}
\begin{document}
\begin{tabular}{@{}l@{}}
line 1\\
line 2
\end{tabular}
\end{document}
Remarks:
- A
tabular
adds\strut
s in the rows, that might cause small white margins. - Class
standalone
only uses the character bounding boxes and does not know about the bounding boxes of the glyph appearance. Thus additional margin makes sense to avoid cut off characters. - The result can be run through
pdfcrop
to remove the remaining white margins.
Package varwidth
The environment varwidth
of the package with the same name is a kind of minipage
that is horizontally shrunk afterwards:
\documentclass{standalone}
\usepackage{varwidth}
\begin{document}
\begin{varwidth}{\linewidth}
line 1
line 2
\end{varwidth}
\end{document}
Class standalone
provides the option varwidth
for this use case (thanks mozartstrasse for the hint). That simplifies the example:
\documentclass[varwidth]{standalone}
\begin{document}
line 1
line 2
\end{document}
Character clipping
The problem that the character glyphs are outside their official font bounding boxes cannot be solved inside TeX, because TeX does only knows the official character bounding boxes and not their visual appearances.
Example:
\documentclass[varwidth]{standalone}
\begin{document}
\raggedleft
\itshape
line f
line $\not$
\end{document}
Part of f
is not visible and \not
has vanished entirely (extreme example, because \not
has width zero, it overlaps the following relational operator).
As workaround a larger margin can be added:
\documentclass[margin=10pt,varwidth]{standalone}
And the result is cropped, e.g. via pdfcrop
:
\documentclass[preview,multi]{standalone}
\begin{document}
\preview
line 1
\endpreview
\preview
line 2
\endpreview
\end{document}