# How to correctly vertical-center cells in a tabularx? [duplicate]

I want to add additional spacing to table rows/cells for a tabularx. However, after setting \arraystretch to something larger than 1 and setting \tabularxcolumn to m (see below), I notice that the content is somewhat vertically centered but shifted considerably down, i.e. below the vertical center of each cell.

How I correctly center this?

\documentclass[a4paper,11pt]{article}

\usepackage{tabularx}
\pagenumbering{gobble}

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

\begin{document}

\begin{tabularx}{\textwidth}{|X|X|}
\hline
hello & world \\
\hline
hello & x\vspace{20ex}\newline world \\
\hline
\end{tabularx}

\end{document}


I've found some answers that hack some seemingly random spacing to the top. However, this doesn't appear to address this particular issue. At least I don't see a general solution by adding a fixed space of e.g. {-1ex} as the spacing appears to depend on the content size. If my eyes play tricks on me and the offset is indeed constant, how do I determine the exact offset?

Edit: cellspace does NOT appear to work in my context. These are the troubles with a minimal working example, I guess.

• Could you please post a minimal example reproducing this problem? – Bernard Oct 20 '19 at 18:05

Here is a way, combining the redefinition of the X column type as an m{...} column, and the S pre-column specifier of cellspace, which ensures a minimal vertical spacing at the top and bottom of cells in columns so prefixed:

\documentclass[a4paper,11pt]{article}

\usepackage{tabularx, cellspace}
\pagenumbering{gobble}

\setlength{\cellspacetoplimit}{30pt}
\setlength{\cellspacebottomlimit}{30pt}

\begin{document}

\renewcommand{\tabularxcolumn}[1]{m{#1}}
\begin{tabularx}{\textwidth}{|S{X}|S{X}|}
\hline
hello & world \\
\hline
hello & x \newline world \\
\hline
\end{tabularx}

\end{document}


With the help of the cellspace package you can achieve the following:

\documentclass{article}

\usepackage{cellspace}
\usepackage{tabularx}

\renewcommand\tabularxcolumn[1]{m{#1}}
\setlength\cellspacetoplimit{2.5\baselineskip}
\setlength\cellspacebottomlimit{2.5\baselineskip}

\begin{document}

\begin{tabularx}{\textwidth}{|SX|SX|}
\hline
hello & world \\
\hline
hello & x\vspace{20ex}\newline world  \\
\hline
\end{tabularx}

\end{document}

• I think it's better to also redefine the X column type. See the layout if you remove \vpace{20ex}. – Bernard Oct 20 '19 at 17:32

You can direct a \vspace{fill} before and after the cell's content, using >{} syntax. If you only have this type of X columns, you put the redirection into the redefinition of the X column, i.e.:

\renewcommand\tabularxcolumn[1]{>{\vspace{\fill}}m{#1}<{\vspace{\fill}}}


You may also use p-columns and a standard tabular. To have the content exactly centred, you need to terminate the cells with \par.

\documentclass[a4paper,11pt]{article}

\usepackage{tabularx, array}
\pagenumbering{gobble}

\renewcommand\tabularxcolumn[1]{>{\vspace{\fill}}m{#1}<{\vspace{\fill}}}
\renewcommand\arraystretch{5}

\newcolumntype{P}{m{\dimexpr(0.5\linewidth-2\tabcolsep-1.5\arrayrulewidth)}}

\begin{document}

%% tabularx and redefined X-columns

\begin{tabularx}{\textwidth}{|X|X|}
\hline
hello\par& world\par\\
\hline
hello\par & x\vspace{20ex}\newline world\par\\
\hline
\end{tabularx}

\vspace{2cm}

%% Tabular and m-columns

\begin{tabular}{|P|P|}
\hline
hello\par& world\par\\
\hline
hello\par& x\vspace{20ex}\newline world\par\\
\hline
\end{tabular}

\end{document}