4

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.

Moriambar
  • 11,466
  • 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 05 '12 at 08:38
  • @JosephWright: I agree. I am very keen on applying the DRY (Don't Repeat Yourself) philosophy to my daily projects. :-) Thanks! – kiss my armpit Aug 05 '12 at 08:46

2 Answers2

10

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}
David Carlisle
  • 757,742
Heiko Oberdiek
  • 271,626
0

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}
s__C
  • 3,186