1

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?

  • 1
    please provide complete small document (mwe: minimal working example) which demonstrate your problem. – Zarko Oct 12 '18 at 14:26
  • Yes, the width means the width of the texts in the columns. \arraycolsep gets added. – user121799 Oct 12 '18 at 14:30
  • @marmot but why two tables have same sums of widths of columns, but have different total widths showed in PDF? – user6703592 Oct 12 '18 at 14:57
  • Simply because there is an additional term (n-1)*\arraycolsep where n is the number of columns. – user121799 Oct 12 '18 at 14:59
  • 1
    It is next-to-impossible to write an answer since your question does not contain an MWE, i.e. a small compilable document starting with \documentclass and ending with \end{document}. One would have to guess too many things. – user121799 Oct 12 '18 at 16:05
2

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.

  • what's the meanings of \tabcolsep and \arrayrulewidth? And I still don't understand the logic why table1 and table 2 have the same width? – user6703592 Oct 14 '18 at 11:31
  • @user6703592, \tabcolsep is distance between cell content to cell border (vertical line in your case), \arrayrulewidth width of lines in table. both you can define width \setlength\tabcolsep{<desired distance>} and \setlength\arrayrulewidth{<desired width>} . their default values are 5pt and 015pt if i remember correctly. – Zarko Oct 14 '18 at 12:01
  • why do you use the coefficient 1.13 and 1.25 for \arrayrulewidt ? – user6703592 Oct 14 '18 at 12:49
  • and except for the manually calculating, is there any way to make two tables with different number of columns having same widths? – user6703592 Oct 14 '18 at 12:54
  • (i) 1.13 is approx. 8 lines/7 columns, i e. width of eight vertical lines divided to seven columns, similarly for 1.25 ~ 5/4. (ii) with use of tabularx environment. see en.wikibooks.org/wiki/LaTeX/Tables. – Zarko Oct 14 '18 at 14:25

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.