我无法制作纵向表格,因为我有一些较大的列标题(有些带有连字符)无法换行。我创建此表格的唯一方法是将其制作成横向。请指出如何修改我的代码,以便我可以纵向制作下面的表格。
\documentclass{report}
\usepackage{booktabs, multirow, pdflscape}
\begin{document}
\begin{landscape}
\begin{table}[t]
\caption{This is my table and I hope you can help me improve
it. How do I make it in portrait?}
\label{tab:testtab}
\begin{tabular*}{\textwidth} {p{1.4cm}|p{1cm}p{2.9cm}p{3.5cm}|p{1cm}p{2.9cm}p{3.5cm}|p{2cm}}
\toprule
\multirow{2}[2]{1.4cm} {Col group (years)} & \multicolumn{3}{c}
{{\textbf{Location one}}} & \multicolumn{3}{c}{{\textbf{Location two}}} &
\multirow{2}[2]{2.6cm}{\textbf{Combined conditionX- associated SOME cases}}\\
& SOME cases & Percent positive for conditionX & conditionX-associated SOME cases & SOME cases & Percent positive for conditionX & conditionX-associated SOME cases & \\
\midrule 0-$<$1 & 455 & 10.4 (15/144) & 47 & 381 & 10.0 (2/20) & 38 & 86 (25\%)\\
1-4 & 376 & 10.9 (19/175) & 41 & 256 & 20.8 (10/48)& 53 & 94 (28\%)\\
5-15 & 91 & 10.0 (1/10) & 9 & 157 & 30.0 (3/10) & 47 & 56 (17\%)\\
16-24 & & & & 91 & 12.0 (3/25) & 11 & 11 (3\%)\\
25-49 & & & & 244 & 11.4 (8/70) & 28 & 28 (8\%)\\
50-64 & & & & 280 & 10.0 (3/30) & 28 & 28 (8\%)\\
$\geq$65 & & & & 334 & 11.1 (5/45) & 37 & 37 (11\%)\\
\hline
Total & 922 & 10.6 (35/329) & 97 & 1,743& 13.7 (34/248) & 242 & 340
(100\%)\\ \bottomrule
\end{tabular*}
\end{table}
\end{landscape}
\end{document}
要将此表转换为纵向,我需要将第 3 列和第 4 列换行(文本应显示在 3 行而不是 2 行中)。完成此操作后,最后一列看起来会很好(即文本应分布在 4 行中)。注意:我在文档的其他地方列出了几个使用的软件包。希望有人能帮忙。
答案1
一些建议:
不要猜测列的宽度
p
,而是使用说明进行测量\settowidth
。有四种不同的宽度需要计算。由于您希望使用
tabular*
环境,请将\tabcolsep
参数设置为,0pt
并通过提供指令让 LaTeX 找出列间空格的数量@{\extracolsep{\fill}}
。在表格标题中提供更多视觉结构。使用
\cmidrule
指令。不要在表格中使用垂直线。它们是没有必要的。
我上次检查时,,
47 + 38 \ne 86
和97 + 242 \ne 340
。:-)写成
conditionX- associated
而不是conditionX-associated
。这样,conditionX-
即使您已禁止使用连字符,在第 4、7 和 8 列的标题后仍可以换行。顺便说一句,我认为通过指令抑制所有连字符,然后恢复一些体面的排版,这种做法非常值得怀疑
\sloppy
。如果你真的不能忍受连字符,那就制作整个文档\raggedright
,并承担后果。就此而言,连字符有什么可怕的,让你愿意放弃体面的排版输出?
\documentclass[a4paper]{report}
\usepackage{geometry}
\geometry{includehead,headheight=15pt,
hmargin={3.5cm,2.3cm},vmargin={1.6cm,2.5cm}}
\sloppy %% highly questionable!
%\hyphenation{}
\hyphenpenalty=10000
\exhyphenpenalty=10000
\usepackage[skip=0.333\baselineskip]{caption} % optional
\usepackage{booktabs}
\usepackage{array} % for "\newcolumntype" macro
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
% Measure four separate column widths:
\newlength\lena \settowidth{\lena}{(years)}
\newlength\lenb \settowidth{\lenb}{SOME}
\newlength\lenc \settowidth{\lenc}{10.9 (19/175)}
\newlength\lend \settowidth{\lend}{SOME cases}
\begin{document}
\begin{table}[t]
\setlength\tabcolsep{0pt} % default: 6pt
\caption{This table now fits in portrait mode}
\label{tab:testtab}
\begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}
P{\lena} P{\lenb}P{\lenc}P{\lend}
P{\lenb}P{\lenc}P{\lend} P{\lend} }
\toprule
& \multicolumn{3}{c}{Location one}
& \multicolumn{3}{c}{Location two}
& Combined\\
\cmidrule{2-4} \cmidrule{5-7} \cmidrule(l){8-8}
Col group (years)
& SOME cases
& Percent positive for conditionX
& conditionX- associated SOME cases
& SOME cases
& Percent positive for conditionX
& conditionX- associated SOME cases
& conditionX- associated SOME cases \\
\midrule
0--$<$1 & 455 & 10.4 (15/144) & 47 & 381 & 10.0 (2/20) & 38 & 86 (25\%)\\
1--4 & 376 & 10.9 (19/175) & 41 & 256 & 20.8 (10/48)& 53 & 94 (28\%)\\
5--15 & 91 & 10.0 (1/10) & 9 & 157 & 30.0 (3/10) & 47 & 56 (17\%)\\
16--24 & & & & 91 & 12.0 (3/25) & 11 & 11 (3\%)\\
25--49 & & & & 244 & 11.4 (8/70) & 28 & 28 (8\%)\\
50--64 & & & & 280 & 10.0 (3/30) & 28 & 28 (8\%)\\
$\geq$65 & & & & 334 & 11.1 (5/45) & 37 & 37 (11\%)\\
\midrule
Total & 922 & 10.6 (35/329) & 97 &1,743&13.7 (34/248)&242 &340 (100\%) \\
\bottomrule
\end{tabular*}
\end{table}
\end{document}
答案2
另一个建议:交换行和列并添加一些颜色。
\documentclass{report}
\usepackage{booktabs, multirow, caption, array, makecell}
\usepackage{siunitx}
\usepackage[svgnames, table]{xcolor}
\begin{document}
\begin{table}[!htb]
\setlength{\extrarowheight}{2pt}
\setlength{\tabcolsep}{5pt}
\sisetup{range-phrase=--}
\caption{Table in portrait – rows and columns are swapped}
\centering
\begin{tabular}{*{8}{c}}
\toprule
\multicolumn{8}{c}{Col group (years)}\\
\addlinespace
\numrange[range-phrase=\,--\textless]{0}{1} & \numrange{1}{4} & \numrange{5}{15} & \numrange{16}{24}
& \numrange{25}{49} & \numrange{50}{64} & $\geq 65$ & \textbf{Total} \\
\addlinespace[1ex]
\rowcolor{WhiteSmoke!50! Lavender}\multicolumn{8}{@{}c@{}}{\textbf{Location one}}\\
\addlinespace[1ex]
\multicolumn{8}{@{}l}{SOME cases}\\
555 & 376 & 91 & & & & & 922 \\
\addlinespace
\multicolumn{ 8}{@{}l}{Percent positive for conditionX}\\
\makecell{10.4 \\ (15/144) } & \makecell{10.9\\ (19/175)} & \makecell{10.0 \\ (1/10)} & & & & & \makecell{10.6\\ (35/329)} \\
\addlinespace
\multicolumn{8}{@{}l}{ConditionX-associated SOME cases} \\
47 & 41 & 9 \\
\addlinespace[1ex]
\rowcolor{WhiteSmoke!50! Lavender}\multicolumn{ 8}{@{}c@{}}{\textbf{Location two}} \\
\addlinespace[1ex]
\multicolumn{8}{@{}l}{SOME cases}\\
381 & 256 & 157 & 91 & 244 & 280 & 334 & 1{,}743 \\
\addlinespace
\multicolumn{8}{@{}l}{Percent positive for conditionX}\\
\makecell{10.0 \\ (2/20)} & \makecell{20.8 \\ (10/48) } & \makecell{30.0\\ (3/10)} & \makecell{12.0\\ (3/25)}& \makecell{11.4\\ (8/70)} & \makecell{10.0 \\ (3/30)} & \makecell{11.1\\ (5/45)} & \makecell{13.7\\ (34/248)} \\
\addlinespace
\multicolumn{8}{@{}l}{ConditionX-associated SOME cases} \\
38 & 53 & 47 & 11 & 28 & 28 & 37 & 242 \\
\addlinespace[1ex]
\rowcolor{WhiteSmoke!50! Lavender}\multicolumn{ 8}{@{}c@{}}{\textbf{Combined conditionX-associated SOME cases}} \\
\addlinespace[1ex]
\makecell{86\\ (25\,\%)} & \makecell{94\\ (28\,\%)} & \makecell{56 \\ (17\,\%)} & \makecell{11 \\ (3\,\%)} & \makecell{28 \\ (8\,\%)} & \makecell\\{28 (8\,\%)} & \makecell{37 \\ (11\,\%)} & \makecell{340 \\ (100\,\%)} \\
\bottomrule
\end{tabular}
\end{table}
\end{document}