如何在边距所在的位置停止表格,而不是使用 textwidth 命令?

如何在边距所在的位置停止表格,而不是使用 textwidth 命令?

我在 tabularx 环境中嵌套表格时遇到了一些问题。基本上,我希望每个表格都停在边距处。通常,我会使用\textwidth宽度设置来实现这一点,但是,当我嵌套表格时,由于每个 tabularx 都会在表格前后添加空白(我不想直接删除空白),因此文本会“溢出”。这是一个 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}

基本上,我想要一些可以告诉 tabularx 在边距处换行的东西。有人知道怎么做吗?

编辑:解决了最初的问题(感谢 Werner!)但后来意识到使用 \linewidth 没有考虑到在 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}

有人知道怎么修这个东西吗?

答案1

使用宽度\linewidth,它会根据线的宽度进行调整:

在此处输入图片描述

\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}

作为参考,请参阅\textwidth\linewidth之间的区别\hsize

也许更符合“在边缘换行”的可能是用作{X@{}}两个内部的列说明符tabularx

在此处输入图片描述

%...
\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}
%...

答案2

表格行的\tabcolsep开头始终有一个。也可以更改左边距,而不使用表格:

\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.
\par\noindent\advance\leftskip by 1em
This is where the second level of the nested tables is. Unfortunately, it goes ``too far'' before the text wraps around.
\par\noindent\advance\leftskip by 1em
And here is the third level. As can be seen, the problem gets worse and worse with each level.

\noindent\advance\leftskip by -2em%% the old value
foo

\end{document}

在此处输入图片描述

答案3

如果你发现自己使用嵌套,那么百分之九十九的情况是,tabularx材料根本不应该放在表格中,因为它不是表格,而是缩进的文本布局,在这种情况下,使用列表几乎总是更好。

相关内容