1

I have the following code

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}%
\begin{tabularx}{418.26pt}{|c|c|c|>{\centering}X|>{\centering}X|>{\centering}X|c|c|}
\hline
\rowcolor{black!20}Nr. & Date & Version & Changed Chapters & Types of Change & Autor & Status\\\hline
1 & 06.12.2022 & 0.1 & Alle & First Draft & Long Ass Lastname & -\\\hline
\end{tabularx}
\end{document}

which results in the following output

screenshot of output

In addition to centering the cells horizontally, I also want to center them vertically. Ideally, I'd like to define a variant of the cell type X so as to not clutter the actual contents of the table with special case handling.


I've tried both solutions involving tabularx proposed at Vertical alignment in tabular cells with variable height, but neither worked satisfactorally.

The \parbox solution

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}%
\begin{tabularx}{418.26pt}{|c|c|c|>{\centering}X|>{\centering}X|>{\centering}X|c|c|}
\hline
\rowcolor{black!20}Nr. & Date & Version & \noindent\parbox[c]{\hsize}{Changed Chapters} & \noindent\parbox[c]{\hsize}{Types of Change} & Autor & Status\\\hline
1 & 06.12.2022 & 0.1 & Alle & First Draft & \noindent\parbox[c]{\hsize}{Long Ass Lastname} & -\\\hline
\end{tabularx}
\end{document}

results in this monstrosity

vertical size of multiline cells shrinks and word spacing spazzes out

The \multicolumn "solution" is just as bad (except also having to guess an appropriate cell width)

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}%
\begin{tabularx}{418.26pt}{|c|c|c|>{\centering}X|>{\centering}X|>{\centering}X|c|c|}
\hline
\rowcolor{black!20}Nr. & Date & Version & \multicolumn{1}{|m{2cm}|}{Changed Chapters} & \multicolumn{1}{|m{2cm}|}{Types of Change} & Autor & Status\\\hline
1 & 06.12.2022 & 0.1 & Alle & First Draft & \multicolumn{1}{|m{2cm}|}{Long Ass Lastname} & -\\\hline
\end{tabularx}
\end{document}

horizontal centering is off and the cell color in the first row doesn't fill the entire cell

2 Answers 2

1

This solution works perfectly

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[table]{xcolor}
\begin{document}
\renewcommand{\arraystretch}{1.5}%
{\renewcommand\tabularxcolumn[1]{m{#1}}%
\begin{tabularx}{418.26pt}{|c|c|c|>{\centering}X|>{\centering}X|>{\centering}X|c|c|}
\hline
\rowcolor{black!20}Nr. & Date & Version & Changed Chapters & Types of Change & Autor & Status\\\hline
1 & 06.12.2022 & 0.1 & Alle & First Draft & Long Ass Lastname & -\\\hline
\end{tabularx}%
}
\end{document}

enter image description here

1
  • using \renewcommand\tabularxcolumn[1]{m{#1} is what is documented in the tabularx doc Dec 6, 2022 at 12:42
0

Alternatively to arraystretch, you might want to use struts. These are invisible elements which artificially extend height. On the other hand, \arraystretch is asymmetric and larger values make table look awkward; this can still be compensated by setting negative values to \extrarowheight (it requires array package).

The code below demonstrates using a custom strut. The macro \mystrut[top][bottom] adds invisible bars: top and bottom gaps, respectively. Then, this macro can be a part of a column definition affecting each row in the table.

Also, in narrow environments such as small cells, consider using ragged2e as well as microtype. The former provides improved macros to format paragraphs, such as \Centering or \RaggedRight among others, which in this example substitute \centering or \raggedright.

The code

\documentclass{article}
\usepackage{tabularx}
\usepackage{ragged2e,microtype}
\usepackage[table]{xcolor}
\NewDocumentCommand\mystrut{O{1}O{#1}}{%
    \sbox0{X}%
    \rule[\ht0]{0pt}{#1\dimexpr\normalbaselineskip}%
    \rule[#2\dimexpr-\normalbaselineskip]{0pt}{#2\dimexpr\normalbaselineskip}}
\renewcommand\tabularxcolumn[1]{m{#1}}%


\begin{document}

%%% Struts added manually per row
\noindent%
\begin{tabularx}{\textwidth}{|
        *3{c|}
        *3{>{\Centering}X|}
        *2{c|}
    }
    \hline
    \rowcolor{black!20}
    \mystrut%
    Nr. & Date & Version & Changed Chapters & Types of Change & Autor & Status \\
    \hline
    \mystrut 1 & 06.12.2022 & 0.1 & Alle & First Draft & Long Ass Lastname & - \\
    \hline
    \mystrut[1][2]%
    1 & 06.12.2022 & 0.1 & Alle & First Draft & Long Ass Lastname & - \\
    \hline
\end{tabularx}

\bigskip

%%% Struts added in column definition
\noindent%
\begin{tabularx}{\textwidth}{|
        >{\mystrut[1.25]{}}c|
        *2{c|}
        *3{>{\Centering}X|}
        *2{c|}
    }
    \hline
    \rowcolor{black!20}
    Nr. & Date & Version & Changed Chapters & Types of Change & Autor & Status \\
    \hline
    1 & 06.12.2022 & 0.1 & Alle & First Draft & Long Ass Lastname & - \\
    \hline
\end{tabularx}%
\end{document}

enter image description here

You must log in to answer this question.

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