我想使用 tabularx 格式化某些内容,其中第一行有一列从左边距开始,另一列从右边距开始,并且接下来的行只有一列跨越整个文本宽度,允许在文本中换行。以下是我的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{multicol}
\usepackage{tabularx}
\begin{document}
\section{Introduction}
\begin{flushleft}
\begin{tabularx}{\textwidth}{@{}X @{\extracolsep{\fill}} r @{}}
Text beginning at left margin & Something at right margin \\
\hline
Here is some text that can get longer and longer until it causes a linebreak eventually & \\
\textit{Some closing text that would go to the next line soon because of the cell to the right} & \\
\end{tabularx}
\end{flushleft}
\begin{flushleft}
\begin{tabularx}{\textwidth}{@{}X @{\extracolsep{\fill}} r @{}}
Text beginning at left margin & Something at right margin \\
\hline
\multicolumn{2}{p{\textwidth}}{Here is some longer sentence that goes past the original column on the right } \\
\multicolumn{2}{p{\textwidth}}{\textit{Here is some text that can get longer and longer and longer and longer and longer until it causes a linebreak eventually}} \\
\end{tabularx}
\end{flushleft}
\end{document}
在下图中,顶部看起来符合预期,但是底部两行的列没有合并。当我尝试使用底部代码执行此操作时,我首先注意到第二行和第三行的缩进。这是来自多列命令吗?我如何修改缩进?此外,当使用 \textwidth 作为 p 列的宽度时,这些行中的文本超出了右页边距。如果我的长度等于纸张宽度减去页边距,它就可以正常工作。这是怎么回事?此外,我很高兴有其他格式建议。
答案1
您可以使用@{}
来删除表格单元格左侧和右侧的小(6 pt)水平空白,就像您在表格代码中所做的那样。由于该\multicolumn
命令覆盖了此设置,因此会重新添加填充,从而导致 ap 类型的列与\textwidth+2\tabcolsep
此一样宽,并且会延伸到右边距。要解决此问题,请按照以下示例所示添加@{}
到\multicolumn
命令中。我还删除了,@{\extracolsep{\fill}}
因为这里不需要此命令。
在以下屏幕截图中,红线表示页边距:
\documentclass{article}
%\usepackage[utf8]{inputenc} % default in an up to date installation
%\usepackage{multicol} % Not related to the \multicolumn command
\usepackage{tabularx}
\begin{document}
\section{Introduction}
\noindent
\begin{tabularx}{\textwidth}{@{} X r @{}}
Text beginning at left margin & Something at right margin \\
\hline
\multicolumn{2}{@{}p{\textwidth}@{}}{Here is some longer sentence that goes past the original column on the right } \\
\multicolumn{2}{@{}p{\textwidth}@{}}{\textit{Here is some text that can get longer and longer and longer and longer and longer until it causes a linebreak eventually}} \\
\end{tabularx}
\end{document}
答案2
从 - https://latex.org/forum/viewtopic.php?t=8352
\multicolumn{2}{@{}p{\textwidth}}{Here is some longer sentence that goes past the
original column on the right } \\