我正在使用以下代码:
> \begin{table} [h!] \caption{Alcohol- or Drug-Related Arrests By
> Ethnicity, 2017-2020} \label{ASUS_drug_arrest_table} \centering
> \begin{tabular}{|p{4cm}|p{1.7cm}|p{1.7cm}|p{1.7cm}|p{1.7cm}|} \hline
> & 2017 & 2018 & 2019 & 2020\\ \hline White/Caucasian & & & &\\
> \hline Black/African American & & & &\\ \hline Hispanic/Latino
> & & & &\\ \hline Native American & & & &\\ \hline Asian & &
> & &\\ \hline Hawaiian/Part Hawaiian & & & &\\ \hline Other
> Pacific Islander & & & &\\ \hline Other & & & &\\ \hline
> \end{tabular} \end{table}
并得到这个输出:
我想知道如何使代表年份的顶部标题居中。
答案1
\documentclass[10pt,a4paper]{article}
\usepackage{booktabs, % new
makecell, % new
}
\renewcommand\theadfont{\centering}
\begin{document}
\begin{tabular}{p{4cm}p{1.7cm}p{1.7cm}p{1.7cm}p{1.7cm}} \toprule
& \thead{2017} & \thead{2018} & \thead{2019} & \thead{2020}\\ \midrule White/Caucasian & & & &\\
\midrule Black/African American & & & &\\ \midrule Hispanic/Latino
& & & &\\ \midrule Native American & & & &\\ \midrule Asian & &
& &\\ \midrule Hawaiian/Part Hawaiian & & & &\\ \midrule Other
Pacific Islander & & & &\\ \midrule Other & & & &\\ \bottomrule
\end{tabular}
\end{document}
编辑
\documentclass[10pt,a4paper]{article}
\begin{document}
\begin{tabular}{|p{4cm}|p{1.7cm}|p{1.7cm}|p{1.7cm}|p{1.7cm}|} \hline
& \thead{2017} & \thead{2018} & \thead{2019} & \thead{2020}\\ \hline White/Caucasian & & & &\\ \hline
Black/African American & & & &\\ \hline
Hispanic/Latino& & & &\\ \hline
Native American & & & &\\ \hline
Asian & && &\\ \hline
Hawaiian/Part Hawaiian & & & &\\ \hline
Other Pacific Islander & & & &\\ \hline
Other & & & &\\ \hline
\end{tabular}
\end{document}
答案2
我使用了 longtable 包,它似乎给了我想要的东西:
\begin{longtable}[h!]{|p{4cm}|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\caption{Alcohol- or Drug-Related Arrests By Ethnicity, 2017-2020}
\label{ASUS_drug_arrest_table} \\
\hline \multicolumn{1}{|c|}{} & \multicolumn{1}{c|}{2017} &
\multicolumn{1}{c|}{2018} &
\multicolumn{1}{c|}{2019} &
\multicolumn{1}{c|}{2020}\\ \hline
White/Caucasian & & & &\\
\hline
Black/African American & & & &\\
\hline
Hispanic/Latino & & & &\\
\hline
Native American & & & &\\
\hline
Asian & & & & \\
\hline
Hawaiian/Part Hawaiian & & & &\\
\hline
Other Pacific Islander & & & &\\
\hline
Other & & & &\\
\hline
\end{longtable}
感谢您的评论!