為什麼第三列這麼寬?

為什麼第三列這麼寬?
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage{caption}



\begin{document}


\begin{center}
\begin{tabularx}{100pt}{c|c|c|}
\multicolumn{1}{c}{ } &\multicolumn{1}{c}{$r$} &\multicolumn{1}{c}{$b$} \\ \cline{2-3}
$r$ & $\frac{4}{9}$ & $\frac{2}{9}$  \\ \cline{2-3}
$b$ & $\frac{2}{9}$ & $\frac{1}{9}$   \\ \cline{2-3} \multicolumn{3}{c}{2 red 1 blue}
\end{tabularx}\hskip 1.7cm 
\begin{tabularx}{100pt}{c|c|c|}
\multicolumn{1}{c}{ } &\multicolumn{1}{c}{$r$} &\multicolumn{1}{c}{$b$} \\ \cline{2-3}
$r$ & $\frac{1}{9}$ & $\frac{2}{9}$  \\ \cline{2-3}
$b$ & $\frac{2}{9}$ & $\frac{4}{9}$   \\ \cline{2-3} \multicolumn{3}{c}{2 blue 1 red}
\end{tabularx}
\end{center}

\end{document}

我的意圖是將“2 red 1 blue”放在子圖下方和子圖中間。但為什麼第三列這麼寬呢?

答案1

正如我在評論中所說,表中至少有一列tabularx必須是X類型。這意味著,在您的案例中,表應該是:

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\NewExpandableDocumentCommand\mcc{O{1}m}%
                                 {\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{center}
\begin{tabularx}{100pt}{c | C|C|}
\mcc{}  & \mcc{$r$}     & \mcc{$b$}     \\
    \cline{2-3}
$r$     & $\frac{4}{9}$ & $\frac{2}{9}$ \\
    \cline{2-3}
$b$     & $\frac{2}{9}$ & $\frac{1}{9}$ \\
    \cline{2-3}
\mcc[3]{2 red 1 blue}
\end{tabularx}
    \hskip 21mm
\begin{tabularx}{100pt}{C | C|C|}
\mcc{}  & \mcc{$r$}     & \mcc{$b$}     \\
    \cline{2-3}
$r$     & $\frac{4}{9}$ & $\frac{2}{9}$ \\
    \cline{2-3}
$b$     & $\frac{2}{9}$ & $\frac{1}{9}$ \\
    \cline{2-3}
\mcc[3]{2 red 1 blue}
\end{tabularx}
    \end{center}
\end{document}

在此輸入影像描述

如您所見,在第一個表中,第二列和第三列的寬度相等,但第一個列較窄,在第二個表中,所有列的寬度都相等。

clr寬度為列中單元格內容的最寬寬度。相反,tabularx您必須指定表格寬度,且表格中至少一列寬度必須由 LaTeX 計算。此功能具有 X從中派生的列(用於居中、左對齊或右對齊單元格內容)。如果未啟用此功能,通常\hline\cline突出到桌子之外(所以您很幸運,這不會發生在您的情況下)。

但是,當\multicolumn單元格的內容比跨列的自然寬度總和寬時,最後一列的寬度會增加\multicolumn單元格寬度決定的量。因此,您應該注意不要發生這種情況(定義更大的跨度列寬度)。

tabularray在我看來,如果您使用包並在最後兩列下方居中 ˙\multicolum` 單元格,您的表格會更好:

\documentclass{article}
\usepackage{tabularray}

\begin{document}
    \begin{center}
\begin{tblr}{hline{2-Y}={2-Z}{solid},
             vline{2-Z}={2-Y}{solid}, 
             colspec={c *{2}{Q[c,wd=2em,mode=math]}},  
             row{Z} = {mode=text},
             rowsep=3pt
             }
    &       r       &   b           \\ 
r   & \frac{4}{9}   & \frac{2}{9}   \\ 
b   & \frac{2}{9}   & \frac{1}{9}   \\ 
    & \SetCell[c=2]{c}    2 red 1 blue 
\end{tblr}
    \hskip 21mm
\begin{tblr}{hline{2-Y}={2-Z}{solid},
             vline{2-Z}={2-Y}{solid},
             colspec={c *{2}{Q[c,wd=2em,mode=math]}},
             row{Z} = {mode=text},
             rowsep=3pt
             }
    &       r       &   b           \\
r   & \frac{4}{9}   & \frac{2}{9}   \\
b   & \frac{2}{9}   & \frac{1}{9}   \\
    & \SetCell[c=2]{c}  2 red 1 blue
\end{tblr}
    \end{center}
\end{document}

在此輸入影像描述

附錄: 看來到目前為止 Overleaf 仍然沒有刷新他們的套件儲存庫。希望這能在附近的功能中發揮作用。直到您可以透過使用arraymakecell包來使用“經典”方法:

\documentclass{article}
\usepackage{array, makecell}
\NewExpandableDocumentCommand\mcc{O{1}m}%
                                 {\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{center}
    \setcellgapes{3pt}
    \makegapedcells
\begin{tabular}{c | *{2}{>{$}w{c}{2em}<{$} |}}
\mcc{}  & \mcc{$r$}     & \mcc{$b$}     \\
    \cline{2-3}
r       & \frac{4}{9}   & \frac{2}{9}   \\
    \cline{2-3}
b       & \frac{2}{9}   & \frac{1}{9}   \\
    \cline{2-3}
\mcc{}  & \mcc[2]{2 red 1 blue}
\end{tabular}
    \hskip 21mm
\begin{tabular}{c | *{2}{>{$}w{c}{2em}<{$} |}}
\mcc{}  & \mcc{$r$}     & \mcc{$b$}     \\
    \cline{2-3}
r       & \frac{4}{9}   & \frac{2}{9}   \\
    \cline{2-3}
b       & \frac{2}{9}   & \frac{1}{9}   \\
    \cline{2-3}
\mcc[3]{2 red 1 blue}
\end{tabular}
    \end{center}
\end{document}

編譯結果與使用該tabularray套件時非常相似。

在此輸入影像描述

為了進行風格比較,這兩個表略有不同。第一個文字「2 red 1 blue」位於第二列和第三列下方,第二個文字位於所有三列下方。

答案2

前面的評論:由於範例表中的任何列似乎都不需要在單元格內自動換行,因此我認為使用環境不是一個好主意tabularx

你問,

但為什麼第三列這麼寬呢?

首先是一些術語:

  • lc、 或類型的列的總寬度是 (a) 列中最寬單元格的「自然」寬度和 (b) (或如果正在使用環境)r的總和。這些參數決定LaTeX 在列邊緣插入的空白填充量,取決於是否使用-like 或環境。2\tabcolsep2\arraycolseparray\tabcolsep\arraycolseptabulararray

  • 多列tabulararray環境的總寬度幾乎按照定義給出,作為各個總列寬度的總和加上任何垂直規則(如果存在)的組合寬度。

您的tabular環境也包含以下 3 列條目:

\multicolumn{3}{c}{2 red 1 blue}

正如您「發現」的,此項決定了環境的整體或總寬度tabular。由於其寬度超過了三個單獨列寬度的總和,因此必須做出一些調整。 LaTeX 選擇的解決方案是將多餘的寬度分配給最後一列。雖然這種選擇看起來可能是任意的,但任何其他解決方案(例如將多餘的寬度等份分配給 3 列中的每一列)都同樣是任意的。

我認為您希望第 2 列和第 3 列的寬度相等。有兩種選擇可讓您實現目標:(a) 增加 的值\tabcolsep(或\arraycolsep,如果array正在使用環境),和/或 (b) 增加列寬使其超出其「自然」寬度。下面的螢幕截圖說明了這兩種可能性,其中包含三行。

  • 最上面一行說明了您的初始設定。 (為了減少程式碼混亂並擺脫大量$符號,我已經切換tabulararray環境)。顯然,第 3 列比第 2 列寬得多。

  • \arraycolsep中間行說明了在保持基本列類型的同時增加 的值的效果c。將 5pt 加到預設值\arraycolsep(也剛好是 5pt)即可完成工作。 (旁白:這種方法“有效”,因為第 2 列和第 3 列的自然寬度是由\frac恰好寬度相等的項決定的。)

  • 底行說明了為第 2 列和第 3 列設定固定(可用)寬度 14pt 的效果(透過使用套件w提供的列類型array),同時使用預設值\arraycolsep。只要將列wc(其中c代表「中心」)的寬度選擇為「足夠大」(這裡,14pt 似乎是正確的),您最終會得到一個第二列和第三列寬度相等的表格。

一種解決方案總是比另一種解決方案更好嗎?一般來說,沒有。不過,對於目前的具體情況,請注意第一個解決方案方法最終也會增加第 1 列的總寬度,而第二個解決方案方法則不然。如果第一列的最大緊湊性對您很重要,您可能會喜歡使用第二種解決方法。

在此輸入影像描述

\documentclass{article}
\usepackage{array} % for 'w' column type and \extrarowheight macro
\setlength\extrarowheight{2pt}
\newcommand\mc[1]{\multicolumn{1}{c}{#1}} % handy shortcut macro

\begin{document}

Initial setup
\[
\begin{array}{ c | c | c | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{4}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{1}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 red 1 blue}
\end{array}
\hskip 1.7cm 
\begin{array}{ c | c | c | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{1}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{4}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 blue 1 red}
\end{array}
\]

\bigskip
Solution 1: Increase \texttt{\string\arraycolsep} or \texttt{\string\tabcolsep}
\[
\addtolength{\arraycolsep}{5pt} % default value: 5pt
\begin{array}{ c | c | c | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{4}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{1}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 red 1 blue}
\end{array}
\hskip 1.7cm 
\begin{array}{ c | c | c | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{1}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{4}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 blue 1 red}
\end{array}
\]

\bigskip
Solution 2: Increase usable column widths
\[
\begin{array}{ c |wc{14pt} | wc{14pt} | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{4}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{1}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 red 1 blue}
\end{array}
\hskip 1.7cm 
\begin{array}{ c |wc{14pt} | wc{14pt} | }
\mc{} & \mc{r} & \mc{b} \\ \cline{2-3}
r & \frac{1}{9} & \frac{2}{9}   \\ \cline{2-3}
b & \frac{2}{9} & \frac{4}{9}   \\ \cline{2-3} 
\multicolumn{3}{>{$}c<{$}}{2 blue 1 red}
\end{array}
\]
\end{document}

相關內容