# Auto center a table entry vertically and horizontally using tabularx

I wonder if its possible to automatically center a table entry vertically and horizontally within a tabularx environment.

You can see that the matrix does not center:

The code I want to adjust is:

\begin{table}[h]
\renewcommand{\arraystretch}{1.1}
\centering{\caption{Test Table}
\begin{tabularx}{\textwidth}{|X||c|c|c|c|c|}
\hline
Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
%   \cline{2-5}
& $\boldsymbol{X}_1$    & $\boldsymbol{X}_1$ & $\boldsymbol{X}_2$ & {$\boldsymbol{X}_2$} & {Holder4 additional}\\
\cline{1-6}
abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11} \end{bmatrix}$   & $\begin{bmatrix} D_{11} \end{bmatrix}$    & $\begin{bmatrix} K_{12} \end{bmatrix}$    & 0  & 22\\
\hline
abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ Kl_{21} \end{bmatrix}$      & $\begin{bmatrix} D_{11} \\ D_{21} \end{bmatrix}$     & $\begin{bmatrix} X_{12} \\ S_{22} \end{bmatrix}$    & 0  & 1\\
\hline
\text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} \\ W_{31} & O_{61} \end{bmatrix}$      & $\begin{bmatrix} G_{11} \\ G_{21} \\ G_{31} \end{bmatrix}$   & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12} \end{bmatrix}$   & 0  & 3\\
\hline
\end{tabularx}
\label{}}


\end{table}

• Welcome to TeX SX! Is it for a single entry (I suppose the first column head) or for all entries in the column? – Bernard May 28 '17 at 9:55
• Hi Bernard, it should be for all entries. Especially for the entries with the matrix since they overlap – Zeit May 28 '17 at 9:58

Redefine the X column type as m{#1}. I added some vertical padding with cellspace and extrarowheight and gave a decent spacing between caption and table with the caption package.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tabularx, caption}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\renewcommand{\tabularxcolumn}[1]{>{\centering}m{#1}}

\begin{document}

\begin{table}[h]
\caption{Test Table}
\setlength\extrarowheight{2pt}
\begin{tabularx}{\textwidth}{|X||*{5}{Sc|}}
\hline
Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
\cline{2-5}
& $\boldsymbol{X}₁$ & $\boldsymbol{X}₁$ & $\boldsymbol{X}₂$ & {$\boldsymbol{X}₂$} & {Holder4 additional} \\
\cline{1-6}
\strut abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11} \end{bmatrix}$ & $\begin{bmatrix} D_{11} \end{bmatrix}$ & $\begin{bmatrix} K_{12} \end{bmatrix}$ & 0 & 22 \\
\hline
abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ Kl_{21} \end{bmatrix}$ & $\begin{bmatrix} D_{11} \\ D_{21} \end{bmatrix}$ & $\begin{bmatrix} X_{12} \\ S_{22} \end{bmatrix}$ & 0 & 1 \\
\hline
\text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} \\ W_{31} & O_{61} \end{bmatrix}$ & $\begin{bmatrix} G_{11} \\ G_{21} \\ G_{31} \end{bmatrix}$ & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12} \end{bmatrix}$ & 0 & 3 \\
\hline
\end{tabularx}
\label{}
\end{table}

\end{document}


• Thank you Bernard, do you know why I get the following errors using your code?: siunitx error: "duplicate-exponent-token" Duplicate exponent marker token '\token_to_str:N e' in input. For immediate help type H <return>. \end{tabularx} -------------------- siunitx error: "invalid-number" Invalid numerical input 'de'. For immediate help type H <return>. \end{tabularx} -------------------- Emergency stop. \end{tabularx} – Zeit May 28 '17 at 10:43
• This did not happen with just my code, since it doesn't use siunitx. Could you post a (full) minimal example code reproducing the error? – Bernard May 28 '17 at 10:47
• Yes you are right, it works if I simply execute your code. If I add the \usepackage{siunitx} it throws an error – Zeit May 28 '17 at 10:59
• @Zeit: I had forgotten: that is due to the S pre-qualifier used by cellspace to ensure padding in the corresponding columns, as it is also defined as a column type by siunitx. The solution is explained in the documentation of the latter package: use the pre-qualifier C instead–hoping you don't already have to use a C column type… – Bernard May 28 '17 at 11:16
• That is a great comment! Now it is simply perfect with the C pre-qualifier :) Thank you – Zeit May 28 '17 at 11:22
\documentclass[a4paper,11pt,ngerman]{scrartcl}
\usepackage{amsmath}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}
\begin{document}

\renewcommand{\arraystretch}{1.7}
\begin{tabularx}{\textwidth}{|X||c|c|c|c|c|}        \hline
Holder1 & \multicolumn{2}{c|}{Holder2}& \multicolumn{2}{c|}{Holder3} & Holder4 \\
%   \cline{2-5}
& $\boldsymbol{X}_1$    & $\boldsymbol{X}_1$ & $\boldsymbol{X}_2$ &
{$\boldsymbol{X}_2$} & {Holder4 additional}\\
\cline{1-6}
abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} W_{11} \end{bmatrix}$   & $\begin{bmatrix} D_{11} \end{bmatrix}$    & $\begin{bmatrix} K_{12} \end{bmatrix}$    & 0  & 22\\
\hline
abcv abcv abcv abcv abcv abcv ac sf abcv abcv & $\begin{bmatrix} DD_{11} \\ Kl_{21} \end{bmatrix}$      & $\begin{bmatrix} D_{11} \\ D_{21} \end{bmatrix}$     & $\begin{bmatrix} X_{12} \\ S_{22} \end{bmatrix}$    & 0  &
1\\
\hline
\strut\text{text text text} & $\begin{bmatrix} G_{11} & G_{41} \\ G_{21} & G_{51} \\ W_{31} & O_{61} \end{bmatrix}$      & $\begin{bmatrix} G_{11} \\ G_{21} \\ G_{31} \end{bmatrix}$   & $\begin{bmatrix} F_{12} \\ F_{12} \\ X_{12} \end{bmatrix}$   & 0  & 3\\
\hline
\end{tabularx}
\end{document}


However, I wouldn't use all the vertical lines.

• Thank you very much Herbert, it looks much better now. Do you think it is also possible to add extra space between the brackets of the matrix and the horizontal lines? Especially the last row is quite close to the this line. – Zeit May 28 '17 at 10:18
• for the last row use something like \rule[-1cm]{0pt}{2cm}\text{text text text} & ... . It is an invisible vertical line – user2478 May 28 '17 at 10:26