0

I am using the following codes:

> \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}

and getting this output:

enter image description here

I was wondering how the top header representing years can be centered.

5
  • 1
    The simplest would be to use &\centering 2017&\centering 2018 & ….
    – Bernard
    Commented Feb 24, 2021 at 23:49
  • @Bernard I tried with `` & \centering 2017 & \centering 2018 & \centering 2019 & \centering 2020\\ \hline`` and getting an error that says misplaced \noalign.
    – OGC
    Commented Feb 24, 2021 at 23:54
  • 2
    \multico;umn{1}[c}{2017} etc. is the usual approach. Commented Feb 24, 2021 at 23:56
  • @OGC have a look athe answer below -- using thead from the makecell package and booktabs for better horizontal lines also removing all verticals
    – js bibra
    Commented Feb 25, 2021 at 0:14
  • 1
    @OGC: You're right – there's a problem with \centering in the last column. You can circumvent it replacing \\ \hline with \tabularnewline \hline for the heads row, but as the aim of using this command was to simplify w.r.t. using \multicolumn{1}{c}{…}, it is debatable.
    – Bernard
    Commented Feb 25, 2021 at 9:05

2 Answers 2

1

enter image description here

\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}

EDIT

\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}

enter image description here

2
  • I actually want to keep the lines in the table
    – OGC
    Commented Feb 25, 2021 at 0:36
  • 1
    not a problem simply revert to your hlines as before in the earlier code you have posted in your question -- remove the toprule, midrule and bottomrule
    – js bibra
    Commented Feb 25, 2021 at 0:39
0

I used the longtable package and it seems to give me what I wanted:

\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}

Thanks for the comments!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .