在 tabularx 中使用 \multicolumn 時出現縮排和邊距問題

在 tabularx 中使用 \multicolumn 時出現縮排和邊距問題

我想使用 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 } \\

相關內容