我在 LaTeX 中创建表格时遇到了一些问题,真心希望大家能帮我解决。我的表格如下所示:
这是我的编码:
\documentclass[a4paper,12pt,oneside]{article}
\usepackage[english]{babel}
\usepackage{tikz}
\usepackage{pgf}
\usetikzlibrary{shapes,arrows,positioning,calc}
\usepackage{tabu}
\usepackage{hyperref}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow,tabularx,booktabs}
\begin{document}
\begin{center}
\begin{tabularx}{0.8\linewidth}{l !{\qquad} r X r X r X r}
\toprule
& \multicolumn{3}{c}{Name1} & & \multicolumn{3}{c}{Name2}
\\ \cmidrule{2-4} \cmidrule{6-8}
& Factor1 && Factor2 && Factor1 && Factor2\\
\midrule
Test 1 & 12 && 32 && 3 && 5 \\
Test 2 & 1234 && 234 && 334 && --- \\
\bottomrule
\end{tabularx}
\end{center}
\end{document}
如果可能的话,我希望我的表格看起来像这样(当然,名称 1 和名称 2 下面的行中仍应有一个空格):
进一步的问题:
- 如何添加“注释:”?
- 如何添加表名?
希望有人能帮助我。非常感谢。
答案1
我建议不要将表格宽度加到超出其自然宽度。tabular*
如果您确实想扩大列间距,则可以避免“幻像列”。
利用这里的设施,siunitx
可以轻松制作包含数字数据的表格。
\documentclass[a4paper]{article}
\usepackage{siunitx,booktabs}
\begin{document}
% natural width
\begin{table}[htp]
\centering
\caption{What is this table about?}\label{test1}
\medskip
\begin{tabular}{
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 & 12 & 32 & 3 & 5 & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\multicolumn{6}{@{}l@{}}{\strut\footnotesize Note: Blah blah}
\end{tabular}
\end{table}
% enlarged width
\begin{table}[htp]
\centering
\caption{What is this table about?}\label{test2}
\medskip
\begin{tabular*}{.8\textwidth}{
@{\extracolsep{\fill}}
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 & 12 & 32 & 3 & 5 & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\multicolumn{6}{@{}l@{}}{\strut\footnotesize Note: Blah blah}
\end{tabular*}
\end{table}
\end{document}
对于需要拆分成多行的较长的笔记,最好的方法是加载threeparttable
包。
\documentclass[a4paper]{article}
\usepackage{siunitx,booktabs,threeparttable}
% customize threeparttable
\renewcommand{\TPTnoteSettings}{\footnotesize}
\renewcommand{\TPTnoteLabel}[1]{#1}
\begin{document}
% natural width
\begin{table}[htp]
\centering
\begin{threeparttable}
\caption{What is this table about?}\label{test1}
\medskip
\begin{tabular}{
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 & 12 & 32 & 3 & 5 & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\end{tabular}
\begin{tablenotes}
\item[Note:] A long note that goes beyond the table width
because we want to have many many words that
mean as little as possible
\end{tablenotes}
\end{threeparttable}
\end{table}
% enlarged width
\begin{table}[htp]
\centering
\begin{threeparttable}
\caption{What is this table about?}\label{test2}
\medskip
\begin{tabular*}{.8\textwidth}{
@{\extracolsep{\fill}}
l
S[table-format=4.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=3.0]
S[table-format=2.0]
}
\toprule
& \multicolumn{2}{c}{Name1} & \multicolumn{2}{c}{Name2} \\
\cmidrule(lr){2-3} \cmidrule(lr){4-5}
& {Factor1} & {Factor2} & {Factor1} & {Factor2} & {Total} \\
\midrule
Test 1 & 12 & 32 & 3 & 5 & 99 \\
Test 2 & 1234 & 234 & 334 & {---} & 99 \\
\bottomrule
\end{tabular*}
\begin{tablenotes}
\item[Note:] A long note that goes beyond the table width
because we want to have many many words that
mean as little as possible
\end{tablenotes}
\end{threeparttable}
\end{table}
\end{document}