1

I'm trying to align the following table so that the "21" is centered under its header.

\begin{table}[h]
\centering
\begin{tabularx}{1.09\textwidth}{|cll>{\raggedleft\arraybackslash}X|}
\hline
\textbf{ID}     & \textbf{Severity}     & \textbf{Vulnerability}       &
\textbf{Occurrences} \\
\hline
111111         & Lorem                 & Lorem ipsum dolor sit amet & 21 \\
\hline
\end{tabularx}
\caption{example}
\label{table:example}
\end{table}

enter image description here

I can use \centering instead of \raggedleft for the last column to center the numbers under the header but then the header ends up too much to the left, I want the header to be like in the above picture.

enter image description here

Is there anyway I can get the combination of these two? The header aligned to the right like in the first picture but the number centered under the header like in the second picture.

2
  • Welcome to TeX SX! Have you any reason for a tabularx which flows into the margins?
    – Bernard
    Commented Mar 18, 2019 at 10:21
  • @Bernard The reason i'm using tabularx is to get the same width as my widest table
    – antmo
    Commented Mar 18, 2019 at 10:27

1 Answer 1

2

I would use the X type colum for the 3rd instead of the 4th cell: \begin{tabularx}{1.09\textwidth}{|clXc|}. With \makecell[r]{\textbf{Occurrences}} you can then right align the header:

\documentclass{article}

\usepackage{tabularx}
\usepackage{makecell}

\begin{document}
\begin{table}[h]
\centering
\begin{tabularx}{1.09\textwidth}{|clXc|}
\hline
\textbf{ID}     & \textbf{Severity}     & \textbf{Vulnerability}       &
\makecell[r]{\textbf{Occurrences}} \\
\hline
111111         & Lorem                 & Lorem ipsum dolor sit amet & 21 \\
\hline
\end{tabularx}
\caption{example}
\label{table:example}
\end{table}
\end{document}
2
  • 2
    Maybe using \thead for all column heads would simplify the code?
    – Bernard
    Commented Mar 18, 2019 at 10:19
  • 1
    Thank you, that was an easy solution that gives the right look!
    – antmo
    Commented Mar 18, 2019 at 10:21

You must log in to answer this question.

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