3

I want to center the multirow cells vertically, but because I need to color them aswell, I can't use \multirowcell from the makecell package as it is not compatible. Therefore, I used the shortstack command to achieve a linebreak, but the vertical alignment seems to be random.

Please don't suggest altering the table without color or the booktabs package.

enter image description here

MWE:

\documentclass[10pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{multirow}
\usepackage{makecell}

\usepackage[usenames, table ,xcdraw]{xcolor}
    \definecolor{mgrau1}{RGB}{230, 230, 230}
    \definecolor{mgrau2}{RGB}{204, 204, 204}

\newcommand{\multirc}[2]{\multirow{#1}{*}{\shortstack[c]{#2}}}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}m{#1}}
\newcolumntype{M}[1]{>{\centering}m{#1}}
\usepackage{hhline}

\begin{document}

\begin{table}
    \setlength\tabcolsep{3.3pt}
    \renewcommand{\arraystretch}{1.2}
    \centering\footnotesize
    \begin{tabular}{|>{\columncolor{mgrau2}}M{1.5cm}|>{\columncolor{mgrau1}}M{1.5cm}|P{2.2cm}|}
        \hline
        \multicolumn{2}{|c|}{} & \\
        \hhline{|~~|~|}
        \multicolumn{2}{|c|}{\multirowcell{-2}{Prozesselement/\\Funktion}} & \multirowcell{-2}{mögliche Fehler}  \\
        \Xhline{2\arrayrulewidth}
        &  &  \\
        \hhline{|>{\arrayrulecolor{mgrau2}}->{\arrayrulecolor{mgrau1}}->{\arrayrulecolor{black}}|-|}
        & \multirc{-2}{STL-Datei\\reparieren} & this is a really long text spanning multiple lines \\
        \hhline{|>{\arrayrulecolor{mgrau2}}->{\arrayrulecolor{black}}|-|-|}
        &  & this is a really long text spanning multiple lines \\
        \hhline{|>{\arrayrulecolor{mgrau2}}->{\arrayrulecolor{mgrau1}}->{\arrayrulecolor{black}}|-|}
        & \multirc{-2}{STL-Datei\\verkleinern} &  \\
        \hhline{|>{\arrayrulecolor{mgrau2}}->{\arrayrulecolor{black}}|-|-|} 
        &  & this is a really long text spanning multiple lines \\
        \hhline{|>{\arrayrulecolor{mgrau2}}->{\arrayrulecolor{mgrau1}}->{\arrayrulecolor{black}}|-|}
        \multirc{-6}{Baujobvor-\\bereitung} & \multirc{-2}{STL-Datei\\platzieren} & this is a really long text spanning multiple lines \\
        \hline
    \end{tabular}
\end{table}

\end{document}

1 Answer 1

3

With {NiceTabular} of nicematrix.

\documentclass[10pt]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{nicematrix}
\usepackage{ragged2e}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}\arraybackslash}m{#1}}
\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}}        

\begin{document}

\begin{table}
\setlength\tabcolsep{3.3pt}
\renewcommand{\arraystretch}{1.2}
\centering\footnotesize
\begin{NiceTabular}{|M{1.5cm}|M{1.5cm}|P{2.2cm}|}[hvlines] 
\CodeBefore
  \rectanglecolor[RGB]{204, 204, 204}{2-1}{7-1} 
  \rectanglecolor[RGB]{230, 230, 230}{2-2}{7-2}
\Body
    \Block{1-2}{Prozesselement \\ Funktion} & & mögliche Fehler \\
    \Block{6-1}{Baujobvor \\ beritugn} & \Block{2-1}{STL-Datei\\ reparieren} \\
     & & this is a really long text spanning multiple lines \\
     & \Block{2-1}{STL-Datei\\ reparieren} & this is a really long text spanning multiple lines \\
    & & \\
     & \Block{2-1}{STL-Datei\\ reparieren} &  this is a really long text spanning multiple lines \\
     & &  this is a really long text spanning multiple lines\\
\end{NiceTabular}
\end{table}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Result of the above code

4
  • 1
    are those commands only available in a NiceTable environment or can they be used in conjunction with other tables such as tabularx or longtable?
    – fuj36840
    Commented May 13, 2020 at 18:05
  • 2
    As for now, they are only available in the environment {NiceTabular}. Maybe in a future version... Commented May 13, 2020 at 18:26
  • Not working on overleaf.com: it says "Undefined control sequence" on line 15,16,17,18,26, and "Package nicematrix Error: You try to draw a block in the cell 0-0 of your matrix but the matrix is too small for that block."
    – root
    Commented Apr 29, 2021 at 16:42
  • @root: Overleaf has not the latest version of nicematrix. You should upload the latest version of the file nicematrix.sty in the repertory of your Overleaf project. You can find that file on the SVN server of TeXLive: www.tug.org/svn/texlive/trunk/Master/texmf-dist/tex/latex/nicematrix/nicematrix.sty Commented Apr 29, 2021 at 16:47

You must log in to answer this question.

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