Tengo estas tablas creadas a partir de longtable, ¿por qué no tienen el mismo ancho?

Tengo estas tablas creadas a partir de longtable, ¿por qué no tienen el mismo ancho?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{longtable}

\title{Table Sample}
\author{Nimish Mistry}
\date{July 2017}

\begin{document}

\maketitle

\section{2 column table}
\begin{longtable}{|p{0.5\textwidth}|p{0.5\textwidth}|}
    \hline
    A & B \\
    \hline
\end{longtable}

\section{3 column table}
\begin{longtable}{|p{0.33\textwidth}|p{0.33\textwidth}|p{0.33\textwidth}|}
    \hline
    A & B & C \\
    \hline
\end{longtable}

\section{4 column table}
\begin{longtable}{|p{0.25\textwidth}|p{0.25\textwidth}|p{0.25\textwidth}|p{0.25\textwidth}|}
    \hline
    A & B & C & D \\
    \hline
\end{longtable}

\section{4 column table}

\end{document}

Respuesta1

Dado que tiene diferentes números de columnas y, en consecuencia, diferentes números de líneas y \tabcolsepespacios verticales, al determinar el ancho de la columna debe considerar el ancho de las líneas y \tabcolsepespacios verticales.

Pruebe lo siguiente:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array, longtable}% added array, as suggest David in hos comment below

\begin{document}
\begin{longtable}{|*{2}{p{\dimexpr0.5\textwidth-2\tabcolsep-1.5\arrayrulewidth\relax}|}} % calculation of column width
    \hline
    A & B \\
    \hline
\end{longtable}
\begin{longtable}{|*{3}{p{\dimexpr0.333\textwidth-2\tabcolsep-1.33\arrayrulewidth\relax}|}}
    \hline
    A & B & C \\
    \hline
\end{longtable}
\begin{longtable}{|*{4}{p{\dimexpr0.25\textwidth-2\tabcolsep-1.25\arrayrulewidth\relax}|}}
    \hline
    A & B & C & D \\
    \hline
\end{longtable}
\end{document}

ingrese la descripción de la imagen aquí

Editar: La primera solución tiene un ancho tabular calculado incorrectamente (consulte el comentario de David Carlisle a continuación. Teniendo esto en cuenta, el código anterior se corrige en consecuencia. Como prueba, vea la imagen a continuación:

ingrese la descripción de la imagen aquí

que es generado por:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array,longtable}
%-------------------------------------- only for show page layout
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\setlength\arrayrulewidth{22pt}
\begin{document}
\begin{longtable}{|*{2}{p{\dimexpr0.5\textwidth-2\tabcolsep-1.5\arrayrulewidth)\relax}|}} % calculation of column width
    \hline
    A & B \\
    \hline
\end{longtable}
\begin{longtable}{|*{3}{p{\dimexpr0.333\textwidth-2\tabcolsep-1.33\arrayrulewidth\relax}|}}
    \hline
    A & B & C \\
    \hline
\end{longtable}
\begin{longtable}{|*{4}{p{\dimexpr0.25\textwidth-2\tabcolsep-1.25\arrayrulewidth\relax}|}}
    \hline
    A & B & C & D \\
    \hline
\end{longtable}
\end{document}

Respuesta2

Cuando no necesita repetir el encabezado de la tabla al comienzo de las páginas siguientes (la función principal del longtablepaquete) y cuando supone solo textos cortos en las celdas de la tabla (sin párrafos de varias líneas), entonces no necesita calcular el ancho de las celdas manualmente. Puede utilizar la construcción primitiva \hbox to\hsizecombinada por \hfilen cada celda.

\def\ta|{\hbox to\hsize \bgroup \vrule height12pt depth5pt \taA}
\def\taA{\futurelet\next\taB}
\def\taB{\ifx\next\relax\egroup \else \expandafter\taC \fi}
\def\taC#1|{\quad\rlap{#1}\hfil\vrule \taA}

\hrule
\ta |A|B|C|\relax
\hrule

\medskip

\hrule
\ta |A|B|\relax
\hrule

\medskip

\hrule
\ta |A|B|C|D|\relax
\hrule

\bye

información relacionada