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.

The following table has unwanted right and left white spaces. But you cannot see them because the figure has been converted to PNG with alpha channel enabled.

How to trim the left and right white spaces when a table is previewed?

enter image description here

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,array}
\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt
\PreviewEnvironment{longtable}

\begin{document}
\begin{longtable}{*3{m{\dimexpr0.5\linewidth/3\relax}}}
        \toprule
        Unit & Macro & Symbol\\
        \midrule
        ampere & \verb|\ampere| & \si{\ampere}\\
        candela & \verb|\candela|& \si{\candela}\\
        kelvin & \verb|\kelvin|& \si{\kelvin}\\
        kilogram & \verb|\kilogram|& \si{\kilogram}\\
        meter & \verb|\meter|& \si{\meter}\\
        mole & \verb|\mole|& \si{\mole}\\
        second & \verb|\second|& \si{\second}\\
        \bottomrule
\end{longtable}
\end{document}

Note that I am not talking about the white spaces that can be removed by @{}s. The unwanted white spaces can be understood if you compile my MWE.

share|improve this question
2  
Not an answer to the question, but I would suggest using an s coulmn for the units. You might also want to define a function which prints the name and the unit, as I do in the siunitx manual, to avoid repeating material in your source. –  Joseph Wright Aug 5 '12 at 8:38
    
@JosephWright: I agree. I am very keen on applying the DRY (Don't Repeat Yourself) philosophy to my daily projects. :-) Thanks! –  Who is crazy first Aug 5 '12 at 8:46

3 Answers 3

up vote 10 down vote accepted

Implicit change from longtable to tabular

In simple cases, where the use of longtable does not contain more than tabular would allow, the longtable environment could be redefined to be a tabular environment:

\ifPreview
  \let\longtable\tabular
  \let\endlongtable\endtabular
\fi
\PreviewEnvironment{longtable}

Anyway, the longtable is not broken across pages, when taken as preview element. Also other features of longtable will not work, e.g. \endfoot.

Adjusting \hsize

The longtable environment sets chunks with width \hsize, filled left with \LTleft and on the right side with \LTright. The widths of the columns are remembered in the .aux file. The latter data can be used to calculate the width of the table. Then \hsize is set to the total width. \linewidth can remain unchanged, it is not used in longtable.

\documentclass{article}
\usepackage{siunitx}
\usepackage{booktabs,longtable,array}
\usepackage[active,tightpage]{preview}
\PreviewBorder=0pt

\makeatletter
\ifPreview
  \newcommand*{\SetLongtableWidth}{%
    \begingroup
      \edef\@ltnum{\@roman\numexpr\value{LT@tables}+1}%
      \@ifundefined{LT@\@ltnum}{%
        \gdef\@ltwidth{0pt}%
      }{%
        \def\LT@entry##1##2{+##2}%
        \xdef\@ltwidth{\the\dimexpr 0pt\csname LT@\@ltnum\endcsname}%
      }%
    \endgroup
    \ifdim\@ltwidth>\z@
      \hsize=\@ltwidth\relax
    \fi
  }%
  \newcommand{\org@longtable}{}%
  \let\org@longtable\longtable
  \def\longtable{\SetLongtableWidth\org@longtable}%
\fi
\makeatother

\PreviewEnvironment{longtable}

\begin{document}
\begin{longtable}{*3{m{\dimexpr0.5\linewidth/3\relax}}}
        \toprule
        Unit & Macro & Symbol\\
        \midrule
        ampere & \verb|\ampere| & \si{\ampere}\\
        candela & \verb|\candela|& \si{\candela}\\
        kelvin & \verb|\kelvin|& \si{\kelvin}\\
        kilogram & \verb|\kilogram|& \si{\kilogram}\\
        meter & \verb|\meter|& \si{\meter}\\
        mole & \verb|\mole|& \si{\mole}\\
        second & \verb|\second|& \si{\second}\\
        \bottomrule
\end{longtable}
\end{document}
share|improve this answer

You can add @{} on each side of your table (skipping the header) :

\begin{document}
\begin{longtable}{@{}*3{m{\dimexpr0.5\linewidth/3\relax}}@{}}
\toprule
etc.
\midrule
etc
\bottomrule
\end{longtable}
\end{document}
share|improve this answer
    
The unwanted white spaces I am talking about are not what you said above. –  Who is crazy first Aug 5 '12 at 7:07

Add the following command to you preamble:

\renewcommand \PreviewBbAdjust {2.4cm -\PreviewBorder -2.4cm \PreviewBorder}

See the manual page 5.

share|improve this answer
    
Is there a solution without using hard-coded constants? –  Who is crazy first Aug 5 '12 at 8:12

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.