一周前,我决定用 LaTeX 写我的硕士论文。我从零开始。我通过你和这个论坛学到了一切。请包容我,不要嘲笑我的代码 :-) 所以,我正在尝试创建一个包含 3 个子表的表。我的问题是:
- 我的第一个子表的最后一列太长了。我认为这是因为最后一行的句子太长了。我找不到改变其宽度的方法。
- 我不知道在 tabular、tabularx、tabular* 之间使用什么来改变这个宽度。我尝试了 tabularx 和 sub caption 的不同方法,但都不起作用。
- 我希望 3 个子表有相同的宽度。
- 我想将 a)、b)、c) 放在左侧并与“固定效果”对齐,而不是放在每个子表上方并居中。
这是我的代码:
\usepackage{graphicx}
\usepackage[french,english]{babel}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{subcaption}
\begin{document}
\renewcommand{\thetable}{\arabic{table}}
\begin{table}[]
\caption{A first very long sentence. a) Name of my first subtable. b) Name of my second subtable. c) Name of my third subtable.}
\scriptsize
\centering
\begin{subtable}{0.9\textwidth}
\centering
\subcaption{}
\begin{tabular}{l c c c c}
\cline{1-5}
Fixed effects & \multicolumn{1}{c}{Estimate $\pm$ SE} & \multicolumn{2}{c}{Z} & \multicolumn{1}{r}{P} \\
\cline{1-5}
(Intercept) & \multicolumn{1}{c}{-0.27 $\pm$ 0.13} & \multicolumn{2}{c}{-2.10} & \multicolumn{1}{r}{0.036} \\
Trial & \multicolumn{1}{c}{0.00 $\pm$ 0.05} & \multicolumn{2}{c}{0.04} & \multicolumn{1}{r}{0.965} \\
\cline{1-5}
Random effects & \multicolumn{1}{c}{Variance $\pm$ SD} & \multicolumn{1}{c}{LRT} & \multicolumn{1}{c}{d.f.} & \multicolumn{1}{r}{P} \\
\cline{1-5}
Bird (intercept) & \multicolumn{1}{c}{0.55 $\pm$ 0.74} & \multicolumn{1}{r}{159} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{< 0.001} \\
Bird (slope with trial) & \multicolumn{1}{c}{0.02 $\pm$ 0.16} & \multicolumn{1}{r}{1.40} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{0.237} \\
Flock (intercept) & \multicolumn{1}{c}{0.00 $\pm$ 0.00} & \multicolumn{1}{r}{0} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{1} \\
\cline{1-5}
\multicolumn{5}{l}{\multirow{2}{\linewidth}{N = 454. The response variable is the proportion of scrounged patches, expressed as a 2-column matrix composed of the number of scrounged patches and number of produced patches.}} \\
\multicolumn{5}{l}{} \\
\end{tabular}
\label{tbl:sub-table-a}
\end{subtable}
\begin{subtable}{0.9\textwidth}
\centering
\subcaption{}
\begin{tabular}{l c c c c}
\cline{1-5}
Fixed effects & \multicolumn{1}{c}{Estimate $\pm$ SE} & \multicolumn{2}{c}{Z} & \multicolumn{1}{r}{P} \\
\cline{1-5}
(Intercept) & \multicolumn{1}{c}{-0.26 $\pm$ 0.19} & \multicolumn{2}{c}{-1.39} & \multicolumn{1}{r}{0.166} \\
Trial & \multicolumn{1}{c}{0.04 $\pm$ 0.09} & \multicolumn{2}{c}{0.44} & \multicolumn{1}{r}{0.659} \\
\cline{1-5}
Random effects & \multicolumn{1}{c}{Variance $\pm$ SD} & \multicolumn{1}{c}{LRT} & \multicolumn{1}{c}{d.f.} & \multicolumn{1}{r}{P} \\
\cline{1-5}
Bird (intercept) & \multicolumn{1}{c}{0.60 $\pm$ 0.77} & \multicolumn{1}{r}{81.2} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{< 0.001} \\
Bird (slope with trial) & \multicolumn{1}{c}{0.05 $\pm$ 0.22} & \multicolumn{1}{r}{1.68} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{0.194} \\
Flock (intercept) & \multicolumn{1}{c}{0.00 $\pm$ 0.00} & \multicolumn{1}{r}{0} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{1} \\
\cline{1-5}
\multicolumn{5}{l}{N = 234.} \\
\end{tabular}
\label{tbl:sub-table-b}
\end{subtable}
\begin{subtable}{0.9\textwidth}
\centering
\subcaption{}
\begin{tabular}{l c c c c}
\cline{1-5}
Fixed effects & \multicolumn{1}{c}{Estimate $\pm$ SE} & \multicolumn{2}{c}{Z} & \multicolumn{1}{r}{P} \\
\cline{1-5}
(Intercept) & \multicolumn{1}{c}{-0.27 $\pm$ 0.17} & \multicolumn{2}{c}{-1.57} & \multicolumn{1}{r}{0.117} \\
Trial & \multicolumn{1}{c}{-0.03 $\pm$ 0.07} & \multicolumn{2}{c}{-0.42} & \multicolumn{1}{r}{0.674} \\
\cline{1-5}
Random effects & \multicolumn{1}{c}{Variance $\pm$ SD} & \multicolumn{1}{c}{LRT} & \multicolumn{1}{c}{d.f.} & \multicolumn{1}{r}{P} \\
\cline{1-5}
Bird (intercept) & \multicolumn{1}{c}{0.50 $\pm$ 0.71} & \multicolumn{1}{r}{77.5} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{< 0.001} \\
Bird (slope with trial) & \multicolumn{1}{c}{0.01 $\pm$ 0.10} & \multicolumn{1}{r}{0.17} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{0.682} \\
Flock (intercept) & \multicolumn{1}{c}{0.00 $\pm$ 0.00} & \multicolumn{1}{r}{0} & \multicolumn{1}{c}{1} & \multicolumn{1}{r}{1} \\
\cline{1-5}
\multicolumn{5}{l}{N = 220.} \\
\end{tabular}
\label{tbl:sub-table-c}
\end{subtable}
\label{tab:scrounging}
\end{table}
\end{document}
答案1
以下是一些建议(无特定顺序):
使用包中的线条绘制宏
booktabs
——\toprule
、\midrule
等——而不是\hline
和\cline
来获得间距适当的水平线。使用表格
\scriptsize
必然会让读者眯起眼睛,并可能想跳到其他内容。\footnotesize
如果您希望读者花一些时间阅读表格内容,请不要往下看。0.7\textwidth
对三个环境分别使用一个共同的宽度 casubtable
,并确保三个环境具有相同的宽度;这可以通过对每个环境中的最后一行tabular
使用指令来实现。p{\textwidth}
tabular
\multicolumn
通过为 4 个数据列设置“智能”的、具体来说就是自动数学模式列,可以节省环境主体中的大量输入工作(所有这些指令肯定已经过时了……)。在下面的序言中,查看和列类型的定义tabular
方式。C
R
要使环境的标题左对齐
subtable
,请运行如下指令\captionsetup[subtable]{singlelinecheck=false,justification=raggedright}
请使用与嵌入式环境相同的字体大小来显示标题
tabular
。除非您喜欢进行(代码)寻宝游戏,否则请将
\caption
和相关\label
指令放在一起。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage{amsmath,multirow}
\usepackage{booktabs,array,subcaption}
\newcolumntype{C}{>{$}c<{$}}
\newcolumntype{R}{>{$}r<{$}}
\begin{document}
\renewcommand{\thetable}{\arabic{table}}
\begin{table}
\footnotesize
\captionsetup[subtable]{size=footnotesize,
singlelinecheck=false,
justification=raggedright}
\caption{A first very long sentence. a) Name of my first subtable.
b) Name of my second subtable.
c) Name of my third subtable.}
\label{tab:scrounging}
\centering % a single \centering instruction suffices
\begin{subtable}{0.7\textwidth}
\caption{} \label{tbl:sub-table-a}
\begin{tabular}{@{}lCRCR@{}}
\toprule
Fixed effects & \text{Estimate} \pm \text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.27 \pm 0.13 & \multicolumn{2}{C}{-2.10} & 0.036 \\
Trial & \phantom{-}0.00 \pm 0.05 & \multicolumn{2}{C}{\phantom{-}0.04} & 0.965 \\
\midrule
Random effects & \text{Variance} \pm \text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.55 \pm 0.74 & 159 & 1 & < 0.001 \\
Bird (slope with trial) & 0.02 \pm 0.16 & 1.40 & 1 & 0.237 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
\multicolumn{5}{@{}p{\textwidth}@{}}{$N = 454$. The response variable is
the proportion of scrounged patches, expressed as a 2-column matrix
composed of the number of scrounged patches and number of produced patches.} \\
\end{tabular}
\end{subtable}
\bigskip % insert some space between the subtables
\begin{subtable}{0.7\textwidth}
\subcaption{} \label{tbl:sub-table-b}
\begin{tabular}{@{}lCRCR@{}}
\toprule
Fixed effects & \text{Estimate} \pm \text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.26 \pm 0.19 & \multicolumn{2}{C}{-1.39} & 0.166 \\
Trial & \phantom{-}0.04 \pm 0.09 & \multicolumn{2}{C}{\phantom{-}0.44} & 0.659 \\
\midrule
Random effects & \text{Variance} \pm \text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.60 \pm 0.77 & 81.2 & 1 & < 0.001 \\
Bird (slope with trial) & 0.05 \pm 0.22 & 1.68 & 1 & 0.194 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
\multicolumn{5}{@{}p{\textwidth}@{}}{$N = 234$.} \\
\end{tabular}
\end{subtable}
\bigskip
\begin{subtable}{0.7\textwidth}
\subcaption{} \label{tbl:sub-table-c}
\begin{tabular}{@{}lCRCR@{}}
\toprule
Fixed effects & \text{Estimate}\pm\text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.27 \pm 0.17 & \multicolumn{2}{C}{-1.57} & 0.117 \\
Trial & -0.03 \pm 0.07 & \multicolumn{2}{C}{-0.42} & 0.674 \\
\midrule
Random effects & \text{Variance}\pm\text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.50 \pm 0.71 & 77.5 & 1 & < 0.001 \\
Bird (slope with trial) & 0.01 \pm 0.10 & 0.17 & 1 & 0.682 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
\multicolumn{5}{@{}p{\textwidth}@{}}{$N = 220$.} \\
\end{tabular}
\end{subtable}
\end{table}
\end{document}
附录回答 OP 的第一个后续问题:如果您想让列之间的“间隙”宽度相等,我建议您使用tabular*
环境,如下面的代码所示。这个看起来有些笨拙的指令@{\extracolsep{\fill}}
指示 LaTeX 增加列间空白以填满可用空间。
请注意,我还建议将通用图例移至子表上方的位置。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[french,english]{babel}
\usepackage{amsmath,booktabs,array,subcaption}
\newcolumntype{C}{>{$}c<{$}}
\newcolumntype{R}{>{$}r<{$}}
\begin{document}
\begin{table}
\captionsetup[subtable]{size=footnotesize,
skip=0.25\baselineskip,
singlelinecheck=false,
justification=raggedright}
\caption{A first very long sentence.
a) Name of my first subtable.
b) Name of my second subtable.
c) Name of my third subtable.}
\label{tab:scrounging}
\footnotesize
\bigskip
In the following tables, the response variable is the proportion of scrounged patches, expressed as a 2-column matrix composed of the number of scrounged patches and number of produced patches.
\centering % a single \centering instruction suffices
\setlength\tabcolsep{0pt} % let LaTeX figure out the intercolumn spacing
\bigskip
\begin{subtable}{0.7\textwidth}
\caption{} \label{tbl:sub-table-a}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lCRCR}
\toprule
Fixed effects & \text{Estimate} \pm \text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.27 \pm 0.13 & \multicolumn{2}{C}{-2.10} & 0.036 \\
Trial & \phantom{-}0.00 \pm 0.05 & \multicolumn{2}{C}{\phantom{-}0.04} & 0.965 \\
\midrule
Random effects & \text{Variance} \pm \text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.55 \pm 0.74 & 159 & 1 & < 0.001 \\
Bird (slope with trial) & 0.02 \pm 0.16 & 1.40 & 1 & 0.237 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
$N = 454$. \\
\end{tabular*}
\end{subtable}
\bigskip % insert some space between the subtables
\begin{subtable}{0.7\textwidth}
\subcaption{} \label{tbl:sub-table-b}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lCRCR}
\toprule
Fixed effects & \text{Estimate} \pm \text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.26 \pm 0.19 & \multicolumn{2}{C}{-1.39} & 0.166 \\
Trial & \phantom{-}0.04 \pm 0.09 & \multicolumn{2}{C}{\phantom{-}0.44} & 0.659 \\
\midrule
Random effects & \text{Variance} \pm \text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.60 \pm 0.77 & 81.2 & 1 & < 0.001 \\
Bird (slope with trial) & 0.05 \pm 0.22 & 1.68 & 1 & 0.194 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
$N = 234$. \\
\end{tabular*}
\end{subtable}
\bigskip
\begin{subtable}{0.7\textwidth}
\subcaption{} \label{tbl:sub-table-c}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lCRCR}
\toprule
Fixed effects & \text{Estimate}\pm\text{SE} & \multicolumn{2}{C}{Z} & P \\
\midrule
(Intercept) & -0.27 \pm 0.17 & \multicolumn{2}{C}{-1.57} & 0.117 \\
Trial & -0.03 \pm 0.07 & \multicolumn{2}{C}{-0.42} & 0.674 \\
\midrule
Random effects & \text{Variance}\pm\text{SD} & \text{LRT} & \text{d.f.} & P \\
\midrule
Bird (intercept) & 0.50 \pm 0.71 & 77.5 & 1 & < 0.001 \\
Bird (slope with trial) & 0.01 \pm 0.10 & 0.17 & 1 & 0.682 \\
Flock (intercept) & 0.00 \pm 0.00 & 0 & 1 & 1 \\
\midrule
$N = 220$. \\
\end{tabular*}
\end{subtable}
\end{table}
\end{document}
答案2
tabularx
和 的解决方案siunitx
:
\documentclass{book}
\usepackage[utf8]{inputenc}%
\usepackage[T1]{fontenc}%
\usepackage{lmodern}
\usepackage[french,english]{babel}
\usepackage{booktabs}
\usepackage{tabularx, siunitx}
\sisetup{table-number-alignment=center}
\usepackage{caption, subcaption}
\captionsetup{font=small}
\captionsetup[subtable]{singlelinecheck=off, font=footnotesize}
\begin{document}
\begin{table}[!htbp]
\caption{A first very long sentence. a) Name of my first subtable. b) Name of my second subtable. c) Name of my third subtable.}
\footnotesize\setlength{\tabcolsep}{3pt}
\centering
\begin{subtable}{0.65\textwidth}
\subcaption{}\label{tbl:sub-table-a}
\begin{tabularx}{\linewidth}{X S[table-format=-1.2, separate-uncertainty, table-figures-uncertainty=2] c c S[table-format=1.3, table-comparator]}
\toprule
Fixed effects & {Estimate $\pm$ SE} & \multicolumn{2}{c}{Z} & {P} \\
\midrule %
(Intercept) & -0.27(13) & \multicolumn{2}{c}{$-2.10 $} & 0.036 \\%
Trial & 0.00(05) & \multicolumn{2}{c}{$\phantom{-} 0.04 $} & 0.965 \\
\midrule
Random effects & {Variance $\pm$ SD} & LRT & d.f. & {P} \\
\midrule
Bird (intercept) & 0.55(74) & \multicolumn{1}{r}{159} & 1 & {}< 0.001 \\%
Bird (slope with trial) & 0.02(16) & \multicolumn{1}{r}{1.40} & 1 & 0.237 \\%
Flock (intercept) & 0.00(0) & \multicolumn{1}{r}{0} & 1 & {1} \\
\bottomrule
\addlinespace[1ex]
\multicolumn{5}{@{}p{\linewidth}@{}}{N = 454. The response variable is the proportion of scrounged patches, expressed as a 2-column matrix composed of the number of scrounged patches and number of produced patches.\medskip}
\end{tabularx}
\end{subtable}
\begin{subtable}{0.65\textwidth}
\subcaption{}\label{tbl:sub-table-b}
\begin{tabularx}{\linewidth}{X S[table-format=-1.2, separate-uncertainty, table-figures-uncertainty=2] c c S[table-format=1.3, table-comparator]}
\toprule
Fixed effects & {Estimate $\pm$ SE} & \multicolumn{2}{|c|}{Z} & {P} \\
\midrule %
(Intercept) & -0.26(19) & \multicolumn{2}{c}{$ -1.39 $} & 0.166 \\%
Trial & 0.00(05) & \multicolumn{2}{c}{$\phantom{-} 0.44 $} & 0.659 \\
\midrule
Random effects & {Variance $\pm$ SD} & LRT & d.f. & {P} \\
\midrule
Bird (intercept) & 0.60(77) & \multicolumn{1}{r}{81.2} & 1 & < 0.001 \\%
Bird (slope with trial) & 0.05(22) & \multicolumn{1}{r}{1.68} & 1 & 0.194 \\%
Flock (intercept) & 0.00(0) & \multicolumn{1}{r}{0} & 1 & {1} \\
\bottomrule
\addlinespace[1ex]
\multicolumn{5}{@{}p{\linewidth}@{}}{N =234.\medskip}
\end{tabularx}
\end{subtable}
\begin{subtable}{0.65\textwidth}
\subcaption{}\label{tbl:sub-table-c}
\begin{tabularx}{\linewidth}{X S[table-format=-1.2, separate-uncertainty, table-figures-uncertainty=2] c c S[table-format=1.3, table-comparator]}
\toprule
Fixed effects & {Estimate $\pm$ SE} & \multicolumn{2}{|c|}{Z} & {P} \\
\midrule %
(Intercept) & -0.27(17) & \multicolumn{2}{c}{$ -1.57 $} & 0.117 \\%
Trial & -0.03(07) & \multicolumn{2}{c}{$ -0.42 $} & 0.674 \\
\midrule
Random effects & {Variance $\pm$ SD} & LRT & d.f. & {P} \\
\midrule
Bird (intercept) & 0.50(71) & \multicolumn{1}{r}{77.5} & 1 & < 0.001 \\%
Bird (slope with trial) & 0.01(10) & \multicolumn{1}{r}{0.17} & 1 & 0.682 \\%
Flock (intercept) & 0.00(0) & \multicolumn{1}{r}{0} & 1 & {1} \\
\bottomrule
\addlinespace[1ex]
\multicolumn{5}{@{}p{\linewidth}@{}}{N =220.}
\end{tabularx}
\end{subtable}
\end{table}
\end{document}
答案3
编辑:
您说对了一部分 :) 最后multicolum
一行太宽了。它定义为linewidth
,宽度定义为subtable
。如果您将其缩短到 77 毫米,表格将如您所愿显示。
关于subcaption
位置,我理解,您希望将它们放在表格左侧,与第一行对齐。这可以通过使用floatrow
和使用subfig
包而不是来实现subcaption
。
我现在还利用booktabs
水平规则的存在并添加makecell
用于使用其宏定义列头thead
。
为了使所有子表的宽度相等,使用具有规定宽度的表格环境是明智的,因为它们是tabularx
和tabular*
。使用`tabular更简单。对于它们的宽度,我建议定义新的长度(参见下面的MWE),这样您就可以在一个地方设置所有子表的宽度。
在估计合适的表格宽度时,我借助了showframe
显示页面布局的包。当然,在最终文档中,这个包必须从序言中删除。
离题:multicolumn
您的表格没有必要那么复杂。我删除了除跨两列的表格之外的 所有表格。此外,字体大小的表格\script
难以阅读,因此我建议使用较大的字体。
完整的、仅包含第一个表的新 MWE 是:
\documentclass{book}
\usepackage[french,english]{babel}
\usepackage{booktabs, makecell, tabularx}% <--- added makecell
\renewcommand\theadfont{\footnotesize}
\newcommand\mc[2]{\multicolumn{#1}{c}{#2}}
\newcommand\mr[1]{\multicolumn{1}{r}{#1}}
\newlength\tabwidth
\usepackage{floatrow}% <-- added for side position of subcaptions
\floatsetup[table]{style=plain,subcapbesideposition=top,capposition=top}
\usepackage{caption}
\usepackage[label font=bf,labelformat=parens]{subfig}
\usepackage{calc}% <-- added for calculation of cell width
\usepackage{showframe}
\renewcommand*\ShowFrameColor{\color{red}}
\begin{document}
\begin{table}[]
\caption{A first very long sentence. a) Name of my first subtable. b) Name of my second subtable. c) Name of my third subtable.}
\label{tbl:sub-table-a}
\centering
\setlength\tabwidth{90mm}
\footnotesize
\sidesubfloat[]{
\begin{tabularx}{\tabwidth}{X c c c r}
\toprule
\thead{Fixed effects}
& \thead{Estimate $\pm$ SE}
& \mc{2}{\thead{Z}} & \thead{P} \\
\midrule
(Intercept) & -0.27 $\pm$ 0.13 & \mc{2}{-2.10} & 0.036 \\
Trial & 0.00 $\pm$ 0.05 & \mc{2}{0.04} & 0.965 \\
\midrule
\thead{Random effects}
& \thead{Variance $\pm$ SD}
& \thead{LRT} & \thead{d.f.}
& \thead{P} \\
\midrule
Bird (intercept)& 0.55 $\pm$ 0.74 & \mr{159} & 1 & $<$ 0.001 \\
Bird (slope with trial)
& 0.02 $\pm$ 0.16 & \mr{1.40} & 1 & 0.237 \\
Flock (intercept)
& 0.00 $\pm$ 0.00 & \mr{0} & 1 & 1 \\
\midrule[1pt]
\multicolumn{5}{p{\tabwidth-2\tabcolsep}}{N = 454. The response variable is the proportion of scrounged patches, expressed as a 2-column matrix composed of the number of scrounged patches and number of produced patches.}
\end{tabularx}
}
\end{table}
\end{document}