2
\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}
  • 2
    welcome to tex.se! probably because you have different numbers of columns and consequesntly diferent numbers of vertical lines and tabcolsep spaces – Zarko Jul 10 '17 at 13:27
  • I'm using Sharelatex, compiled just once! – Nimish Mistry Jul 10 '17 at 13:29
  • Then how do I ensure that my tables have the same width? – Nimish Mistry Jul 10 '17 at 13:33
  • Maybe first try to follow the advice and compile twice :) – Dr. Manuel Kuehner Jul 10 '17 at 13:40
  • @Dr.ManuelKuehner, sorry that did not work! – Nimish Mistry Jul 10 '17 at 13:56
2

Since you have different numbers of columns and consequently different numbers of vertical lines and \tabcolsep spaces, you need in determining of column width to consider width of vertical lines and \tabcolsep spaces.

Try the following:

\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}

enter image description here

Edit: Firs solution has wrong calculated tabular width (see comment of David Carlisle below. Considering it the code above is accordingly corrected. As proof se image below:

enter image description here

which is generated by:

\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}
  • @DavidCarlisle, thank you to point on errors in my answer. I correct it now. – Zarko Jul 10 '17 at 19:59
0

When you does not need to repeat the header of the table at beginning of next pages (the core function of longtable package) and when you supposes only short texts in table cells (no multiline paragraphs) then you need not to calculate cell widths manually. You can use the primitive construct \hbox to\hsize combined by \hfil in each cell.

\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

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.