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?