
\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}
Responder1
Como você tem números diferentes de colunas e, conseqüentemente, números diferentes de linhas verticais e \tabcolsep
espaços, é necessário, ao determinar a largura da coluna, considerar a largura das linhas verticais e \tabcolsep
espaços.
Experimente o seguinte:
\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}
Editar: A primeira solução tem largura tabular calculada incorretamente (veja o comentário de David Carlisle abaixo. Considerando isso, o código acima é corrigido adequadamente. Como prova, veja a imagem abaixo:
que é gerado 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}
Responder2
Quando você não precisa repetir o cabeçalho da tabela no início das próximas páginas (a função central do longtable
pacote) e quando você supõe apenas textos curtos nas células da tabela (sem parágrafos multilinhas), então você não precisa calcular as larguras das células manualmente. Você pode usar a construção primitiva \hbox to\hsize
combinada \hfil
em cada célula.
\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