表格中的标题有两行

表格中的标题有两行

以下代码LaTeX创建了一个表格。在标题中,我希望第一列显示“结束”除以“第 k 周”,第二列显示“总数量”除以“已售副本数”。

    \documentclass{amsart}

    \usepackage{adjustbox}\usepackage{mathtools}
    \usepackage{array}\usepackage{makecell}\usepackage{stackengine}\setstackEOL{\cr} %EOL is abbreviation for "end of line."

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{|| c | c||} \hline
End of the \textit{k}\textsuperscript{th} week  &   Total Number of Copies Sold \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

答案1

\thead使用来自 的命令很容易makecell。我借此机会改进了您的表格,\hhline并更改了 的值stackdap,因为标题与第一个 太接近了\hline

\documentclass{amsart}

\usepackage{adjustbox}\usepackage{mathtools}
\usepackage{hhline}
\usepackage{array}\usepackage{makecell}
\usepackage{stackengine}\setstackEOL{\cr}%EOL is abbreviation for "end of line."
\setstackgap{S}{2ex}
\setcellgapes{3pt}
\begin{document}

\stackon{\makegapedcells%
    \begin{tabular}{|| c | c||}
        \hline
        \thead{End of the & \\\textit{k}\textsuperscript{th} week} & \thead{Total Number of\\ Copies Sold} \\ \Xhline{0.8pt}
        $ k=1 $ & 3200 \\ \hhline{||--||}
        $ k=2 $ & 5500 \\ \hhline{||--||}
        $ k=3 $ & 6800 \\ \hhline{||--||}
        $ k=4 $ & 7400 \\ \hhline{||--||}
        $ k=5 $ & 7700 \\ \hline
    \end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document} 

在此处输入图片描述

答案2

你介意这样的事情吗:

在此处输入图片描述

代码:

    \documentclass{article}

    \usepackage{adjustbox}\usepackage{mathtools}
    \usepackage{array}
    \usepackage{makecell}
    \usepackage{stackengine}
\setstackEOL{\cr} %EOL is abbreviation for "end of line."

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{|| c | c||} \hline
\thead{End of the\\ \textit{k}\textsuperscript{th} week}
    &   \thead{Total Number of\\ Copies Sold} \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

很多人有观点认为,如果没有垂直线,桌子会更漂亮:

    \documentclass{article}

    \usepackage{adjustbox}\usepackage{mathtools}
    \usepackage{array}
    \usepackage{makecell,booktabs}
    \usepackage{stackengine}
\setstackEOL{\cr} %EOL is abbreviation for "end of line."

    \begin{document}

    \setlength\extrarowheight{2pt}\stackon{%
\begin{tabular}{c c} 
    \toprule
\thead{End of the\\ \textit{k}\textsuperscript{th} week}
    &   \thead{Total Number of\\ Copies Sold} \\ 
    \midrule
k=1 &   3200 \\ 
k=2 &   5500 \\ 
k=3 &   6800 \\ 
k=4 &   7400 \\ 
k=5 &   7700 \\ 
    \bottomrule
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

在此处输入图片描述

答案3

在这里,我使用堆栈拆分标题。OP 在评论中询问如何定义拆分标题的对齐方式。我将其显示为居中,但在代码中添加此行,它将定义拆分标题行的对齐方式:

\renewcommand\stackalignment{c}% <--- USE l,c, OR r TO SET SPLIT HEADER 

标题将根据数据居中,如 的参数所示{|c|c|}tabular但标题的各个行将根据上述设置对齐\stackalignment

完整的 MWE:

\documentclass{amsart}

\usepackage{adjustbox}\usepackage{mathtools}
\usepackage{array}
\usepackage{makecell}
\usepackage{stackengine}\setstackEOL{\cr} %EOL is abbreviation for "end of line."

\begin{document}
\setlength\extrarowheight{2pt}\stackon{%
\renewcommand\stackalignment{c}% <--- USE l,c, OR r TO SET SPLIT HEADER ALIGNMENT
\begin{tabular}{|| c | c||} \hline
\stackanchor{End of the}{\textit{k}\textsuperscript{th} week}  &  
\addstackgap{\stackanchor{Total Number of}{Copies Sold}} \\ \Xhline{0.8pt}
k=1 &   3200 \\ \hline
k=2 &   5500 \\ \hline
k=3 &   6800 \\ \hline
k=4 &   7400 \\ \hline
k=5 &   7700 \\ \hline
\end{tabular}
}
{\bfseries\Longstack{Sales of Book \textit{A}}}

\end{document}

在此处输入图片描述

相关内容