[Tex/LaTex] How to control the width of longtable

longtablewidth

That's very strange why the sum of the width of each column does not equal the total width of longtable. I have two tables:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{array}
\usepackage{titletoc}
\usepackage{verbatim}
\usepackage{tikz}
\usepackage{longtable}

\begin{document}
\textwidth 15.95cm
\begin{center}
    \begin{longtable}{
    |p{0.25cm}
    |p{4.6cm}
    |p{2 cm}
    |p{2.2cm}
    |p{0.8cm}
    |p{2.1cm}
    |p{1.9cm}|
    }
    \multicolumn{7}{>{\centering}p{\textwidth}}{1
    }\tabularnewline\hline
    a & a & a & a & a & a & a
    \tabularnewline\hline
    \end{longtable}
\end{center}

\begin{center}
    \begin{longtable}{
    | >{\centering}p{13cm}
    | >{\raggedright}p{9.cm}
    | >{\raggedright}p{2.42cm}
    | >{\raggedright}p{2.42cm} |
    }
    \multicolumn{4}{>{\centering}p{\textwidth}}{
    2
    }\tabularnewline\hline
    a & a & a & a
    \tabularnewline\hline
    \end{longtable}
\end{center}


\end{docume

Two tables in the PDF exactly have same widths.

As marmot said the total width of a table should be

sum of columns + (n-1)*\arraycolsep

And how to know the value of \arraycolsep?

More generally, how to make all the tables in the PDF has same widths? And what's the meaning of width \textwidth in \multicolumn{7}{>{\centering}p{\textwidth}}.

Moreover, when I decrease the width of some columns, the width of first line of table will decrease and the second line will not change; when I increase the width of some columns, both first and second lines will increase?

Best Answer

your tables has more issues.

  • sum of column width are not equal to \textwidth (15.95 cm),
  • width of the multi column cells are equal to \textwidth, so it extend lats column that the width of the whole table is equal to \textwidth
  • in examples below are in determination of columns width considered \tabcolsep and \arrayrulewidth.

    \documentclass{article}
    \usepackage[hmargin=2.525cm]{geometry}%
    %\textwidth 15.95cm
    
    \usepackage{array, longtable}
    \newcolumntype{C}[1]{>{\centering\arraybackslash}  p{#1}}
    \newcolumntype{R}[1]{>{\raggedright\arraybackslash}p{#1}}
    
    %-------------------------------------- only for show page layout
    \usepackage{showframe}
    \renewcommand\ShowFrameLinethickness{0.25pt}
    \renewcommand*\ShowFrameColor{\color{red}}
    %---------------------------------------------------------------%
    \begin{document}
    %\begin{center}
    width of table determined by sum of column width
    
    \begin{longtable}{
    |p{\dimexpr0.25cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr4.6cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2 cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.2cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr0.8cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.1cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr1.9cm-2\tabcolsep-1.13\arrayrulewidth}|
    }
    %\multicolumn{7}{|C{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{1}
    \hline
    a & a & a & a & a & a & a   \\
    \hline
    \end{longtable}
    %\end{center}
    
    width of table determined by multi column cell width
    
    \begin{longtable}{
    |p{\dimexpr0.25cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr4.6cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2 cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.2cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr0.8cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.1cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr1.9cm-2\tabcolsep-1.13\arrayrulewidth}|
    }
    \multicolumn{7}{|C{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{1}    \\
    \hline
    a & a & a & a & a & a & a   \\
    \hline
    \end{longtable}
    
    
    width of the multi column cell is adopted to sum of ll columns widths
    
    \begin{longtable}{
    |p{\dimexpr0.25cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr4.6cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2 cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.2cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr0.8cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr2.1cm-2\tabcolsep-1.13\arrayrulewidth}
    |p{\dimexpr1.9cm-2\tabcolsep-1.13\arrayrulewidth}|
    }
    \multicolumn{7}{|c|}{1}    \\
    \hline
    a & a & a & a & a & a & a   \\
    \hline
    \end{longtable}
    
    second table:
    
    %\begin{center}
    \begin{longtable}{
    | C{\dimexpr1.3cm-2\tabcolsep-1.25\arrayrulewidth}% 13cm
    | R{\dimexpr9.0cm-2\tabcolsep-1.25\arrayrulewidth}
    | R{\dimexpr2.42cm-2\tabcolsep-1.25\arrayrulewidth}
    | R{\dimexpr2.42cm-2\tabcolsep-1.25\arrayrulewidth} |
    }
    %\multicolumn{4}{|C{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{2}
    \hline
    a & a & a & a   \\
    \hline
    \end{longtable}
    %\end{center}
    
    \begin{longtable}{
    | C{\dimexpr1.3cm-2\tabcolsep-1.25\arrayrulewidth}% 13cm
    | R{\dimexpr9.0cm-2\tabcolsep-1.25\arrayrulewidth}
    | R{\dimexpr2.42cm-2\tabcolsep-1.25\arrayrulewidth}
    | R{\dimexpr2.42cm-2\tabcolsep-1.25\arrayrulewidth} |
    }
    \multicolumn{4}{|C{\dimexpr\linewidth-2\tabcolsep-2\arrayrulewidth}|}{2}    \\
    \hline
    a & a & a & a   \\
    \hline
    \end{longtable}
    
    \end{document}
    

enter image description here

(red lines indicates border of text area).

the first and second table without of multi column cells have different widths. the purpose of the first table row is not clear. see if the third version of the first table gives what you like to achieve.

Related Question