9

I'm trying to format correctly the following table:

\documentclass[a4paper,twoside]{report}
\usepackage{array,colortbl,multirow,multicol,booktabs,ctable}
\newcommand{\hdrule}{\midrule[\heavyrulewidth]}
\usepackage[english]{babel}
\usepackage{xunicode}
\usepackage[no-math]{fontspec}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{ccccccc>{\itshape}r}
\toprule
a & b & c & d & e & f & g & 1 \\ \hdrule
\multirow{4}{*}{A} & \multirow{4}{*}{B} & \multirow{4}{*}{C} & \multirow{4}{*}{D} & \multirow{2}{*}{E} & F & G & 2 \\ \cmidrule{6-8}
&&&&& H & I & 3 \\ \cmidrule{5-8}
&&&& J & K & L & 4 \\ \cmidrule{5-8}
&&&& M & N & --- & 5 \\ \hdrule
--- & --- & --- & --- & O & P & Q & 6 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}

I use the package booktabs for the horizontal rules, and it seems that it messes up the vertical centering as calculated by multirow. For instance, A B C and D should be aligned with the rule separating lines 2 and 3, and E should be aligned with the rule separating lines 1 and 2. Instead, it is slightly higher.

Is there any way around this?

2
  • Please always post complete code and not just snippets. Where does \hdrule come from? Aug 10, 2012 at 2:30
  • Sorry about that, I've edited with a working example. It includes the definition of \hdrule (which is a big \midrule)
    – Arnaud
    Aug 10, 2012 at 2:35

3 Answers 3

8

For fine tunning the vertical position, you can use the second optional argument of \multirow:

\documentclass[a4paper,twoside]{report}
\usepackage{array,colortbl,multirow,multicol,booktabs,ctable}
\newcommand{\hdrule}{\midrule[\heavyrulewidth]}
\usepackage[english]{babel}
\usepackage{xunicode}
\usepackage[no-math]{fontspec}
%\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\begin{document}
\begin{table}[ht]
\centering
\begin{tabular}{ccccccc>{\itshape}r}
\toprule
a & b & c & d & e & f & g & 0 \\ \hdrule
\multirow{5}{*}[-2pt]{A} & \multirow{5}{*}[-2pt]{B} & \multirow{5}{*}[-2pt]{C} & \multirow{5}{*}[-2pt]{D} & \multirow{2}{*}[-3pt]{E} & F & G & 1 \\ \cmidrule{6-8}
&&&&& H & I & 2 \\ \cmidrule{5-8}
&&&& J & K & L & 3 \\ \cmidrule{5-8}
&&&& M & N & --- & 4 \\ \hdrule
--- & --- & --- & --- & O & P & Q & 5 \\ \bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

I commented out the line \setmainfont just because I don't have that font.

2
  • Yep, that does the trick, thanks! Just for information, I suppose there is no simple way to do this automatically?
    – Arnaud
    Aug 10, 2012 at 2:49
  • 1
    @arnaudrg in this case, I don't think there's an automatic way. Aug 10, 2012 at 2:54
1

You can do what you want with {NiceTabular} of nicematrix (≥ 4.0, 2020-05-08). You don't have any adjustments to do because nicematrix takes into account the real height of the rows whereas multirow computes only with a standard value of \baselineskip.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{booktabs}

\newcommand{\hdrule}{\midrule[\heavyrulewidth]}

\begin{document}
\begin{table}[ht]
\centering
\begin{NiceTabular}{*{7}{C}>{\itshape}R}
\toprule
a & b & c & d & e & f & g & 1 \\ \hdrule
\Block{4-1}{A} & \Block{4-1}{B} & \Block{4-1}{C} & \Block{4-1}{D} & \Block{2-1}{E} & F & G & 2 \\ \cmidrule{6-8}
&&&&& H & I & 3 \\ \cmidrule{5-8}
&&&& J & K & L & 4 \\ \cmidrule{5-8}
&&&& M & N & --- & 5 \\ \hdrule
--- & --- & --- & --- & O & P & Q & 6 \\ \bottomrule
\end{NiceTabular}
\end{table}
\end{document}

Result of the above code

1
  • In this answer, since version 5.0 of nicematrix one must write *{7}{c} instead of *{7}{C} in the preamble of {NiceTabular} (but there is an option for backward compatibility). Jul 18, 2020 at 20:47
0

Additional remark: you \usepackage{ctable}, but you don't use it. Here is an alternative and cleaner way to do this (you don't need array, colortbl, multicol, booktabs, babel, and xunicode here):

%!xelatex
\documentclass[a5paper,twoside]{report}
\usepackage{multirow,ctable}
\usepackage[no-math]{fontspec}
\setmainfont[Ligatures=TeX]{TeX Gyre Pagella}
\begin{document}

\ctable[pos = ht,caption = ctable version]{ccccccc>{\itshape}r}{}{\FL
  a & b & c & d & e & f & g & 0 \ML
  \multirow{5}{*}[-2pt]{A} & \multirow{5}{*}[-2pt]{B} & \multirow{5}{*}[-2pt]{C} & \multirow{5}{*}[-2pt]{D} & \multirow{2}{*}[-3pt]{E} & F & G & 1 \NN \cmidrule{6-8}
  &&&&& H & I & 2 \NN \cmidrule{5-8}
  &&&& J & K & L & 3 \NN \cmidrule{5-8}
  &&&& M & N & --- & 4 \ML[\heavyrulewidth]
  --- & --- & --- & --- & O & P & Q & 5 \LL
}

\end{document}

You must log in to answer this question.

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