34

I tried to combine this answer Compile a LaTeX document into a PNG image that's as short as possible with my answer Add column in a multicolumn table

But the pdflatex -shell-escape table.tex ignores linebreaks and the 3 tables are horizontally aligned and not vertically.

table.tex

\documentclass[convert]{standalone}
% \documentclass{article}

\usepackage{booktabs} % for nice lines
\usepackage{siunitx}  % nice numbers and units and 'S' in table
\usepackage{multirow} % enable \multirow command

\begin{document}
short answer to your question:\\
\begin{tabular}{|c|c|S|}
\hline
Genre & Name & {Weight}\\\hline
Humain & Aragorn & 102 \\\hline
\multirow{4}{*}{Hobbit}&Frodon & 32.1\\\cline{2-3}
    &Sam & 3.2 \\\cline{2-3}
    &Peregrin & 2.232\\\cline{2-3}
    &Meriadoc & 23 \\\hline
Elfe&Legolas & 34.5 \\\hline
\end{tabular}\\[1cm]


You can also use the power of booktabs and siunitx:\\
\begin{tabular}[ht]{l c S} % alignment l = left, c = centre, S = at decimal - needs siunitx
\toprule
Genre&Name& {Weight}\\  % protect non numbers with {} in S columns
\midrule
Elfe&Legolas & 34.5\\
Dwarf&Peter & 103.31\\
\bottomrule
\end{tabular}\\[1cm]

And combine that with your table:\\
\begin{tabular}{c c S}
\toprule
Genre & Name & {Weight}\\\midrule
Humain & Aragorn & 102 \\\midrule
\multirow{4}{*}{Hobbit}&Frodon & 32.1\\\cline{2-3}
    &Sam & 3.2 \\\cline{2-3}
    &Peregrin & 2.232\\\cline{2-3}
    &Meriadoc & 23 \\\midrule
Elfe&Legolas & 34.5 \\
\bottomrule
\end{tabular}


\end{document}
1
  • 3
    You have to pass the varwidth option to the class.
    – egreg
    Commented Nov 14, 2012 at 18:36

3 Answers 3

40

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.

11

There are several possibilities:

Table

\documentclass{standalone}

\begin{document}
\begin{tabular}{@{}l@{}}
  line 1\\
  line 2
\end{tabular}
\end{document}

Result

Remarks:

  • A tabular adds \struts 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}

Result

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}

Clipped result

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:

Result

2
  • Thanks. That's very nice, but it only works for roman. If I use italics, a portion is clipped (as you see in the edit to the question). If I use a new font, a large chunk doesn't even appear. What is the general solution?
    – Calaf
    Commented Jun 16, 2013 at 16:12
  • On second thought, let me ask it as a sequel since the issue appears to be orthogonal to standalone and varwidth.
    – Calaf
    Commented Jun 16, 2013 at 16:28
5
\documentclass[preview,multi]{standalone}

\begin{document}
\preview
line 1
\endpreview

\preview
line 2
\endpreview
\end{document}
2
  • That generates two pages. My understanding of the question was that the lines should be on one page in two lines and not in one line. Commented Jun 16, 2013 at 15:53
  • @HeikoOberdiek: Your understanding is correct and mine is wrong. I did not read the question carefully. :-) Commented Jun 16, 2013 at 15:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .