# How do I stop a table where the margin falls, rather than using the textwidth command?

I've been having some problems with nesting tables in the tabularx environment. Basically, I would like each table to stop at the margin. Ordinarily, I would achieve this with \textwidth as the width setting, however, when I nest tables, because each tabularx adds white space before and after the table (and I don't want to just delete the white space), the text "overflows". Here is a MWE:

\documentclass{article}
\usepackage[letterpaper,margin=2in]{geometry}
\usepackage{tabularx}

\begin{document}
\noindent
Here is a bunch of text to demonstrate where the margin naturally falls. Note that the vertical lines around the tables show where each level has it's break''.

\noindent
\begin{tabularx}{\textwidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good.\\
{\begin{tabularx}{\textwidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around.
{\begin{tabularx}{\textwidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}
\end{document}


Basically I would like something that tells tabularx to wrap each line at the margin. Anyone know how to do this?

Edit: Solved the initial problem (Thanks, Werner!) but then realized that using \linewidth doesn't account for space added before the tabularx. MWE:

\documentclass{article}
\usepackage[letterpaper,margin=2in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx

\begin{document}
\noindent
Here is a bunch of text to demonstrate where the margin naturally falls. Note that the vertical lines around the tables show where each level has it's break''.

\bigskip

\noindent
\begin{tabularx}{\textwidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\textwidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\textwidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\bigskip

\noindent
\begin{tabularx}{\linewidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\linewidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\linewidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\vspace{1em}
The second set of nested tables seems to work great, but then it runs into problems when I add space before the nested tables, which I would like to be able to do.
\vspace{1em}

\hspace{30pt}
\begin{tabularx}{\linewidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\linewidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\linewidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\end{document}


Anyone know how to fix this?

• 99 times out of a hundred if you find yourself using nested tabularx then the material should not be in a table at all as it is not tabular but rather an indented textual layout, in which case it is almost always better to use a list. – David Carlisle Oct 11 '13 at 7:05
• Did not think of that at all, this works great, thanks. – Abe Schulte Oct 11 '13 at 16:26

Use a width of \linewidth, which adjusts with the width of the line:

\documentclass{article}
\usepackage[letterpaper,margin=2in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tabularx}% http://ctan.org/pkg/tabularx

\begin{document}
\noindent
Here is a bunch of text to demonstrate where the margin naturally falls. Note that the vertical lines around the tables show where each level has it's break''.

\bigskip

\noindent
\begin{tabularx}{\textwidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\textwidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\textwidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\bigskip

\noindent
\begin{tabularx}{\linewidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\linewidth}{X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\linewidth}{X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\end{document}


Perhaps more in line with "wrapping at the margin" might be using {X@{}} as a column specifier for the two inner tabularx's:

%...
\noindent
\begin{tabularx}{\linewidth}{X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good. \\
{\begin{tabularx}{\linewidth}{X@{}}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around. \\
{\begin{tabularx}{\linewidth}{X@{}}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}
%...

• Thanks a bunch, both of those are great. If you get a chance, would you be able to look at my edit to the original post? Basically, I would like to be able to have "the fix" work even if I add space before the table. – Abe Schulte Oct 11 '13 at 5:57
• @AbeSchulte: You mean the left margin? Use @{} to remove any column separation. So, perhaps {@{}X|} for the outer tabularx and {@{}X@{}} for the inner two. – Werner Oct 11 '13 at 5:59
• No, I mean if I manually add space before the entire thing (so that the nested tabularxs are not in the center), then the "nest" gets moved, including the text, which then goes into the margin, as can be seen by my edited MWE. – Abe Schulte Oct 11 '13 at 6:06
• @AbeSchulte: Ahhh, you could use \hspace{30pt}% (note the use of % and then use (as tabularx width) \dimexpr\linewidth-30pt. Does that answer your question, or are you looking for something more dynamic? – Werner Oct 11 '13 at 6:15
• @AbeSchulte - If you follow Werner's advice, be sure to put a \noindent instruction immediately before \hspace{30pt}. – Mico Oct 11 '13 at 6:18

your tabular rows always have a \tabcolsep at the beginning. It is also possible to change the left margin instead of using a tabular:

\documentclass{article}
\usepackage[letterpaper,margin=2in]{geometry}
\usepackage{tabularx}

\begin{document}
\noindent
Here is a bunch of text to demonstrate where the margin naturally falls. Note that the vertical lines around the tables show where each level has it's break''.

\noindent
\begin{tabularx}{\textwidth}{@{}X|}
This is the first level of the nested tables. It roughly goes to where the margin is, which is good.\\
{\begin{tabularx}{\textwidth}{@{}X|}
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around.
{\begin{tabularx}{\textwidth}{@{}X|}
And here is the third level. As can be seen, the problem gets worse and worse with each level.
\end{tabularx}}
\end{tabularx}}
\end{tabularx}

\noindent
This is the first level of the nested tables. It roughly goes to where the margin is, which is good.
This is where the second level of the nested tables is. Unfortunately, it goes too far'' before the text wraps around.

99 times out of a hundred if you find yourself using nested tabularx then the material should not be in a table at all as it is not tabular but rather an indented textual layout, in which case it is almost always better to use a list.