Vertical and horizontal alignment of text in multicolumn table

alignhorizontal alignmenttablestext;vertical alignment

I created the following table:

Current Table

with the following code:

\begin{table}[h]
    \begin{tabular}
        {|M{0.16\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
          M{0.24\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
          M{0.18\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
          M{0.44\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|}
        \hline
        Zeitpunkt
        & Variable 
        & Datentyp  
        & Beschreibung 
        \\ 
        \hline
        t0, t1, t2 &
        Alter & 
        Numerisch &
        Alter eines Studenten in \newline Jahren 
        \\ 
        \hline
        t0, t1, t2 &
        Geschlecht & 
        Nominal & 
        Geschlecht eine Studenten
        \\ 
        \hline
        t0, t1, t2 &
        HzbArt & 
        Nominal &
        Art der HZB
        \\ 
        \hline
        t0, t1, t2 &
        HzbNote & 
        Numerisch &
        Notendurchschnitt der HZB
        \\ 
        \hline
        t0, t1, t2 &
        Ang\textunderscore Abschluss & 
        Nominal  &  
        Bachelor oder Master
        \\ 
        \hline
        t0, t1, t2 &
        Studiengang & 
        Nominal & 
        Studiengang eines Studenten 
        \\ 
        \hline
        t0, t1, t2 &
        Bildungsland &
        Nominal &
        Bildungsinländer oder \newline Bildungsausländer 
        \\ 
        \hline
        t1, t2 &
        DN \textunderscore Sem\textunderscore1 &
        Numerisch &
        Notendurchschnitt nach dem ersten Semester
        \\ 
        \hline
        t1, t2 &
        NB\textunderscore Pruef\textunderscore \newline Sem\textunderscore1 &
        Numerisch &
        Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester
        \\ 
        \hline
        t2 &
        DN \textunderscore Sem\textunderscore2 &
        Numerisch &
        Notendurchschnitt nach dem zweiten Semester
        \\ 
        \hline
        t2 &
        NB\textunderscore Pruef\textunderscore \newline Sem\textunderscore2 &
        Numerisch &
        Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester
        \\ 
        \hline
        t0, t1, t2 &
        Abbrecher & 
        Nominal &
        Studienabbrecher oder \newline Nicht-Studienabbrecher
        \\ 
        \hline
    \end{tabular}
\caption{Variablen der finalen Datensätze}
\label{Variablen der finalen Datensätze}
\end{table} 

Hoewever, I want to create a table that looks like this:

Table I want to create

In other words, I want to aggregate the cells of the first column that share the same information and align the corresponding text vertically and horizontally.

I've read that I must use a multicolumn table for doing this. However, once I create a multicolumn element with the following code

\begin{table}[h]
        \begin{tabular}
                {|M{0.16\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
                M{0.24\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
                M{0.18\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|
                M{0.44\textwidth-2\tabcolsep - 1.25\arrayrulewidth}|}
            \hline
            Zeitpunkt & 
            Variable & 
            Datentyp & Beschreibung                                                         \\ \hline
            \multirow{7}{*}{t0, t1, t2} 
            & 
            Alter & 
            Numerisch & 
            Alter eines Studenten in Jahren 
            \\ 
            \cline{2-4} 
            & 
            Geschlecht & 
            Nominal & 
            Geschlecht eines Studenten                                    
            \\ 
            \cline{2-4} 
            & 
            HzbArt & 
            Nominal & 
            Art der HZB 
            \\ 
            \cline{2-4} 
            & 
            HzbNote & 
            Numerisch & 
            Notendurchschnitt der HZB 
            \\ 
            \cline{2-4} 
            & 
            Ang\_Abschluss & 
            Nominal & 
            Bachelor oder Master 
            \\ 
            \cline{2-4} 
            & 
            Studiengang & 
            Nominal & 
            Studiengang eines Studenten 
            \\ 
            \cline{2-4} 
            & Bildungsland 
            & Nominal 
            & Bildungsinländer oder Bildungsausländer  
            \\ 
            \hline
            \multirow{2}{*}{t1, t2}   
             & 
             DN\_Sem\_1 & 
             Numerisch & 
             Notendurchschnitt nach dem ersten Semester         
             \\ 
             \cline{2-4} 
             & 
             NB\_Pruef\_ Sem\_1 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester  
             \\ 
             \hline
            \multirow{2}{*}{t2} 
            & 
            DN\_Sem\_2 & 
            Numerisch & 
            Notendurchschnitt nach dem zweiten Semester 
            \\ 
            \cline{2-4} 
            &
             NB\_Pruef\_ Sem\_2 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester     
             \\ 
             \hline
             t0, t1, t2 & 
             Abbrecher & 
             Nominal & 
             Studienabbrecher oder Nicht-Studienabbrecher 
             \\ 
             \hline
        \end{tabular}%
\end{table}

The table will look like this (the text is aligned vertically and horizontally with the wrong proportions):

multicolumn attempt 1

Another code I tried with regular column sizes looks like this:

\begin{table}[h]
        \begin{tabular}{|c|l|l|l|}
            \hline
            Zeitpunkt & 
            Variable & 
            Datentyp & Beschreibung                                                         \\ \hline
            \multirow{7}{*}{t0, t1, t2} 
            & 
            Alter & 
            Numerisch & 
            Alter eines Studenten in Jahren 
            \\ 
            \cline{2-4} 
            & 
            Geschlecht & 
            Nominal & 
            Geschlecht eines Studenten                                    
            \\ 
            \cline{2-4} 
            & 
            HzbArt & 
            Nominal & 
            Art der HZB 
            \\ 
            \cline{2-4} 
            & 
            HzbNote & 
            Numerisch & 
            Notendurchschnitt der HZB 
            \\ 
            \cline{2-4} 
            & 
            Ang\_Abschluss & 
            Nominal & 
            Bachelor oder Master 
            \\ 
            \cline{2-4} 
            & 
            Studiengang & 
            Nominal & 
            Studiengang eines Studenten 
            \\ 
            \cline{2-4} 
            & Bildungsland 
            & Nominal 
            & Bildungsinländer oder Bildungsausländer  
            \\ 
            \hline
            \multirow{2}{*}{t1, t2}   
             & 
             DN\_Sem\_1 & 
             Numerisch & 
             Notendurchschnitt nach dem ersten Semester         
             \\ 
             \cline{2-4} 
             & 
             NB\_Pruef\_ Sem\_1 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester  
             \\ 
             \hline
            \multirow{2}{*}{t2} 
            & 
            DN\_Sem\_2 & 
            Numerisch & 
            Notendurchschnitt nach dem zweiten Semester 
            \\ 
            \cline{2-4} 
            &
             NB\_Pruef\_ Sem\_2 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester     
             \\ 
             \hline
             t0, t1, t2 & 
             Abbrecher & 
             Nominal & 
             Studienabbrecher oder Nicht-Studienabbrecher 
             \\ 
             \hline
        \end{tabular}%
\end{table}

In this case, the table doesn't fit the text width any more and is distorted:

multicolumn attempt 2

How can I manage to create a table that aggregates the desired columns and aligns the their text horizontally and vertically with the right proportions (same space between top-bottom and left-right). Besides, it should also not be larger than the text size of my latex document (which is \documentclass[11pt,a4paper]{article}).

Thanks very much in advance!

Best Answer

Here is a solution to your problem using the tabularray package. The SetCell macro is the equivalent of both the multicolumn and the multirow, in the optional argument r is for the no of rows to be included (c is for columns). The hlines and vlines option tells to set horizontal and vertical lines around every cell and they automatically take care of multirow and multicolumn (if there is any). The X column type is equivalent to the tabularx X column where the optional value sets the horizontal and vertical alignment of the column respectively and the numbered optional value sets the relative width of the column with respect to default width.

\documentclass[11pt,a4paper]{article}

\usepackage{tabularray}

\begin{document}
\begin{table}[h]
        \begin{tblr}{
            hlines, vlines,
            colspec={*{3}{X[c,m]} X[2.5,c,m]},
        }
            Zeitpunkt & 
            Variable & 
            Datentyp & Beschreibung
            \\ 
            \SetCell[r=7]{c}t0, t1, t2 
            & 
            Alter & 
            Numerisch & 
            Alter eines Studenten in Jahren 
            \\ 
            & 
            Geschlecht & 
            Nominal & 
            Geschlecht eines Studenten              
            \\ 
            & 
            HzbArt & 
            Nominal & 
            Art der HZB 
            \\ 
            & 
            HzbNote & 
            Numerisch & 
            Notendurchschnitt der HZB 
            \\
            & 
            Ang\_Abschluss & 
            Nominal & 
            Bachelor oder Master 
            \\
            & 
            Studiengang & 
            Nominal & 
            Studiengang eines Studenten 
            \\
            & Bildungsland 
            & Nominal 
            & Bildungsinländer oder Bildungsausländer  
            \\
            \SetCell[r=2]{c} t1, t2   
             & 
             DN\_Sem\_1 & 
             Numerisch & 
             Notendurchschnitt nach dem ersten Semester 
             \\
             & 
             NB\_Pruef\_ Sem\_1 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester  
             \\
            \SetCell[r=2]{c}t2 
            & 
            DN\_Sem\_2 & 
            Numerisch & 
            Notendurchschnitt nach dem zweiten Semester 
            \\
            &
             NB\_Pruef\_ Sem\_2 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester     
             \\
             t0, t1, t2 & 
             Abbrecher & 
             Nominal & 
             Studienabbrecher oder Nicht-Studienabbrecher 
             \\ 
        \end{tblr}%
\end{table}
\end{document}

enter image description here

However, I will recommend you to get rid of all the vertical lines and most of the horizontal line to give the table a more professional look.

Addendum:

Here is a somewhat better looking (to me) version of the table. I think the left alignment of the 2nd, 3rd and 4th columns (with centered header) looks better.

\documentclass[11pt,a4paper]{article}

\usepackage{tabularray}
\UseTblrLibrary{booktabs} %allows to use the booktabs rules in the tblr environment.

\begin{document}
\begin{table}[h]
        \begin{tblr}{
            stretch=1.5, %adds extra vertical space (1.5 times of default) around the row
            colspec={X[c,m] *{2}{X[l,m]} X[2.5,l,m]},
            row{1} = {halign=c} %aligns the content of the first row horizontally
        }
            \toprule
            Zeitpunkt & 
            Variable & 
            Datentyp & Beschreibung
            \\ 
            \midrule
            \SetCell[r=7]{c}t0, t1, t2 
            & 
            Alter & 
            Numerisch & 
            Alter eines Studenten in Jahren 
            \\ 
            & 
            Geschlecht & 
            Nominal & 
            Geschlecht eines Studenten              
            \\ 
            & 
            HzbArt & 
            Nominal & 
            Art der HZB 
            \\ 
            & 
            HzbNote & 
            Numerisch & 
            Notendurchschnitt der HZB 
            \\
            & 
            Ang\_Abschluss & 
            Nominal & 
            Bachelor oder Master 
            \\
            & 
            Studiengang & 
            Nominal & 
            Studiengang eines Studenten 
            \\
            & Bildungsland 
            & Nominal 
            & Bildungsinländer oder Bildungsausländer  
            \\
            \midrule
            \SetCell[r=2]{c} t1, t2   
             & 
             DN\_Sem\_1 & 
             Numerisch & 
             Notendurchschnitt nach dem ersten Semester 
             \\
             & 
             NB\_Pruef\_ Sem\_1 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester  
             \\
             \midrule
            \SetCell[r=2]{c}t2 
            & 
            DN\_Sem\_2 & 
            Numerisch & 
            Notendurchschnitt nach dem zweiten Semester 
            \\
            &
             NB\_Pruef\_ Sem\_2 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester     
             \\
             \midrule
             t0, t1, t2 & 
             Abbrecher & 
             Nominal & 
             Studienabbrecher oder Nicht-Studienabbrecher 
             \\ 
             \bottomrule
        \end{tblr}%
\end{table}
\end{document}

enter image description here

Addendum 2: Without using tabularray, it's also possible to center the multirow cells. As I commented, to center vertically, you will need to manually tweak the no of rows (first) argument. And to center the multirow contents horizontally, you can insert it in a multicolumn command.

\documentclass[11pt,a4paper]{article}

\usepackage{array}
\usepackage{multirow}
\usepackage{ragged2e}
\usepackage{calc}
\usepackage{booktabs}

\newcolumntype{M}[1]{>{\RaggedRight}m{#1}}

\begin{document}
\begin{table}[h]
        \renewcommand{\arraystretch}{1.5}
        \begin{tabular}
                {M{0.16\textwidth-2\tabcolsep - 1.25\arrayrulewidth}
                M{0.24\textwidth-2\tabcolsep - 1.25\arrayrulewidth}
                M{0.18\textwidth-2\tabcolsep - 1.25\arrayrulewidth}
                M{0.44\textwidth-2\tabcolsep - 1.25\arrayrulewidth}}
            \toprule
            \multicolumn{1}{c}{Zeitpunkt} &
            \multicolumn{1}{c}{Variable}  &
            \multicolumn{1}{c}{Datentyp}  &
            \multicolumn{1}{c}{Beschreibung}
            \\\midrule
            \multicolumn{1}{c}{\multirow{8}{*}{t0, t1, t2}} 
            & 
            Alter & 
            Numerisch & 
            Alter eines Studenten in Jahren 
            \\
            & 
            Geschlecht & 
            Nominal & 
            Geschlecht eines Studenten                                    
            \\ 
            & 
            HzbArt & 
            Nominal & 
            Art der HZB 
            \\ 
            & 
            HzbNote & 
            Numerisch & 
            Notendurchschnitt der HZB 
            \\ 
            & 
            Ang\_Abschluss & 
            Nominal & 
            Bachelor oder Master 
            \\ 
            & 
            Studiengang & 
            Nominal & 
            Studiengang eines Studenten 
            \\ 
            & Bildungsland 
            & Nominal 
            & Bildungsinländer oder Bildungsausländer  
            \\ 
            \midrule
            \multicolumn{1}{c}{\multirow{3.3}{*}{t1, t2}}
            & 
             DN\_Sem\_1 & 
             Numerisch & 
             Notendurchschnitt nach dem ersten Semester         
             \\ 
             & 
             NB\_Pruef\_ Sem\_1 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem ersten Semester  
             \\ 
            \midrule
            \multicolumn{1}{c}{\multirow{3.3}{*}{t2}}
            & 
            DN\_Sem\_2 & 
            Numerisch & 
            Notendurchschnitt nach dem zweiten Semester 
            \\ 
            &
             NB\_Pruef\_ Sem\_2 & 
             Numerisch & 
             Anzahl der nicht bestandenen Prüfungen nach dem zweiten Semester     
             \\ 
             \midrule
             t0, t1, t2 & 
             Abbrecher & 
             Nominal & 
             Studienabbrecher oder Nicht-Studienabbrecher 
             \\ 
             \bottomrule
        \end{tabular}%
\end{table}
\end{document}

enter image description here