Graphics, Figures & TablesHow to vertically center all elements in a table ?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to vertically center all elements in a table ?

Post by Cham »

Here's a MWE code reproducing my problem. I need all elements to be centered vertically inside their cell. How to do that ?

Code: Select all

\documentclass[11pt,letterpaper,twoside]{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\raggedbottom
\usepackage{microtype}
\usepackage[dvipsnames,table]{xcolor}
\usepackage[total={6in,10in},left=1.5in,top=0.5in,includehead,includefoot]{geometry}
\usepackage{hhline}
\usepackage{tabularx}

\begin{document}

\begin{center}
	\small
	\renewcommand{\arraystretch}{1.5}
	\begin{tabularx}{\textwidth}{|>{\centering\arraybackslash}m{1cm}|>{\centering\arraybackslash}m{1cm}|>{\centering\arraybackslash}m{2cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}m{1.5cm}|>{\centering\arraybackslash}X|}
	\hline
	\multicolumn{1}{|c|}{\normalsize\textsf{Day}}
	& \multicolumn{1}{c|}{\normalsize\textsf{Month}}
	& \multicolumn{1}{c|}{\normalsize\textsf{Hour}}
	& \multicolumn{1}{c|}{\normalsize\textsf{Group}}
	& \multicolumn{1}{c|}{\normalsize\textsf{Room}}
	& \multicolumn{1}{c|}{\normalsize\textsf{Lab title}}
	\\ \hhline{======}
	1 & 09 & 9h to 11h & 20114 & L-2630 &
	\textbf{Some title} \par (some comment)
	\\ \hline
	1 & 09 & 14h to 16h & 20115 & L-2640 &
	\textbf{Another title} \par (some comment)
	\\ \hline
	\end{tabularx}
\end{center}

\end{document}
Here's a preview of what this code is doing. I've shown the problem in red :
table.jpg
table.jpg (34.63 KiB) Viewed 2710 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10326
Joined: Mon Mar 10, 2008 9:44 pm

How to vertically center all elements in a table ?

Post by Stefan Kottwitz »

Hi Cham,

quick solution:

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

Use it in the preamble to have it for all X cells, or just within the center environment to keep the change local there.

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

How to vertically center all elements in a table ?

Post by Cham »

It's working great. Thanks Stefan !
Post Reply