使用 tabularx 格式化表格

使用 tabularx 格式化表格

我正在尝试格式化下表:

\documentclass[12pt]{report}
\usepackage[left=35mm, right=25mm, top=30mm, bottom=30mm]{geometry}
\usepackage{tabularx}
\usepackage[flushleft]{threeparttable}  % package for tablenotes
\usepackage{booktabs,makecell,tabularx} % booktabs is used for horizontal lines (\toprule, \midrule, and \bottomrule)
\usepackage{array} % used together with tabularx
\usepackage{mathcomp} % for symbols

\begin{document}

\begin{table}[htbp]
\begin{threeparttable}
\caption{\linespread{1}\selectfont Fetal and neonatal complications in type 1 diabetic pregnancies and the general obstetric population}

\footnotesize
\linespread{1}\selectfont
\begin{tabularx}{\textwidth}{lcXXX}
 &  \multicolumn{2}{X}{Proportions (\% if not indicated otherwise)} & \multicolumn{2}{X}{OR (95\% Cl) for group differences} \tabularnewline
\cline{2-5} 
Outcome variable & Type 1 diabetes & Control & Crude & Adjusted \tabularnewline
\toprule
Stillbirth  & 1.5 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline
Fetal distress & 14 & 12.3 & g & h\tabularnewline
Perinatal mortality (\textperthousand) & 20 & j & k & l\tabularnewline
Neonatal mortality, 0--7 days (\textperthousand) & 5.1 & n & o & p\tabularnewline 
Neonatal mortality, 0--28 days (\textperthousand)  & 7.0 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Birth \textless37 weeks gestational age  & 21 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Birth \textless37 weeks gestational age  & 2.3 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
LGA  & 31 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline
SGA  & 2.3 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Major malformations  & 4.7 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Apgar score \textless7 at 5 min  & 3.1 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Apgar score \textless4 at 5 min  & 0.80 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Erb palsy*  & 2.1 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Respiratory distress syndrome  & 1.0 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline 
Respiratory disorders  & 9.5 & 0.3 & 4.04 (3.02--5.40)) & 3.34 (2.46--4.55)\tabularnewline
\bottomrule
\end{tabularx}

\label{tab:fetal}
\begin{tablenotes}
      \footnotesize
      \item{\linespread{1}\selectfont Data are proportions or OR (95\% Cl). Adjusted OR, OR adjusted for group differences in maternal age, BMI, parity, chronic hypertensive disorder, smoking habits, and ethnicity. *Vaginal deliveries only.}
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document}

但是,输出结果如下:

在此处输入图片描述

这就是我想要得到的:

在此处输入图片描述

您知道如何正确格式化该表格吗?

答案1

你需要

  • 增加两列标题的宽度

  • 调整 - 类型列的相对宽度X,使最后两列不需要换行。在下面的代码中,您可以看到四列的相对宽度总和L4(此类列的数量),两列的相对宽度总和C也为4

在此处输入图片描述

\documentclass[12pt]{report}
\usepackage[left=35mm, right=25mm, vmargin=30mm]{geometry}
\usepackage[flushleft]{threeparttable}
\usepackage{booktabs,makecell,tabularx,mathcomp}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[htbp]
\linespread{1}\selectfont
\begin{threeparttable}
\caption{Fetal and neonatal complications in type 1 diabetic 
pregnancies and the general obstetric population}
\label{tab:fetal}

\footnotesize
\begin{tabularx}{\textwidth}{@{} l
                           >{\hsize=1.15\hsize}L
                           >{\hsize=0.55\hsize}L
                      *{2}{>{\hsize=1.15\hsize}L} @{}} 
& \multicolumn{2}{>{\hsize=1.7\hsize}C}{Proportions (\% if not indicated otherwise)} 
& \multicolumn{2}{>{\hsize=2.3\hsize}C}{OR (95\% CI) for group differences} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
Outcome variable & Type 1 \mbox{diabetes} & Control & Crude & Adjusted \\
\midrule
Stillbirth & 1.5 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Fetal distress & 14 & 12.3 & g & h\\
Perinatal mortality (\textperthousand) & 20 & j & k & l\\
Neonatal mortality, 0--7 days (\textperthousand) & 5.1 & n & o & p\\
Neonatal mortality, 0--28 days (\textperthousand)  & 7.0 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Birth \textless{} 37 weeks gestational age  & 21 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Birth \textless{} 37 weeks gestational age  & 2.3 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
LGA  & 31 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
SGA  & 2.3 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Major malformations  & 4.7 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Apgar score \textless{} 7 at 5 min  & 3.1 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Apgar score \textless{} 4 at 5 min  & 0.80 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Erb palsy*  & 2.1 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Respiratory distress syndrome  & 1.0 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Respiratory disorders  & 9.5 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
\bottomrule
\end{tabularx}

\begin{tablenotes}
      \footnotesize
      \item{Data are proportions or OR (95\% CI). Adjusted OR, OR adjusted for group differences in maternal age, BMI, parity, chronic hypertensive disorder, smoking habits, and ethnicity. *Vaginal deliveries only.}
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document}

附录回答 OP 的后续问题:我认为将数字与其(隐式或显式)小数标记对齐的附加目标与表格的第 2 列和第 3 列有关。如下表所示,可以使用S数字的列类型,并使用(修改后的)C列作为两个标题单元格——分别是“1 型糖尿病”和“控制”。顺便注意,单元格条目“j”和“n”需要用花括号括起来,以防止 siunitx(错误)解释这些符号的含义。

在此处输入图片描述

\documentclass[12pt]{report}
\usepackage[left=35mm, right=25mm, vmargin=30mm]{geometry}
\usepackage[flushleft]{threeparttable}
\usepackage{tabularx,booktabs,mathcomp,siunitx
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{C}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[htbp]
\linespread{1}\selectfont
\begin{threeparttable}
\caption{Fetal and neonatal complications in type 1 diabetic 
pregnancies and the general obstetric population}
\label{tab:fetal}

\footnotesize
\begin{tabularx}{\textwidth}{@{} l
                      *{2}{S[table-format=2.2]}               
                      *{2}{>{\hsize=1.15\hsize}L} @{}} 
& \multicolumn{2}{>{\hsize=1.7\hsize}C}{Proportions (\% if not indicated otherwise)} 
& \multicolumn{2}{>{\hsize=2.3\hsize}C}{OR (95\% CI) for group differences} \\
\cmidrule(lr){2-3} \cmidrule(l){4-5}
Outcome variable 
& \multicolumn{1}{>{\hsize=1.15\hsize}C}{Type 1 diabetes} 
& \multicolumn{1}{>{\hsize=0.55\hsize}C}{Control}
& Crude & Adjusted \\
\midrule
Stillbirth & 1.5 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Fetal distress & 14 & 12.3 & g & h\\
Perinatal mortality (\textperthousand) & 20 & {j} & k & l\\
Neonatal mortality, 0--7 days (\textperthousand) & 5.1 & {n} & o & p\\
Neonatal mortality, 0--28 days (\textperthousand)  & 7.0 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Birth \textless{} 37 weeks gestational age  & 21 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Birth \textless{} 37 weeks gestational age  & 2.3 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
LGA  & 31 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
SGA  & 2.3 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Major malformations  & 4.7 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Apgar score \textless{} 7 at 5 min  & 3.1 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Apgar score \textless{} 4 at 5 min  & 0.80 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Erb palsy*  & 2.1 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Respiratory distress syndrome  & 1.0 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
Respiratory disorders  & 9.5 & 0.3 & 4.04 (3.02--5.40) & 3.34 (2.46--4.55)\\
\bottomrule
\end{tabularx}

\begin{tablenotes}
      \footnotesize
      \item{Data are proportions or OR (95\% CI). Adjusted OR, OR adjusted for group differences in maternal age, BMI, parity, chronic hypertensive disorder, smoking habits, and ethnicity. *Vaginal deliveries only.}
    \end{tablenotes}
  \end{threeparttable}
\end{table}

\end{document}

相关内容