Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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 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}
share|improve this question
    
You have to pass the varwidth option to the class. –  egreg Nov 14 '12 at 18:36
add comment

3 Answers

up vote 11 down vote accepted

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.

share|improve this answer
add comment

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

share|improve this answer
    
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 Jun 16 '13 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 Jun 16 '13 at 16:28
add comment
\documentclass[preview,multi]{standalone}

\begin{document}
\preview
line 1
\endpreview

\preview
line 2
\endpreview
\end{document}
share|improve this answer
    
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. –  Heiko Oberdiek Jun 16 '13 at 15:53
    
@HeikoOberdiek: Your understanding is correct and mine is wrong. I did not read the question carefully. :-) –  I am who I say I am Jun 16 '13 at 15:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.