# Longtable column problem: column alignment changes

I'm setting up a big table (few hundred pages) with similar data as presented. I've used longtable in the past with good success, no problem. However, when I try to do the same again, I get the following errors:

1. The heading (+ the line at the bottom of the page) of the table does not span the total table width.
2. Alignment of column changes at different places within the table.

I have tried to find the solution but without any problem. Any suggestions would be highly appreciated.

Using Kile 2.1.0 (KDE 4.8.2) on ubuntu 11.10.

The following is the code used for the table:

\documentclass[ twoside,openright,titlepage,fleqn,numbers=noenddot,headinclude,%1headlines,% letterpaper a4paper
11pt,a4paper,BCOR5mm,footinclude=true,cleardoublepage=empty,abstractoff % <--- obsolete, remove (todo)
]{scrreprt}
\usepackage{lscape}
\usepackage{longtable}
\begin{landscape}
\begin{small}
\begin{longtable}{cccccccccccccccc}
\caption[HTML Test Table]{Table shows the problem that I am facing with the correct alignment of all columns. I do not know hat is going on and I have been working on this for quite a while now, but with no success...}\label{c0B:tab:6kvis_full}\\
\hline
\multicolumn{1}{c}{HTM Name}&
\multicolumn{1}{c}{Northing}&
\multicolumn{1}{c}{Easting}&
\multicolumn{1}{c}{Val1}&
\multicolumn{1}{c}{Val1.1}&
\multicolumn{1}{c}{Val4.8}&
\multicolumn{1}{c}{Val5.6}&
\multicolumn{1}{c}{Val9.8}&
\multicolumn{1}{c}{A}&
\multicolumn{1}{c}{B}&
\multicolumn{1}{c}{C}&
\multicolumn{1}{c}{D}&
\multicolumn{1}{c}{E}&
\multicolumn{1}{c}{F}&
\multicolumn{1}{c}{Num1}&
\multicolumn{1}{c}{Num2}\\
\cline{4 - 8}
%NextLine
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{(Northing)}&
\multicolumn{1}{c}{(Easting)}&
\multicolumn{5}{c}{(NanoSec)}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}&
\multicolumn{0}{c}{}\\*
\hline
\multicolumn{16}{c}{Continued Table}\\
\hline
\multicolumn{1}{c}{HTM Name}&
\multicolumn{1}{c}{Northing}&
\multicolumn{1}{c}{Easting}&
\multicolumn{1}{c}{Val1}&
\multicolumn{1}{c}{Val1.1}&
\multicolumn{1}{c}{Val4.8}&
\multicolumn{1}{c}{Val5.6}&
\multicolumn{1}{c}{Val9.8}&
\multicolumn{1}{c}{A}&
\multicolumn{1}{c}{B}&
\multicolumn{1}{c}{C}&
\multicolumn{1}{c}{D}&
\multicolumn{1}{c}{E}&
\multicolumn{1}{c}{F}&
\multicolumn{1}{c}{Num1}&
\multicolumn{1}{c}{Num2}\\
\cline{4 - 8}
%NextLine
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{(Northing)}&
\multicolumn{1}{c}{(Easting)}&
\multicolumn{5}{c}{(NanoSec)}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{}\\*
\hline
\hline
\multicolumn{16}{c}{Continued on next page \dots}
\endfoot
\hline

\endlastfoot
%Data starts here
HTM20 M000012233919&00:00:12.78&-23:39:19.96&98&63&63&&104&-0.29&-0.22&-0.02&0.00&0.31&0.53&0.94&0.99\\
HTM20 M000020-322101&00:00:20.38&-32:21:01.23&118&315&515&521&322&-0.01&-0.28&-0.56&-0.84&-1.03&-1.17&0.97&1.06\\
HTM20 M000105-155107&00:01:05.42&-15:51:07.20&297&295&257&348&-&-0.24&-0.21&-0.06&0.23&0.10&0.01&0.93&0.97\\
HTM20 M000106-174126&00:01:06.31&-17:41:26.21&73&-&-&447&-&-&-&-0.69&-&-&-&0.85&1.14\\
HTM20 M000118-074626&00:01:18.04&-07:46:26.86&177&-&-&208&-&-&-&-0.06&-&-&-&0.91&0.99\\
HTM20 M000124-043759&00:01:24.50&-04:37:59.61&50&-&-&632&-&-&-&-0.96&-&-&-&0.42&0.56\\

HTM20 M000713-402337&00:07:13.41&-40:23:37.43&69&89&97&-&98&-0.01&-0.04&-0.11&-0.15&-0.24&-0.30&0.9&1.02\\
HTM20 M000720-611306&00:07:20.56&-61:13:06.73&150&138&134&-&86&0.26&0.20&0.18&0.05&0.08&0.10&0.97&0.98**\\
HTM20 M000800-233918&00:08:00.42&-23:39:18.04&154&-&-&374&-&-&-&-0.34&-&-&-&0.94&0.97\\
HTM20 M000801-524339&00:08:01.71&-52:43:39.90&124&127&101&-&256&-0.53&-0.30&-0.23&0.39&0.14&-0.03&0.98&1.05\\
HTM20 M000821-394522&00:08:21.18&-39:45:22.9&146&169&188&150&134&0.18&0.07&-0.01&-0.18&-0.18&-0.17&--&--\\
HTM20 M000826-255911&00:08:26.27&-25:59:11.29&120&259&360&468&-&-0.21&-0.33&-0.52&-0.56&-0.77&-0.92&0.92&0.95\\
HTM20 M000828-132930&00:08:28.02&-13:29:30.32&161&-&-&192&-&-&-&-0.07&-&-&-&0.96&--\\
HTM20 M000829-055845&00:08:29.33&-05:58:45.27&66&-&-&1322&-&-&-&-1.14&-&-&-&0.66&0.68\\
HTM20 M000831-141959&00:08:31.08&-14:19:59.93&53&-&-&604&-&-&-&-0.92&-&-&-&0.79&--\\
HTM20 M000837-461940&00:08:37.53&-46:19:40.85&75&98&95&-&114&-0.10&-0.06&-0.13&0.05&-0.17&-0.32&1.0&1.0\\
HTM20 M002122-513011&00:21:22.06&-51:30:11.9&49&59&56&-&45&0.12&0.12&0.03&0.21&-0.21&-0.22&1.11&--\\
HTM20 M002127-161028&00:21:27.55&-16:10:28.82&69&92&61&55&-&0.08&0.28&0.21&0.70&0.21&-0.34&0.95&1.01\\
HTM20 M002135-321637&00:21:35.67&-32:16:37.39&221&275&313&388&559&-0.17&-0.19&-0.21&-0.22&-0.24&-0.26&0.79&--\\
\ before the final
\hline
\end{longtable}
\end{small}
\end{landscape}


-
Welcome to TeX.sx! Until you have enough rep to post images you can upload them and wait for someone with enough rep to post them for you. See meta.tex.stackexchange.com/q/2136/19384 (about mid-answer) for details. Alternatievely, upload your image somewhere and post the link. –  Peter Jansson Feb 18 '13 at 9:14
You have to run LaTeX two or even three times for the column widths to stabilize. LaTeX writes out information at each run and reads it in the following one; the process eventually converges to uniform width across chunks. –  egreg Feb 18 '13 at 9:44
You should edit your posted code to make this into a compilable document. Add a \documentclass and the packages you are using. Also, remove the "<data removed>" lines from the table, as this disturbs the formatting. Remember \ before the final \hline. –  Andrew Swann Feb 18 '13 at 10:31
It is normal (and documented behaviour) for longtable to take a few runs it says Package longtable Warning: Table widths have changed. Rerun LaTeX. in the log file. But what do you intent \multicolumn{0}{c}{}& to mean? You can not have a zero column span, it happens not to generate an error but any effect it does have is entirely accidental. –  David Carlisle Feb 18 '13 at 10:31
@Peter, Thank you. I've now added an image, hopefully, someone can help. –  user26097 Feb 18 '13 at 22:19

The posted code does not compile for a number of reasons, including: (1) no \begin{document} and (2) several paragraph breaks in the middle of the table.

Correcting these, yields a table that is 196pts too wide for the page. Replacing the font by \tiny rather than \small (NB there is no enviornment small) helps a little. Rather than specifying all columns as centered it is better to choose the formatting according to the material in each column. Most of the columns with decimals would be best with the decimals aligned, the dcolumn package helps with that.

Putting this together we get:

\documentclass[twoside,openright,titlepage,fleqn,numbers=noenddot,headinclude,
11pt,a4paper,BCOR5mm,footinclude=true,cleardoublepage=empty,abstractoff % <--- obsolete, remove (todo)
]{scrreprt}
\usepackage{lmodern}
\usepackage{lscape}
\usepackage{longtable}
\usepackage{dcolumn}
\newcolumntype{d}{D{.}{.}{2}}

\begin{document}
\begin{landscape}
\tiny
\begin{longtable}{lllrrrrrddddddrr}
\caption[HTML Test Table]{Table shows the problem that I am facing with the correct alignment of all columns. I do not know hat is going on and I have been working on this for quite a while now, but with no success...}\label{c0B:tab:6kvis_full}\\
\hline
\multicolumn{1}{c}{HTM Name}&
\multicolumn{1}{c}{Northing}&
\multicolumn{1}{c}{Easting}&
\multicolumn{1}{c}{Val1}&
\multicolumn{1}{c}{Val1.1}&
\multicolumn{1}{c}{Val4.8}&
\multicolumn{1}{c}{Val5.6}&
\multicolumn{1}{c}{Val9.8}&
\multicolumn{1}{c}{A}&
\multicolumn{1}{c}{B}&
\multicolumn{1}{c}{C}&
\multicolumn{1}{c}{D}&
\multicolumn{1}{c}{E}&
\multicolumn{1}{c}{F}&
\multicolumn{1}{c}{Num1}&
\multicolumn{1}{c}{Num2}\\
\cline{4 - 8}
%NextLine
\multicolumn{1}{c}{}&
\multicolumn{1}{c}{(Northing)}&
\multicolumn{1}{c}{(Easting)}&
\multicolumn{5}{c}{(NanoSec)}&
&
&
&
&
&
&
&
\\*
\hline
\multicolumn{16}{c}{Continued Table}\\
\hline
HTM Name&
Northing&
Easting&
Val1&
Val1.1&
Val4.8&
Val5.6&
Val9.8&
A&
B&
C&
D&
E&
F&
Num1&
Num2\\
\cline{4 - 8}
%NextLine
&
(Northing)&
(Easting)&
\multicolumn{5}{c}{(NanoSec)}&
&
&
&
&
&
&
&
\\*
\hline
\hline
\multicolumn{16}{c}{Continued on next page \dots}
\endfoot
\hline
\endlastfoot
%Data starts here
HTM20 M000012233919&00:00:12.78&-23:39:19.96&98&63&63&&104&-0.29&-0.22&-0.02&0.00&0.31&0.53&0.94&0.99\\
HTM20 M000020-322101&00:00:20.38&-32:21:01.23&118&315&515&521&322&-0.01&-0.28&-0.56&-0.84&-1.03&-1.17&0.97&1.06\\
HTM20 M000105-155107&00:01:05.42&-15:51:07.20&297&295&257&348&-&-0.24&-0.21&-0.06&0.23&0.10&0.01&0.93&0.97\\
HTM20 M000106-174126&00:01:06.31&-17:41:26.21&73&-&-&447&-&-&-&-0.69&-&-&-&0.85&1.14\\
HTM20 M000118-074626&00:01:18.04&-07:46:26.86&177&-&-&208&-&-&-&-0.06&-&-&-&0.91&0.99\\
HTM20 M000124-043759&00:01:24.50&-04:37:59.61&50&-&-&632&-&-&-&-0.96&-&-&-&0.42&0.56\\
HTM20 M000713-402337&00:07:13.41&-40:23:37.43&69&89&97&-&98&-0.01&-0.04&-0.11&-0.15&-0.24&-0.30&0.9&1.02\\
HTM20 M000720-611306&00:07:20.56&-61:13:06.73&150&138&134&-&86&0.26&0.20&0.18&0.05&0.08&0.10&0.97&0.98**\\
HTM20 M000800-233918&00:08:00.42&-23:39:18.04&154&-&-&374&-&-&-&-0.34&-&-&-&0.94&0.97\\
HTM20 M000801-524339&00:08:01.71&-52:43:39.90&124&127&101&-&256&-0.53&-0.30&-0.23&0.39&0.14&-0.03&0.98&1.05\\
HTM20 M000821-394522&00:08:21.18&-39:45:22.9&146&169&188&150&134&0.18&0.07&-0.01&-0.18&-0.18&-0.17&--&--\\
HTM20 M000826-255911&00:08:26.27&-25:59:11.29&120&259&360&468&-&-0.21&-0.33&-0.52&-0.56&-0.77&-0.92&0.92&0.95\\
HTM20 M000828-132930&00:08:28.02&-13:29:30.32&161&-&-&192&-&-&-&-0.07&-&-&-&0.96&--\\
HTM20 M000829-055845&00:08:29.33&-05:58:45.27&66&-&-&1322&-&-&-&-1.14&-&-&-&0.66&0.68\\
HTM20 M000831-141959&00:08:31.08&-14:19:59.93&53&-&-&604&-&-&-&-0.92&-&-&-&0.79&--\\
HTM20 M000837-461940&00:08:37.53&-46:19:40.85&75&98&95&-&114&-0.10&-0.06&-0.13&0.05&-0.17&-0.32&1.0&1.0\\
HTM20 M002122-513011&00:21:22.06&-51:30:11.9&49&59&56&-&45&0.12&0.12&0.03&0.21&-0.21&-0.22&1.11&--\\
HTM20 M002127-161028&00:21:27.55&-16:10:28.82&69&92&61&55&-&0.08&0.28&0.21&0.70&0.21&-0.34&0.95&1.01\\
HTM20 M002135-321637&00:21:35.67&-32:16:37.39&221&275&313&388&559&-0.17&-0.19&-0.21&-0.22&-0.24&-0.26&0.79&--\\
\hline
\end{longtable}
\end{landscape}
\end{document}


After the first run of latex we get the following alignment problem:

and the log file contains

Package longtable Warning: Table widths have changed. Rerun LaTeX.


A second run yields the desired result:

-
Thank you for showing me the use of dcolumn. However, I have figured out that the problem lies elsewhere, which I've stated in my Answer below. –  user26097 Feb 20 '13 at 2:40

I used the process of elimination to understand where the problem was arising from. This is a very large document. Hence, it has many more packages than the ones I've listed above to ask the question.

As I was commenting things out, I realized that the use of the option

\pagestyle{scrheadings}


caused the table aligment to get messed up. While the warning:

Package longtable Warning: Table widths have changed. Rerun LaTeX.


would appear, it would not go away no matter how many times I reran LaTex.

So, currently the solution seems to be to comment out the \pagestyle{scrheadings}` as there seems to be conflicts between it and the longtable package. Having figured this out, I will now attempt to find how I can solve this conflict.