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.

Why is a longtable environment on top of pages vertically placed different to a tabular environment? I'm digging several days on this question and didn't find a way to align the longtable vertically in the same way.

Code:

\documentclass[11pt,a4paper]{article}
\usepackage{longtable}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pellentesque iaculis nunc eget congue. Etiam lobortis nisi velit. Proin tristique massa a lectus ullamcorper semper. Aenean ut dignissim diam. Integer ullamcorper eros nibh. Cras molestie neque quis lectus lobortis egestas. Maecenas vel tortor in nulla sagittis venenatis. Nulla ac eros dui, eget lacinia ante. Fusce elementum nisl ac tortor hendrerit id lacinia orci malesuada. Praesent eu iaculis mi. Vestibulum sodales tempor rutrum. }
\newcommand{\examplelongtable}{%
    \begin{longtable}{|l|p{58mm}|}
    %
    \hline
    \endfirsthead
    %
    \hline\multicolumn{2}{|l|}{\textsl{\ldots continued}}
    \endhead
    %
    \multicolumn{2}{|l|}{\textsl{will be continued\ldots}}\\*
    \hline
    \endfoot
    %
    \hline
    \endlastfoot
    %
    aa: & 4711\\
    \hline%
    aaa: & 2\\
    \hline%
    aaaa: & 4711-2\\
    \end{longtable}
}
\newcommand{\exampletabular}{%
    \begin{tabular}[H]{|l|p{58mm}|}
    \hline
    aa: & 4711\\
    \hline%
    aaa: & 2\\
    \hline%
    aaaa: & 4711-2\\
    \hline
    \end{tabular}
}
\setlength{\parskip}{1.0\baselineskip}
\setlength{\LTpre}{0pt}
\setlength{\LTpost}{0pt}
\setlength{\LTleft}{\parindent}

\begin{document}
\exampletabular

\lorem

\lorem

\lorem

\lorem

\lorem
\examplelongtable
\end{document}

Supposedly there is a very simple solution, but I seem to be unable to find it...

If I explicitly set a \newpage before the longtable example, an adapted \vspace*{-...} would help, but not on a regular page-break, where the source code doesn't "know" where it occurs.

share|improve this question
1  
There is no [H] option to tabular. If you use [t], instead, the alignment will be the same. –  egreg Sep 18 '12 at 17:26
1  
@egreg The additional rules and box levels interfere with \topskip. The top element in the table, the top line, is aligned with the baseline of the first line in the text body. In the case of longtable. Here the first header box has height 0.4pt because of the rule. Then 10.6pt of \topskip (11pt) are added. –  Heiko Oberdiek Sep 18 '12 at 18:16
2  
@HeikoOberdiek yes there ought to be some option of controlling the position of the head really. (Well if you know what you are doing you can \noalign some negative space into the head, but it could be easier. –  David Carlisle Sep 18 '12 at 18:54
    
+1 @egreg: that's true, I must have been confused with table and float package. At the top, both tables are equally aligned with \begin{tabular}[t]{|l|p{58mm}|} - but, can anyone explain, why not at bottom, too? Putting one more \lorem at the end shows about 5.0pt more distance than below tabular... –  tueftl Sep 26 '12 at 6:21
1  
@tueftl There is a big difference between a [t] tabular and a longtable; a [t] tabular is set in a \halign wrapped in a \vtop, which means that only the first row contributes to the height of the box, the rest is depth; thus the interline glue after this tabular will quite certainly be \lineskip. On the contrary, longtable makes only the \halign, so that each row contributes autonomously to height and depth. –  egreg Sep 26 '12 at 9:07

1 Answer 1

up vote 2 down vote accepted

Not going into depth, I experimentally found the following, which only works with a 0pt \topskip and outside-bordered tables: there are different measures to be applied prior to the tables depending on what precedes which kind of table. Only if tabular follows tabular, nothing is to be added. So all distances are as if only tabular would be used (note: without table environment, so not floating, just in place).

The code is tested with different values of \parskip and \arrayrulewidth (EDIT: and \lineskip). Accuracy is about 0.05pt. The two table variants are exchangeable, page breaks work (everything is vertically aligned equally to the upper page border).

\documentclass[11pt,a4paper]{article}
\usepackage{longtable}

\newcommand{\lorem}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis pellentesque iaculis nunc eget congue. Etiam lobortis nisi velit. Proin tristique massa a lectus ullamcorper semper. Aenean ut dignissim diam. Integer ullamcorper eros nibh. Cras molestie neque quis lectus lobortis egestas. Maecenas vel tortor in nulla sagittis venenatis. Nulla ac eros dui, eget lacinia ante. Fusce elementum nisl ac tortor hendrerit id lacinia orci malesuada. Praesent eu iaculis mi. Vestibulum sodales tempor rutrum. }
\newcommand{\examplelongtable}{%
    \begin{longtable}[H]{|l|p{58mm}|}
    %
    \hline
    \endfirsthead
    %
    \hline\multicolumn{2}{|l|}{\textsl{\ldots continued}}
    \endhead
    %
    \multicolumn{2}{|l|}{\textsl{will be continued\ldots}}\\*
    \hline
    \endfoot
    %
    \hline
    \endlastfoot
    %
    aa: & 4711\\
    \hline%
    aaa: & 2\\
    \hline%
    aaaa: & 4711-2\\
    \end{longtable}
}
\newcommand{\exampletabular}{%
    \begin{tabular}[t]{|l|p{58mm}|}
    \hline%
    aa: & 4711\\
    \hline%
    aaa: & 2\\
    \hline%
    aaaa: & 4711-2\\
    \hline
    \end{tabular}
}
\setlength{\parskip}{1.0\baselineskip}

\begin{document}
\setlength{\topskip}{0pt}
\setlength{\LTpre}{0pt}
\setlength{\LTpost}{\dimexpr\lineskip-6.0pt\relax}  % If '\lineskip' (or the whichever 6.0pt measure) would be changed after this line, '\LTpost' would become out of date.
\setlength{\LTleft}{\parindent}

\lorem

\newpage

%\lorem

\vspace{-\parskip}
\exampletabular

\vspace{\dimexpr\parskip+\lineskip\relax}%  % Only if 'tabular' is followed by a 'longtable'.
\examplelongtable

\vspace{\dimexpr\parskip+6.0pt\relax}%  % Only if 'longtable' is followed by another 'longtable'.
\examplelongtable

\vspace{\dimexpr\arrayrulewidth-7.57pt\relax}%  % Only if 'longtable' is followed by a 'tabular'.
\exampletabular

\exampletabular

\vspace{\dimexpr\parskip+\lineskip\relax}%  % Only if 'tabular' is followed by a 'longtable'.
\examplelongtable

\lorem

\lorem

\lorem

\vspace{\dimexpr\baselineskip-\arrayrulewidth-2.08pt\relax}%    % Only if text is followed by a 'longtable'.
\examplelongtable

\lorem
\end{document}

Note that this question regarding longtable at page breaks might be of interest also.

Does anyone know the origin resp. name of the 6.0pt measure, or any of the other two twisted measures?

share|improve this answer

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.