这些是我的 longtable 代码:
\documentclass{article}
\usepackage{longtable}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{caption}
\begin{document}
\begin{longtable}{l|l|l|l|l|l}
\caption {Covid-19 Test Results by Demographics} \label{tab:title} \\
% header and footer information
\hline
Variable & Negative, N = 25 & Pending, N = 42 & Positive, N = 395 & (Missing), N = 91 & All, N=553\\
\hline
Homeless & & & & & \\
\hline
\hspace{0.3em}No & 15 (2.7\%) & 23 (4.2\%) & 246 (44\%) & 56 (10\%) & 340 (61\%)\\
\hline
\hspace{0.3em}Yes & 8 (1.4\%) & 14 (2.5\%) & 78 (14\%) & 14 (2.5\%) & 114 (21\%)\\
\hline
\hspace{0.3em}(Missing) & 2 (0.4\%) & 5 (0.9\%) & 71 (13\%) & 21 (3.8\%) & 99 (18\%)\\
\hline
Age Group & & & & & \\
\hline
\hspace{0.3em}0-14 & 1 (0.2\%) & 6 (1.1\%) & 67 (12\%) & 12 (2.2\%) & 86 (16\%)\\
\hline
\hspace{0.3em}15-19 & 0 (0\%) & 4 (0.7\%) & 28 (5.1\%) & 6 (1.1\%) & 38 (6.9\%)\\
\hline
\hspace{0.3em}20-24 & 1 (0.2\%) & 5 (0.9\%) & 21 (3.8\%) & 10 (1.8\%) & 37 (6.7\%)\\
\hline
\hspace{0.3em}25-29 & 3 (0.5\%) & 1 (0.2\%) & 22 (4.0\%) & 6 (1.1\%) & 32 (5.8\%)\\
\hline
\hspace{0.3em}30-34 & 1 (0.2\%) & 1 (0.2\%) & 21 (3.8\%) & 6 (1.1\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}35-39 & 1 (0.2\%) & 2 (0.4\%) & 37 (6.7\%) & 6 (1.1\%) & 46 (8.3\%)\\
\hline
\hspace{0.3em}40-44 & 2 (0.4\%) & 2 (0.4\%) & 22 (4.0\%) & 5 (0.9\%) & 31 (5.6\%)\\
\hline
\hspace{0.3em}45-49 & 3 (0.5\%) & 5 (0.9\%) & 21 (3.8\%) & 0 (0\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}50-54 & 3 (0.5\%) & 2 (0.4\%) & 15 (2.7\%) & 9 (1.6\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}55-59 & 0 (0\%) & 1 (0.2\%) & 24 (4.3\%) & 5 (0.9\%) & 30 (5.4\%)\\
\hline
\hspace{0.3em}60-64 & 3 (0.5\%) & 2 (0.4\%) & 24 (4.3\%) & 9 (1.6\%) & 38 (6.9\%)\\
\hline
\hspace{0.3em}65-69 & 1 (0.2\%) & 1 (0.2\%) & 33 (6.0\%) & 11 (2.0\%) & 46 (8.3\%)\\
\hline
\hspace{0.3em}70-74 & 3 (0.5\%) & 3 (0.5\%) & 33 (6.0\%) & 2 (0.4\%) & 41 (7.4\%)\\
\hline
\hspace{0.3em}75+ & 3 (0.5\%) & 7 (1.3\%) & 25 (4.5\%) & 4 (0.7\%) & 39 (7.1\%)\\
\hline
\hspace{0.3em}(Missing) & 0 (0\%) & 0 (0\%) & 2 (0.4\%) & 0 (0\%) & 2 (0.4\%)\\
\hline
Gender & & & & & \\
\hline
\hspace{0.3em}Male & 13 (2.4\%) & 18 (3.3\%) & 220 (40\%) & 53 (9.6\%) & 304 (55\%)\\
\hline
\hspace{0.3em}Female & 12 (2.2\%) & 23 (4.2\%) & 170 (31\%) & 37 (6.7\%) & 242 (44\%)\\
\hline
\hspace{0.3em}(Missing) & 0 (0\%) & 1 (0.2\%) & 5 (0.9\%) & 1 (0.2\%) & 7 (1.3\%)\\
\hline
Race-NHOPI & & & & & \\
\hline
\hspace{0.3em}NHOPI & 7 (1.3\%) & 12 (2.2\%) & 96 (17\%) & 24 (4.3\%) & 139 (25\%)\\
\hline
\hspace{0.3em}(Missing) & 18 (3.3\%) & 30 (5.4\%) & 299 (54\%) & 67 (12\%) & 414 (75\%)\\
\hline
Race-American Indian & & & & & \\
\hline
\hspace{0.3em}American Indian & 0 (0\%) & 0 (0\%) & 5 (0.9\%) & 1 (0.2\%) & 6 (1.1\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 42 (7.6\%) & 390 (71\%) & 90 (16\%) & 547 (99\%)\\
\hline
Race-Asian & & & & & \\
\hline
\hspace{0.3em}Asian & 4 (0.7\%) & 8 (1.4\%) & 78 (14\%) & 21 (3.8\%) & 111 (20\%)\\
\hline
\hspace{0.3em}(Missing) & 21 (3.8\%) & 34 (6.1\%) & 317 (57\%) & 70 (13\%) & 442 (80\%)\\
\hline
Race-Black & & & & & \\
\hline
\hspace{0.3em}Black & 0 (0\%) & 1 (0.2\%) & 2 (0.4\%) & 1 (0.2\%) & 4 (0.7\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 41 (7.4\%) & 393 (71\%) & 90 (16\%) & 549 (99\%)\\
\hline
Race-Hispanic & & & & & \\
\hline
\hspace{0.3em}Hispanic & 0 (0\%) & 1 (0.2\%) & 13 (2.4\%) & 4 (0.7\%) & 18 (3.3\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 41 (7.4\%) & 382 (69\%) & 87 (16\%) & 535 (97\%)\\
\hline
Race-White & & & & & \\
\hline
\hspace{0.3em}White & 0 (0\%) & 2 (0.4\%) & 31 (5.6\%) & 11 (2.0\%) & 44 (8.0\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 40 (7.2\%) & 364 (66\%) & 80 (14\%) & 509 (92\%)\\
\hline
Race-Other Race& & & & & \\
\hline
\hspace{0.3em}Other & 2 (0.4\%) & 4 (0.7\%) & 47 (8.5\%) & 12 (2.2\%) & 65 (12\%)\\
\hline
\hspace{0.3em}(Missing) & 23 (4.2\%) & 38 (6.9\%) & 348 (63\%) & 79 (14\%) & 488 (88\%)\\
\hline
\multicolumn{6}{l}{\rule{0pt}{1em}\textsuperscript{1} Statistics presented: n (\%)}\\
\end{longtable}
\end{document}
我试图让表格居中并显示所有列,但到目前为止,得到的只是该表格作为输出
有什么建议可以使得长桌居中并显示所有列吗?
答案1
您可以使用 apckage 在列标题中引入换行符makecell
并减少 的值\tabcolsep
,以减少表格宽度。我借此机会添加了一些美学改进:
\documentclass{article}
\usepackage{longtable}
\usepackage{makecell}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}
\usepackage{caption}
\begin{document}
\renewcommand{\theadfont}{\normalsize}
\setlength{\tabcolsep}{4pt}
\setlength{\extrarowheight}{2pt}
\centering
\begin{longtable}{|l|l|l|l|l|l|}
\caption {Covid-19 Test Results by Demographics} \label{tab:title} \\
% header and footer information
\hline
Variable & \thead{Negative,\\ N = 25} & \thead{Pending,\\ N = 42} & \thead{Positive,\\ N = 395} & \thead{(Missing),\\ N = 91} & \thead{All,\\ N=553}\\
\endfirsthead
\caption*{Covid-19 Test Results by Demographics\enspace (continued)} \\
\hline
Variable & \thead{Negative,\\ N = 25} & \thead{Pending,\\ N = 42} & \thead{Positive,\\ N = 395} & \thead{(Missing),\\ N = 91} & \thead{All,\\ N=553}\\
\endhead
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Homeless} \\
\hline
\hspace{0.3em}No & 15 (2.7\%) & 23 (4.2\%) & 246 (44\%) & 56 (10\%) & 340 (61\%)\\
\hline
\hspace{0.3em}Yes & 8 (1.4\%) & 14 (2.5\%) & 78 (14\%) & 14 (2.5\%) & 114 (21\%)\\
\hline
\hspace{0.3em}(Missing) & 2 (0.4\%) & 5 (0.9\%) & 71 (13\%) & 21 (3.8\%) & 99 (18\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Age Group} \\
\hline
\hspace{0.3em}0-14 & 1 (0.2\%) & 6 (1.1\%) & 67 (12\%) & 12 (2.2\%) & 86 (16\%)\\
\hline
\hspace{0.3em}15-19 & 0 (0\%) & 4 (0.7\%) & 28 (5.1\%) & 6 (1.1\%) & 38 (6.9\%)\\
\hline
\hspace{0.3em}20-24 & 1 (0.2\%) & 5 (0.9\%) & 21 (3.8\%) & 10 (1.8\%) & 37 (6.7\%)\\
\hline
\hspace{0.3em}25-29 & 3 (0.5\%) & 1 (0.2\%) & 22 (4.0\%) & 6 (1.1\%) & 32 (5.8\%)\\
\hline
\hspace{0.3em}30-34 & 1 (0.2\%) & 1 (0.2\%) & 21 (3.8\%) & 6 (1.1\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}35-39 & 1 (0.2\%) & 2 (0.4\%) & 37 (6.7\%) & 6 (1.1\%) & 46 (8.3\%)\\
\hline
\hspace{0.3em}40-44 & 2 (0.4\%) & 2 (0.4\%) & 22 (4.0\%) & 5 (0.9\%) & 31 (5.6\%)\\
\hline
\hspace{0.3em}45-49 & 3 (0.5\%) & 5 (0.9\%) & 21 (3.8\%) & 0 (0\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}50-54 & 3 (0.5\%) & 2 (0.4\%) & 15 (2.7\%) & 9 (1.6\%) & 29 (5.2\%)\\
\hline
\hspace{0.3em}55-59 & 0 (0\%) & 1 (0.2\%) & 24 (4.3\%) & 5 (0.9\%) & 30 (5.4\%)\\
\hline
\hspace{0.3em}60-64 & 3 (0.5\%) & 2 (0.4\%) & 24 (4.3\%) & 9 (1.6\%) & 38 (6.9\%)\\
\hline
\hspace{0.3em}65-69 & 1 (0.2\%) & 1 (0.2\%) & 33 (6.0\%) & 11 (2.0\%) & 46 (8.3\%)\\
\hline
\hspace{0.3em}70-74 & 3 (0.5\%) & 3 (0.5\%) & 33 (6.0\%) & 2 (0.4\%) & 41 (7.4\%)\\
\hline
\hspace{0.3em}75+ & 3 (0.5\%) & 7 (1.3\%) & 25 (4.5\%) & 4 (0.7\%) & 39 (7.1\%)\\
\hline
\hspace{0.3em}(Missing) & 0 (0\%) & 0 (0\%) & 2 (0.4\%) & 0 (0\%) & 2 (0.4\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Gender} \\
\hline
\hspace{0.3em}Male & 13 (2.4\%) & 18 (3.3\%) & 220 (40\%) & 53 (9.6\%) & 304 (55\%)\\
\hline
\hspace{0.3em}Female & 12 (2.2\%) & 23 (4.2\%) & 170 (31\%) & 37 (6.7\%) & 242 (44\%)\\
\hline
\hspace{0.3em}(Missing) & 0 (0\%) & 1 (0.2\%) & 5 (0.9\%) & 1 (0.2\%) & 7 (1.3\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-NHOPI} \\
\hline
\hspace{0.3em}NHOPI & 7 (1.3\%) & 12 (2.2\%) & 96 (17\%) & 24 (4.3\%) & 139 (25\%)\\
\hline
\hspace{0.3em}(Missing) & 18 (3.3\%) & 30 (5.4\%) & 299 (54\%) & 67 (12\%) & 414 (75\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-American Indian} \\
\hline
\hspace{0.3em}American Indian & 0 (0\%) & 0 (0\%) & 5 (0.9\%) & 1 (0.2\%) & 6 (1.1\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 42 (7.6\%) & 390 (71\%) & 90 (16\%) & 547 (99\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-Asian} \\
\hline
\hspace{0.3em}Asian & 4 (0.7\%) & 8 (1.4\%) & 78 (14\%) & 21 (3.8\%) & 111 (20\%)\\
\hline
\hspace{0.3em}(Missing) & 21 (3.8\%) & 34 (6.1\%) & 317 (57\%) & 70 (13\%) & 442 (80\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-Black} \\
\hline
\hspace{0.3em}Black & 0 (0\%) & 1 (0.2\%) & 2 (0.4\%) & 1 (0.2\%) & 4 (0.7\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 41 (7.4\%) & 393 (71\%) & 90 (16\%) & 549 (99\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-Hispanic} \\
\hline
\hspace{0.3em}Hispanic & 0 (0\%) & 1 (0.2\%) & 13 (2.4\%) & 4 (0.7\%) & 18 (3.3\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 41 (7.4\%) & 382 (69\%) & 87 (16\%) & 535 (97\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-White} \\
\hline
\hspace{0.3em}White & 0 (0\%) & 2 (0.4\%) & 31 (5.6\%) & 11 (2.0\%) & 44 (8.0\%)\\
\hline
\hspace{0.3em}(Missing) & 25 (4.5\%) & 40 (7.2\%) & 364 (66\%) & 80 (14\%) & 509 (92\%)\\
\hline\noalign{\smallskip}
\multicolumn{6}{@{}l}{Race-Other Race} \\
\hline
\hspace{0.3em}Other & 2 (0.4\%) & 4 (0.7\%) & 47 (8.5\%) & 12 (2.2\%) & 65 (12\%)\\
\hline
\hspace{0.3em}(Missing) & 23 (4.2\%) & 38 (6.9\%) & 348 (63\%) & 79 (14\%) & 488 (88\%)\\
\hline
\multicolumn{6}{l}{\rule{0pt}{1em}\textsuperscript{1} Statistics presented: n (\%)}\\
\end{longtable}
\end{document}
答案2
您可能还喜欢这种基于tabular*
、booktabs
和的替代布局siunitx
。如果您将字体大小缩小到\small
,整个表格将适合标准 的文本块article
:
\documentclass{article}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{siunitx}
\sisetup{range-phrase=\,--\,}
\renewcommand{\defaultaddspace}{3pt}
\usepackage{caption}
\begin{document}
\begin{table}
\renewcommand{\theadfont}{\normalsize}
\centering
\small
\setlength{\tabcolsep}{0pt}
\caption {Covid-19 Test Results by Demographics} \label{tab:title}
\begin{tabular*}{\textwidth}{@{\hspace{6pt}}l@{\extracolsep{\fill}}
S[table-format=2]
S[table-format=1.1,input-symbols = {()},table-space-text-pre=(, table-space-text-post=\%),table-align-text-post=false]@{\;}
S[table-format=2]
S[table-format=1.1,input-symbols = {()},table-space-text-pre=(, table-space-text-post=\%),table-align-text-post=false]@{\;}
S[table-format=3]
S[table-format=2.1,input-symbols = {()},table-space-text-pre=(, table-space-text-post=\%),table-align-text-post=false]@{\;}
S[table-format=2]
S[table-format=2.1,input-symbols = {()},table-space-text-pre=(, table-space-text-post=\%),table-align-text-post=false]@{\;}
S[table-format=3]
S[table-format=2.1,input-symbols = {()},table-space-text-pre=(, table-space-text-post=\%),table-align-text-post=false]@{}
}
\toprule
Variable
& \multicolumn{2}{c}{\thead{Negative,\\ N = 25}}
& \multicolumn{2}{c}{\thead{Pending,\\ N = 42}}
& \multicolumn{2}{c}{\thead{Positive,\\ N = 395}}
& \multicolumn{2}{c}{\thead{(Missing),\\ N = 91}}
& \multicolumn{2}{c}{\thead{All,\\ N = 553}}\\
\midrule
\multicolumn{6}{@{}l}{\itshape Homeless} \\
No & 15 & (2.7\%) & 23 & (4.2\%) & 246 & (44\%) & 56 & (10\%) & 340 & (61\%)\\
Yes & 8 & (1.4\%) & 14 & (2.5\%) & 78 & (14\%) & 14 & (2.5\%) & 114 & (21\%)\\
(Missing) & 2 & (0.4\%) & 5 & (0.9\%) & 71 & (13\%) & 21 & (3.8\%) & 99 & (18\%)\\
\midrule
\multicolumn{6}{@{}l}{\itshape Age Group} \\
\numrange{0}{14} & 1 & (0.2\%) & 6 & (1.1\%) & 67 & (12\%) & 12 & (2.2\%) & 86 & (16\%)\\
\numrange{15}{19} & 0 & (0\%) & 4 & (0.7\%) & 28 & (5.1\%) & 6 & (1.1\%) & 38 & (6.9\%)\\
\numrange{20}{24} & 1 & (0.2\%) & 5 & (0.9\%) & 21 & (3.8\%) & 10 & (1.8\%) & 37 & (6.7\%)\\
\addlinespace
\numrange{25}{29} & 3 & (0.5\%) & 1 & (0.2\%) & 22 & (4.0\%) & 6 & (1.1\%) & 32 & (5.8\%)\\
\numrange{30}{34} & 1 & (0.2\%) & 1 & (0.2\%) & 21 & (3.8\%) & 6 & (1.1\%) & 29 & (5.2\%)\\
\numrange{35}{39} & 1 & (0.2\%) & 2 & (0.4\%) & 37 & (6.7\%) & 6 & (1.1\%) & 46 & (8.3\%)\\
\addlinespace
\numrange{40}{44} & 2 & (0.4\%) & 2 & (0.4\%) & 22 & (4.0\%) & 5 & (0.9\%) & 31 & (5.6\%)\\
\numrange{45}{49} & 3 & (0.5\%) & 5 & (0.9\%) & 21 & (3.8\%) & 0 & (0\%) & 29 & (5.2\%)\\
\numrange{50}{54} & 3 & (0.5\%) & 2 & (0.4\%) & 15 & (2.7\%) & 9 & (1.6\%) & 29 & (5.2\%)\\
\addlinespace
\numrange{55}{59} & 0 & (0\%) & 1 & (0.2\%) & 24 & (4.3\%) & 5 & (0.9\%) & 30 & (5.4\%)\\
\numrange{60}{64} & 3 & (0.5\%) & 2 & (0.4\%) & 24 & (4.3\%) & 9 & (1.6\%) & 38 & (6.9\%)\\
\numrange{65}{69} & 1 & (0.2\%) & 1 & (0.2\%) & 33 & (6.0\%) & 11 & (2.0\%) & 46 & (8.3\%)\\
\addlinespace
\numrange{70}{74} & 3 & (0.5\%) & 3 & (0.5\%) & 33 & (6.0\%) & 2 & (0.4\%) & 41 & (7.4\%)\\
75+ & 3 & (0.5\%) & 7 & (1.3\%) & 25 & (4.5\%) & 4 & (0.7\%) & 39 & (7.1\%)\\
(Missing) & 0 & (0\%) & 0 & (0\%) & 2 & (0.4\%) & 0 & (0\%) & 2 & (0.4\%)\\
\midrule
\multicolumn{6}{@{}l}{\itshape Gender} \\
Male & 13 & (2.4\%) & 18 & (3.3\%) & 220 & (40\%) & 53 & (9.6\%) & 304 & (55\%)\\
Female & 12 & (2.2\%) & 23 & (4.2\%) & 170 & (31\%) & 37 & (6.7\%) & 242 & (44\%)\\
(Missing) & 0 & (0\%) & 1 & (0.2\%) & 5 & (0.9\%) & 1 & (0.2\%) & 7 & (1.3\%)\\
\midrule
\multicolumn{6}{@{}l}{\itshape Race} \\
NHOPI & 7 & (1.3\%) & 12 & (2.2\%) & 96 & (17\%) & 24 & (4.3\%) & 139 & (25\%)\\
(Missing) & 18 & (3.3\%) & 30 & (5.4\%) & 299 & (54\%) & 67 & (12\%) & 414 & (75\%)\\
\addlinespace
American Indian & 0 & (0\%) & 0 & (0\%) & 5 & (0.9\%) & 1 & (0.2\%) & 6 & (1.1\%)\\
(Missing) & 25 & (4.5\%) & 42 & (7.6\%) & 390 & (71\%) & 90 & (16\%) & 547 & (99\%)\\
\addlinespace
Asian & 4 & (0.7\%) & 8 & (1.4\%) & 78 & (14\%) & 21 & (3.8\%) & 111 & (20\%)\\
(Missing) & 21 & (3.8\%) & 34 & (6.1\%) & 317 & (57\%) & 70 & (13\%) & 442 & (80\%)\\
\addlinespace
Black & 0 & (0\%) & 1 & (0.2\%) & 2 & (0.4\%) & 1 & (0.2\%) & 4 & (0.7\%)\\
(Missing) & 25 & (4.5\%) & 41 & (7.4\%) & 393 & (71\%) & 90 & (16\%) & 549 & (99\%)\\
\addlinespace
Hispanic & 0 & (0\%) & 1 & (0.2\%) & 13 & (2.4\%) & 4 & (0.7\%) & 18 & (3.3\%)\\
(Missing) & 25 & (4.5\%) & 41 & (7.4\%) & 382 & (69\%) & 87 & (16\%) & 535 & (97\%)\\
\addlinespace
White & 0 & (0\%) & 2 & (0.4\%) & 31 & (5.6\%) & 11 & (2.0\%) & 44 & (8.0\%)\\
(Missing) & 25 & (4.5\%) & 40 & (7.2\%) & 364 & (66\%) & 80 & (14\%) & 509 & (92\%)\\
\addlinespace
Other & 2 & (0.4\%) & 4 & (0.7\%) & 47 & (8.5\%) & 12 & (2.2\%) & 65 & (12\%)\\
(Missing) & 23 & (4.2\%) & 38 & (6.9\%) & 348 & (63\%) & 79 & (14\%) & 488 & (88\%)\\
\bottomrule
\multicolumn{6}{l}{\rule{0pt}{1em}\textsuperscript{1} Statistics presented: n (\%)}\\
\end{tabular*}
\end{table}
\end{document}