1

I have a table with short and long headings. I'm using tha column type X from the tabularx package. The headings are aligned to the top, how can I align the headings horizontally in the middle?

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{array}

\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

enter image description here

  • 1
    I removed the align tag because align refers specifically to a multi-line math display, not general alignment questions. – barbara beeton Mar 15 at 18:23
2

Assuming that you want to achieve horizontally and vertically centered entries you can use the following redefinition of X type columns. Normally they are derived from p type columns that are top aligned. With \renewcommand{\tabularxcolumn}[1]{m{#1}} we can change that so that x type columns are derived from the vertically centered m type columns.

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{array}

\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}

If you want to use horizontally and vertically centered X type columns more often throughout your document, you could also add \newcolumntype{Y}{>{\centering\arraybackslash}X} to your preamble and use \begin{tabularx}{\linewidth}{*2{Y}}.


If you wish to keep the original definition of top aligned X type columns and have a separate derived and vertically cerntered column type, you might want to have a look at Copy tabularx X column as new vertically centered column


If you only want to change the vertical alignment for a single table, you could use the following local redefinition of the X type column. Here I have added \renewcommand{\tabularxcolumn}[1]{m{#1}} just after \begin{table} of the corresponding table.

\documentclass[12pt,a4paper]{article}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{array}


\begin{document}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[htb]
\renewcommand{\tabularxcolumn}[1]{m{#1}}
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\begin{table}[htb]
\centering
\caption{My caption}
\label{my-label}
\begin{tabularx}{\linewidth}{*2{>{\centering\arraybackslash}X}}
\toprule
\textbf{Short heading\newline(units)} &
\textbf{This is a long long, very very long heading\newline(units)}\\%
\midrule
416,09 & 194,30 \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}
  • It will bee affect all the tables. What I have to do if I want to modify only one table? – Daniel Valencia C. Mar 15 at 18:46
  • 2
    @DanielValenciaC - In that case, don't issue the instruction \renewcommand{\tabularxcolumn}[1]{m{#1}} in the preamble. Instead, issue it immediately after \begin{table}[htb]. – Mico Mar 15 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.