longtable からこれらのテーブルを作成しましたが、なぜ幅が同じではないのでしょうか?

longtable からこれらのテーブルを作成しましたが、なぜ幅が同じではないのでしょうか?
\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}

答え1

列の数が異なるため、垂直線とスペースの数も異なるため、列幅を決定する際には垂直線とスペース\tabcolsepの幅を考慮する必要があります。\tabcolsep

次のことを試してください。

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

ここに画像の説明を入力してください

編集: 最初のソリューションでは、表の幅の計算が間違っています (以下の David Carlisle のコメントを参照してください)。これを考慮して、上記のコードは修正されています。証拠として、以下の画像を参照してください。

ここに画像の説明を入力してください

これは以下によって生成されます:

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

答え2

次のページの先頭で表のヘッダーを繰り返す必要がなく (longtableパッケージのコア機能)、表のセルに短いテキストのみ (複数行の段落なし) があると想定される場合は、セルの幅を手動で計算する必要はありません。各セルで\hbox to\hsizeを組み合わせたプリミティブ構造を使用できます。\hfil

\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

関連情報