多列。分佈和水平線

多列。分佈和水平線

我得到了表格,為了簡潔起見,我將其簡化為以下內容,

\begin{tabular}{ |l|l|c c c|l|l|l| }
    \hline
    Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{ |c| }{Solution to $C_k$} & Current & Pursue & Stored\\
    & & $x_1$ & $x_2$ & $z$ & & & \\ \hline
\end{tabular}

產生以下輸出

在此輸入影像描述

現在,我有兩個問題,都與多列相關。首先,是否可以產生\hline僅影響第三列的部分(將「C_k 的解決方案」標籤和三個變數分開)?

其次,看起來 $x_1$、$x_2$ 和 $z$ 沒有居中。這要如何複述呢?

答案1

在此輸入影像描述

列寬的斑點不對稱c是由於\multicolumn{3}{c}{Solution to $C_k$}列寬大於跨越列的寬度總和而引起的。因此,包含 $z$ 的列被擴展以適合多列單元格的寬度。

要使所有c列的寬度相等,您必須指定它們的寬度,例如在列類型的幫助下p{<width>}

\documentclass{article}
\usepackage{array}% needed for column redefinition
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{tabular}{ |l|l|*{3}{C{2em}}|l|l|l| }
    \hline
Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{c|}{Solution to $C_k$}
    & Current & Pursue & Stored     \\
    \cline{3-5}
    & & $x_1$ & $x_2$ & $z$ & & & \\
    \hline
\end{tabular}
\end{document}

但是,我寧願按以下方式設計表格:

在此輸入影像描述

\documentclass{article}
\usepackage{array, booktabs}% needed for column redefinition and nicer looks
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}
\begin{tabular}{ ll *{3}{C{2em}} lll}
    \toprule
    &   & \multicolumn{3}{c}{Solution to $C_k$} &   &   &   \\
    \cmidrule(lr){3-5}
Problem $I_k$ & Solved $C_k$ & $x_1$ & $x_2$ & $z$ & Current & Pursue & Stored     \\
     \midrule
\end{tabular}
\end{document}

答案2

這是使用該tabularx套件使表格自動擴展到 textwidth 的變體,而無需手動指定列的寬度。

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{ |l|l|*3{>{\centering\arraybackslash}X}|l|l|l| }
    \hline
    Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{ c| }{Solution to $C_k$} & Current & Pursue & Stored\\\cline{3-5} 
    & & $x_1$ & $x_2$ & $z$ & & & \\ \hline
\end{tabularx}
\end{document} 

相關內容