Graphics, Figures & TablesHow to prevent a table from crossing the margin area?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

How to prevent a table from crossing the margin area?

Post by yoyoimut »

Hello,
overflow.jpg
overflow.jpg (129.61 KiB) Viewed 5053 times

Code: Select all

\documentclass[a4paper,twoside,11pt,final,dvips]{book}
\usepackage[includemp=true,margin=3cm]{geometry}
\usepackage{array,longtable,lipsum,xcolor}

%------------------------- MARKER ------------------------	
\newcommand*{\MARKER}%
{%	
	\par\noindent\strut\vrule\hrulefill~{\color{red} text area}~\hrulefill\vrule%
	\marginpar{\strut\vrule\hrulefill~{\color{red} margin area}~\hrulefill\vrule}\par%
} 


\begin{document}
\MARKER
\begin{longtable}{|m{0.5\textwidth}|m{0.5\textwidth}|}\hline
\lipsum[1]&\lipsum[3]\\\hline
\end{longtable}

\pagebreak
\MARKER
\begin{longtable}{|m{0.5\textwidth}|m{0.5\textwidth}|}\hline
\lipsum[1]&\lipsum[3]\\\hline
\end{longtable}
\end{document}
I tried to make the table fit the text area by setting each column to the half of the text area width. However, the table still crosses the margin area.
How to prevent the table from crossing the margin?


thank you in advance.


regards,

Yuko

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

How to prevent a table from crossing the margin area?

Post by gmedina »

Hi,

dont't forget that in addition to the width of the columns (0.5\textwidth in your example) you have to take into account the space between columns (controlled by the length \tabcolsep (default value: 6pt) in a tabular environment and by \arraycolsep in an array environment (default value: 5pt)) and the width of the rules of the table (controlled by \arrayrulewidth (default value: 0.4pt)).

Setting those values to 0pt will prevent your table from crossing the margin area, but of course, there will be no separation between columns and there will be no rules:

Code: Select all

\documentclass[a4paper,twoside,11pt,final]{book}
\usepackage[includemp=true,margin=3cm]{geometry}
\usepackage{array,longtable,lipsum,xcolor}

%------------------------- MARKER ------------------------   
\newcommand*{\MARKER}%
{%   
   \par\noindent\strut\vrule\hrulefill~{\color{red} text area}~\hrulefill\vrule%
   \marginpar{\strut\vrule\hrulefill~{\color{red} margin area}~\hrulefill\vrule}\par%
}

\begin{document}

\setlength\tabcolsep{0pt}
\setlength\arrayrulewidth{0pt}

\MARKER
\begin{longtable}{|m{0.5\textwidth}|m{0.5\textwidth}|}\hline
\lipsum[1]&\lipsum[3]\\\hline
\end{longtable}

\pagebreak
\MARKER
\begin{longtable}{|m{0.5\textwidth}|m{0.5\textwidth}|}\hline
\lipsum[1]&\lipsum[3]\\\hline
\end{longtable}

\end{document}
That's why the sum of the widths of the columns of a table should always be slightly less than \textwidth.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
yoyoimut
Posts: 120
Joined: Mon Oct 19, 2009 6:58 am

How to prevent a table from crossing the margin area?

Post by yoyoimut »

Thank gmedina for your useful information about the three length properties of tabular and array.


I use calc.sty and do the calculation as follows:

Code: Select all


\newlength{\mycolwidth}

%number of columns, N = 2
%tabcolsep = 6pt
%arrayrulewidth = 0.4pt

%12.6pt = (2 x N x tabcolsep  + (N + 1) x arrayrulewidth) / N 
%12.6pt = (2 x 2 x 6pt  + (2 + 1) x 0.4pt) / 2

\setlength{\mycolwidth}{0.5\textwidth-12.6pt}

regards,

Yuko
Post Reply