I created the following 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:
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):
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:
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. TheSetCell
macro is the equivalent of both themulticolumn
and themultirow
, in the optional argumentr
is for the no of rows to be included (c
is for columns). Thehlines
andvlines
option tells to set horizontal and vertical lines around every cell and they automatically take care of multirow and multicolumn (if there is any). TheX
column type is equivalent to thetabularx
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.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.
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 amulticolumn
command.