4

Even if frowned upon, I'd like to keep the vertical lines. Nevertheless, in the following MWE i get a misalignment in the heading. Once towards the top, once towards the bottom - I added a line in the picture for clarification. Increasing the cell width to keep the heading in one line in the first example does not solve the problem. Question: How do I achieve vertical alignment in tabu-environment across all columns?

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{tabu}
\usepackage{multirow}
\usepackage{array}

\begin{document}
\begin{table}[!htb]
    \vspace{2ex}
    \centering
    {\tabulinesep=1.2mm
        \begin{tabu}spread 0pt{|X[1cm]|X[-2.5cm]|X[-2cm]|}\hline\hline
            \textbf{Jahr}&\textbf{Plätze insgesamt}&\textbf{Plätze besetzt}\\ \hline
            2010&579.564&559.959\\ \hline
            \hline
        \end{tabu}}
    \end{table} 


\begin{table}[!htb]
    \vspace{2ex}
    \centering
    {\tabulinesep=1.2mm
    \begin{tabu}spread 0pt{|X[1cm]|X[1cm]|X[-1cm]|X[-2cm]|}\hline\hline
    \textbf{Jahrgang}&\textbf{Hauptschule}&\textbf{Realschule}&\textbf{Studienberechtigung}\\ \hline
    2010 &  4 \% &  67 \% & 29 \% \\ \hline
            \hline
        \end{tabu}}
    \end{table} 
\end{document}

enter image description here

EDIT (As I can not yet comment): 1) The link provided by Salim Bou deals with the problem of alignment in multi-line X[m]-tables. I ran across that solution, but the problem outlined here occures even in one-lined (headings of) tables.

2) The solution provided by gernot works (!) and is quickly to implement but has a drawback I'd like to point out: In my MWE Jahr needs a \dstrut as well, as soon as the table is expanded. Furthermore, I had to use several iterations to identify each column in need of a downward extender - this quickly gets out of hand in a longer document.

3) The solution of egreg has the additional benefit of allowing to be placed in a \newenvironment to fix this problem on the fly.

%Mytable with three arguments
%#1 - Label
%#2 - Ref
%#3 - dynamic number of columns

%example for a table with two columns: 
%\begin{mt}
% {Number of things in this case over the years}{tab:things_yrs}{2}
%1&2\\ \hline
%3&4\\ \hline
%\end{mt}

\newenvironment{mt}[4]{
\begin{table}[!htb]
\vspace{2ex}
\caption[#1]{#2}
\label{#3}
\centering
\tabulinesep=1.2mm
\begin{tabu}{|*#4{X[-1cm]<{\unskip\strut}|}}\hline\hline}   
{\hline\hline
\end{tabu}
\end{table}}
1

2 Answers 2

2

It's a known problem: tabu doesn't add a \strut at the end of a cell in an X column. The second table doesn't suffer from the issue, because there is a \strut at the beginning.

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabu}
\usepackage{multirow}
\usepackage{array}

\begin{document}
\begin{table}[!htb]
\centering
\tabulinesep=1.2mm

\begin{tabu} spread 0pt{
  |X[1cm]<{\unskip\strut}|
   X[-2.5cm]<{\unskip\strut}|
   X[-2cm]<{\unskip\strut}|
}
\hline
\textbf{Jahr} & \textbf{Plätze insgesamt} & \textbf{Plätze besetzt} \\
\hline
2010          & 579.564                   & 559.959                 \\
\hline
\end{tabu}

\end{table} 


\begin{table}[!htb]
\centering
\tabulinesep=1.2mm

\begin{tabu} spread 0pt{|X[1cm]|X[1cm]|X[-1cm]|X[-2cm]|}
\hline
\textbf{Jahrgang} & \textbf{Hauptschule} & \textbf{Realschule} & \textbf{Studienberechtigung}\\
\hline
2010              & 4\%                  & 67\%                & 29\% \\
\hline
\end{tabu}

\end{table} 

\end{document}

enter image description here

2

The differences in vertical alignment are due to the fact that insgesamt extends below the baseline, while besetzt does not. So the height of Plätze insgesamt is slightly greater than the one of Plätze besetzt. The same for Realschule (has no downward extender).

The simplest solution is to define an invisble downward extender

\newcommand\dstrut{\vphantom{g}}

and to add it after besetzt and Realschule.

enter image description here

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{tabu}
\usepackage{multirow}
\usepackage{array}
\newcommand\dstrut{\vphantom{g}}

\begin{document}
\begin{table}[!htb]
    \vspace{2ex}
    \centering
    {\tabulinesep=1.2mm
        \begin{tabu}spread 0pt{|X[1cm]|X[-2.5cm]|X[-2cm]|}\hline\hline
            \textbf{Jahr}&\textbf{Plätze insgesamt}&\textbf{Plätze besetzt\dstrut}\\ \hline
            2010&579.564&559.959\\ \hline
            \hline
        \end{tabu}}
    \end{table} 


\begin{table}[!htb]
    \vspace{2ex}
    \centering
    {\tabulinesep=1.2mm
    \begin{tabu}spread 0pt{|X[1cm]|X[1cm]|X[-1cm]|X[-2cm]|}\hline\hline
    \textbf{Jahrgang}&\textbf{Hauptschule}&\textbf{Realschule\dstrut}&\textbf{Studienberechtigung}\\ \hline
    2010 &  4 \% &  67 \% & 29 \% \\ \hline
            \hline
        \end{tabu}}
    \end{table} 
\end{document}
1
  • tabu should have inserted these struts itself, but it has been removed according to the doc to avoid a problem. ("Modification of \@finalstrut (“null-rule” added) to avoid problems with \columncolor.") I have had more annoying effects because of this change. Jan 5, 2017 at 20:06

You must log in to answer this question.

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