3

When I specify the width by \begin{longtable}p{xcm}

For example, here is my code

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\begin{document}

\newpage
\begin{longtable}{p{2cm}lp{2cm}lp{2cm}l}
\caption{Test}\\
\label{table:sh}\\
\hline\hline
2cm  &  2cm   & 2cm \\
\hline
Test &  test  & test  \\
\end{longtable}

\begin{longtable}{p{4cm}lp{4cm}lp{4cm}l}
\caption{Test}\\
\label{table:sh}\\
\hline\hline
4cm  &  4cm   & 4cm \\
\hline
Test &  test  & test  \\
\end{longtable}
\end{document}

And clearly the first and third column width have been changed but not the middle column. Why is that? How to fix it?

enter image description here

4

\begin{longtable}{p{2cm}lp{2cm}lp{2cm}l} means that you have 6 colums, 3 of them with specified width. Hence it is expected that the second column (l as left) has only width needed to enclose your test word.

2

There are missing & characters to use the correct columns here -- 6 columns are defined, but only 3 & are used.

Either remove the inner l column specifiers or use additional & characters.

The additional l columns aren't necessary, in my point of view -- the table will get too wide and LaTeX complains about overfull boxes.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}
\begin{document}

\begin{longtable}{p{2cm}lp{2cm}lp{2cm}l}
\caption{Test}\\
\label{table:sh}\\
\hline\hline
2cm  & & 2cm  & & 2cm \\
\hline
Test & & test & & test  \\
\end{longtable}

\begin{longtable}{p{4cm}lp{4cm}lp{4cm}l}
\caption{Test}\\
\label{othertable:sh}\\
\hline\hline
4cm  &  & 4cm   & & 4cm \\
\hline
Test &  & test  & & test  \\
\end{longtable}
\end{document}

enter image description here

1

You have the wrong number of &: you should have written

 x cm & & x cm & & x cm \\

In other words: you're not always in the column you believe you're in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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