
\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}
편집하다: Firs 솔루션의 계산된 표 너비가 잘못되었습니다(아래 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