\endhead text length affects longtable column width. How to avoid this

longtable

The width of long table is affected by one of the line \endhead. So if the actual table width was 2 inch based on the specs, but the \endhead text was longer than that, the last column stretches all the way to the right, even though it has say p{.1in}. An example will make this clear.

\documentclass[12pt]{article} 

\usepackage{amsmath}
\usepackage{longtable}

\begin{document}   
   \begin{longtable}[c]{|p{0.2in}|p{1in}|p{0.1in}|}\hline
   \# & ODE & A \\\hline
   \endfirsthead
   \multicolumn{3}{c}{\tablename \thetable {} -- second order series method. Regular singular point. Difference not integer. Continued from previous page}\\\hline 
   \# & ODE & A \\\hline
   \endhead \hline 
   \multicolumn{3}{|r|}{Continued on next page}\\\hline 
   \endfoot \hline 
   \endlastfoot      
   %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
\end{longtable}       
\end{document}

When compiled using lualatex gives

enter image description here

This is because the \endhead text is long. Help says

\endhead Specifies rows to appear at the top of every page.

I can make its fonts small. and this will help a little, like this, by changing the above one line to and adding \scriptsize, like this

   \multicolumn{3}{c}{\tablename \thetable {} -- {\scriptsize second order series method. Regular singular point. Difference not integer. Continued from previous page}}\\\hline 

Now it compiles to

enter image description here

But the last column width is still not according to the p specification in the table.

The last column stretches to accommodate the text that should show up at top of each page in the long table.

So if this text is long (more descriptive), the last column will overflow to the right and does not look good.

Is there a way to make the text in \endhead not affect the width of the table and keep each column same width as given in the p format?

Here is an example from the real pdf with the actual table which has 10 columns. The \endhead text is what shows at the top of the table on each page to describe what is in the table.

enter image description here

Since the description is long, the table to the right goes over the edge, even though the format p stops at the column heading called time. There should be nothing after that.

May be I could format \endhead text in multiple lines? How?

Here is example of another table, using same format p as the above, but because the endhead description is shorter, the last column does not overflow to the right

enter image description here

I just do not want to worry about the table description being too long and its affect on the actual table.

edit

I do use caption. But caption do not carry on each page,. It only show on first page of longtable. So if I put the description of the table in the caption, it will not show at top of next page. Only text that goes with \endhead carries over. Here is a MWE

\documentclass[12pt]{article} 
\usepackage{amsmath}
\usepackage{longtable}
\begin{document}   
   \begin{longtable}[c]{|p{0.2in}|p{1in}|p{0.1in}|}
   \caption{second order series method. Regular singular point. Difference not integer}\\\hline 
   \# & ODE & A \\\hline
   \endfirsthead
   \multicolumn{3}{c}{\tablename \thetable {} -- Continued from previous page}\\\hline 
   \# & ODE & A \\\hline
   \endhead \hline 
   \multicolumn{3}{|r|}{Continued on next page}\\\hline 
   \endfoot \hline 
   \endlastfoot      
   %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
 %first row
   1
   &2
   &3\\ \hline
   %second row
   1
   &2
   &3\\ \hline
\end{longtable}        
\end{document}

When compiled, first page shows

enter image description here

But next page do not have the caption description on it any more:

enter image description here

And that is the problem. I need the long description at top of each table on each page so I know what table I am looking at.

TL 2021 on Linux.

Best Answer

To keep the original table size try

    \begin{longtable}[c]{|p{0.2in}|p{1in}|p{0.1in}|}
        \caption{Second order series method. Regular singular point. Difference not integer.}\\
        \hline  
        \# & ODE & A \\\hline
        \endfirsthead
        \multicolumn{3}{@{}c@{}}{\parbox{\dimexpr1.3in+6\tabcolsep-2\arrayrulewidth\relax}{\small\tablename~\thetable\ Second order series method. Regular singular point.  Difference not integer\\ {\footnotesize \emph{Continued from previous page}}\vspace*{1ex}}}\\\hline 
        \# & ODE & A \\\hline
        \endhead \hline 
        \multicolumn{3}{|r|}{\footnotesize \emph{Continued on next page}}\\\hline 
        \endfoot \hline 
        \endlastfoot      
        %first row

a .

c

The parbox will do line breaks and allow \\ inside. Its width is equal to the interior width of the table so it will not expand the table unnecessarily.

Related Question