2

I am confused about why \begin{tabular}{|p{0.6\textwidth} | p{0.4\textwidth}|} generates a table with width larger than the page textwidth, since 0.6+0.4 should come out to be 1.0 and so I expected the table to be same width as textwidth. According to https://en.wikibooks.org/wiki/LaTeX/Page_Layout this is the Latex page layout

Mathematica graphics

but when I run the following MWE and tell geometry to draw the frame:

Mathematica graphics

I see my table going over the texwidth, even though my 2 columns add to 100% of the textwidth !

\documentclass[11pt]{book}
\usepackage[letterpaper,showframe]{geometry}

\begin{document}
\noindent
\begin{tabular}{|p{0.6\textwidth} | p{0.4\textwidth}|}\hline
A&B\\\hline
\end{tabular}

\end{document} 

Here is the pdf

Mathematica graphics

The question is: What is the correct way to code this so that the columns widths add to 100% of the textwidth and the table does not overflow the textwidth?

I am trying to make my table always to be the same as textwidth, and not use hardcoded width values in inches or cm as I was doing, but use fractions of the textwidth instead, so that if I change the style of the document or change pagesize, the table always stays as the textwidth in all cases.

  • 2
    Love the question! – A Feldman May 21 '16 at 2:21
  • @AFeldman Shouldn't you vote for it in that case? ;) – cfr May 21 '16 at 2:30
  • Surely this is a duplicate? – cfr May 21 '16 at 2:30
  • Yup, you have a very good point. I don't think so, I just looked. – A Feldman May 21 '16 at 2:31
4

Your calculations do not take account of the width of the | or of the separation between the columns. You could take these into account explicitly.

\documentclass[11pt]{book}
\usepackage[letterpaper,showframe]{geometry}
\usepackage{array,calc}
\newcolumntype{Y}[1]{>{\hsize=#1\hsize}X}
\begin{document}
\noindent
\begin{tabular}{|p{.6\textwidth-2\tabcolsep-.6pt} | p{0.4\textwidth-2\tabcolsep-.6pt}|}\hline
  A&B\\\hline
\end{tabular}
\end{document}

fit width

However, it is much simpler to let a package do it for you. tabularx can do this job. For example:

\documentclass[11pt]{book}
\usepackage[letterpaper,showframe]{geometry}
\usepackage{array,tabularx}
\newcolumntype{Y}[1]{>{\hsize=#1\hsize}X}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{|X | p{0.4\textwidth}|}\hline
  A&B\\\hline
\end{tabularx}

\noindent
\begin{tabularx}{\textwidth}{|Y{1.2} | Y{.8}|}\hline
  A&B\\\hline
\end{tabularx}
\end{document}

tabularx possibilities

In the first case, one column has specified width and the X expands to fill the remaining space - a bit less than .6\textwidth, as explained above.

In the second case, tabularx is asked to use two X columns but, rather than making them of equal size (as it would do if we used |X|X|, it is asked to figure out how to make one of them equal to .4 of the available width and the other equal to .6 of that width. It is vital that the total numbers here add up to the number of X columns. Here, there are 2 X columns and 1.2+.8 makes 2.

The code for the Y column specification is from the tabularx manual. If you use this, please see the documentation for some caveats which apply when using X columns of different widths. If you use one or more plain X columns (|X|p{.45\textwidth}| or XcXXr), these caveats don't apply. (But you should still read the manual, of course, if you use the package.)

| improve this answer | |

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.