37

I want to vertically align (best would be to center) the text ("TEST") in the left (yellow) column of the following table. It works fine with multirow when every row has exactly the hight of one line:

\documentclass[ngerman,a4paper,12pt,pdftex]{report}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{colortbl}

\begin{document}
\begin{longtable}{|p{0.025\textwidth}|p{0.775\textwidth}|>{\centering arraybackslash}m{0.2\textwidth}|}
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline 
\endfirsthead
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline
\endhead
\cellcolor{yellow}& short text & 1 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 2 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 3 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 4 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 5 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 6 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 7 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 8\\
\cline{2-3} 
\cellcolor{yellow} & short text & 9 \\
\cline{2-3} 
\multirow{-10}{*}{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}  & short text &  10 \\
\hline
\end{longtable}

\end{document}

enter image description here

When there is a long text in one of the cells, the text of the left cell ("TEST") is aligned somewhere:

\documentclass[ngerman,a4paper,12pt,pdftex]{report}
\usepackage[utf8]{inputenc}

\usepackage{rotating}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{colortbl}

\begin{document}
\begin{longtable}{|p{0.025\textwidth}|p{0.775\textwidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline 
\endfirsthead
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline
\endhead
\cellcolor{yellow}& short text & 1 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 2 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 3 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 4 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 5 \\
\cline{2-3} 
\cellcolor{yellow} & very long text ....... ......... ........ ....... ....... ....... ........ ............. ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... needs lots of lines & 6 \\
\cline{2-3} 
\cellcolor{yellow} & short text & 7 \\
\cline{2-3} 
\cellcolor{yellow} & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 8\\
\cline{2-3} 
\cellcolor{yellow} & short text & 9 \\
\cline{2-3} 
\multirow{-10}{*}{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}  & short text &  10 \\
\hline
\end{longtable}

\end{document}

enter image description here

How can I align the text of the left column in the second example?

4
  • Is your tabular really a longtable which could break across pages? If yes what should happen if the page break is at the places of TEST? Aug 10, 2012 at 8:02
  • It's a two page table which I break manually after the "TEST" section using the \newpage command
    – Philipp
    Aug 10, 2012 at 8:05
  • Here are the images of the two tables (I am not allowed to add them in the question) dropbox.com/s/z24or3i6795w3vk/test1.png and dropbox.com/s/exdfcvfw9q9dr5c/test2.png
    – Philipp
    Aug 10, 2012 at 8:10
  • 3
    In general I try to avoid multirow. I would in your case probably use tikz with [remember picture] to place nodes in the corners and then connect them. But it would be some fine tuning to get the size correct. Another way is to nest tabulars, but this could get a bit difficult due to the headers. Beside this I would much less lines ;-). Aug 10, 2012 at 9:02

5 Answers 5

40

From my experience multirow is not capable of a correct vertical alignment, if it spans multi-line cells. You can, however, adjust the alignment manually using the fixup-parameter:

% \multirow{nrows}[bigstruts]{width}[fixup]{text}
\multirow{-10}{*}[3em]{\cellcolor{yellow}\begin{sideways}TEST\end{sideways}}

Other possibilities for solving this problem are discussed in "Centering mutirow text in a table whose cells contain several lines of text" and "How can I achieve proper vertical spacing for long blocks of text within a \multirow?".

1
  • thx - it's not the solution I hoped but it seems the best way to use the fixup-parameter
    – Philipp
    Aug 14, 2012 at 9:17
2

I also solved it with a quick fix: combining a parbox with a vspace within multirow

\multirow{5}{*}{\parbox{1\linewidth}{\vspace{1cm} something something}}
2

This table is easy and straight forward to produce using cals. Nothing special, only standard commands:

\documentclass[a4paper, 11pt]{article}
\usepackage{cals, xcolor, rotating}
\usepackage[margin=2.5cm]{geometry}


\let\nc=\nullcell                                                  % Shortcuts
\let\sc=\spancontent

\begin{document}

\begin{calstable}

% Defining 3 column relativ to each other and relativ to the margins
\colwidths{{\dimexpr(\columnwidth)/1000*100\relax}
            {\dimexpr(\columnwidth)/1000*775\relax}
            {\dimexpr(\columnwidth)/1000*125\relax}
            }
% The tabular fills the text area

% Set up the tabular
\makeatletter
\def\cals@framers@width{0.4pt}   % Outside frame rules, reduce if the rule is too heavy
\def\cals@framecs@width{0.4pt}
\def\cals@bodyrs@width{0.4pt}
\cals@setpadding{Ag}
\cals@setcellprevdepth{Al}
\def\cals@cs@width{0.4pt}        % Inside rules, reduce if the rule is too heavy
\def\cals@rs@width{0.4pt}
\def\cals@bgcolor{}

\def\yellow{\ifx\cals@bgcolor\empty % Define colour on-off switch
\def\cals@bgcolor{yellow!30}
\else \def\cals@bgcolor{} \fi}

\def\tb{\ifx\cals@borderT\relax     % Top border switch (off-on)
    \def\cals@borderT{0pt}
\else \let\cals@borderT\relax\fi}

\def\bb{\ifx\cals@borderB\relax     % Botton border switch (off-on)
    \def\cals@borderB{0pt}
\else \let\cals@borderB\relax\fi}

\def\rb{\ifx\cals@borderR\relax     % Right border switch (off-on)
    \def\cals@borderR{0pt}
\else \let\cals@borderR\relax\fi}

\def\lb{\ifx\cals@borderL\relax     % Left border switch (off-on)
    \def\cals@borderL{0pt}
\else \let\cals@borderL\relax\fi}

% H1
\thead{%
\bfseries
\brow
    \yellow\nc{tlr}\yellow
    \alignL\cell{Foo}
    \alignC\cell{Bar}
\erow
\mdseries
}
% R1
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{1}
\erow
% R2 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{2}
\erow
% R3 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{3}
\erow
% R4 Body
\brow
    \yellow \nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{4}
\erow
% R5 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{5}
\erow
% R6 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{6}
\erow
% R7 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{7}
\erow
% R8 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{8}
\erow
% R9 Body
\brow
    \yellow\nc{lr}\yellow
    \alignL\cell{short text}
    \alignC\cell{9}
\erow
% R10 Body
\brow
    \yellow\nc{blr}\alignC\sc{\vfil\begin{sideways}TEST\end{sideways}}\yellow
    \alignL\cell{short text}
    \alignC\cell{10}
\erow
\makeatletter
\end{calstable}\par % \par needed to align the tabular

\end{document}

enter image description here

enter image description here

0

You can do that easily with {NiceTabular} of nicematrix. In that environment, you merge cells both vertically and horizontally with the command \Block and the content of the block is positionned at the mathematical center of the \Block. That means that vertically, you count the number of logical row and not the number of physical lines as with multirow.

\documentclass[ngerman,a4paper,12pt,pdftex]{report}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{|p{0.025\textwidth}|p{0.775\textwidth-6\tabcolsep-4\arrayrulewidth}|>{\centering\arraybackslash}m{0.2\textwidth}|}
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline 
 & \textbf{foo} & \textbf{bar} \\
\hline
\hline
\Block[fill=yellow]{10-1}{\rotate TEST}& short text & 1 \\ \Hline
 & medium text ........ ....... ....... ....  ........ ....... ....... .... 
   ........ ....... ....... ....needs two lines & 2 \\ \Hline
 & short text & 3 \\ \Hline
 & short text & 4 \\ \Hline
 & medium text ........ ....... ....... ....  ........ ....... ....... .... 
   ........ ....... ....... ....needs two lines & 5 \\ \Hline
 & very long text ....... ......... ........ ....... ....... ....... ........
    ............. ....... ........ ...... ....... ....... ........ ...... ....... 
    ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ 
    ...... ....... ....... ........ ...... ....... ....... ........ ...... 
    needs lots of lines & 6 \\ \Hline
 & short text & 7 \\
 & medium text ........ ....... ....... ....  ........ ....... ....... .... 
   ........ ....... ....... ....needs two lines & 8 \\ \Hline
 & short text & 9 \\ \Hline
 & short text &  10 \\ \Hline
\hline
\end{NiceTabular}

\end{document}

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

Output of the above code

0

A better solution with the new LaTeX3 package tabularray:

\documentclass[ngerman,a4paper,12pt,pdftex]{report}

\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{tabularray}
\usepackage{xcolor}

\begin{document}
\begin{longtblr}[
  caption = {Long Table},
]{
  colspec={|c|X[5,m,l]|X[1,m,c]|},
  rowhead = 1,
  row{1} = {font=\bfseries},
  cell{2}{1} = {r=10}{yellow7}, % multirow
}
\hline[1pt]
 & Foo & Bar \\
\hline[0.8pt]
 \begin{sideways}TEST\end{sideways} & short text & 1 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 2 \\
\cline{2-3} 
 & short text & 3 \\
\cline{2-3} 
 & short text & 4 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 5 \\
\cline{2-3} 
 & very long text ....... ......... ........ ....... ....... ....... ........ ............. ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... ....... ....... ........ ...... needs lots of lines & 6 \\
\cline{2-3} 
 & short text & 7 \\
\cline{2-3} 
 & medium text ........ ....... ....... ....  ........ ....... ....... .... ........ ....... ....... ....needs two lines & 8\\
\cline{2-3} 
 & short text & 9 \\
\cline{2-3} 
 & short text &  10 \\
\hline[1pt]
\end{longtblr}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.