5

I'm trying to tex a table with alternating rows. It's working but there is something like a non-colored gap/spacing between the rows, so it looks very weird:

enter image description here

Here is the code for the example above:

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}



\begin{document}

\begin{table}[htbp]
  \caption{My Title}
  \rowcolors{2}{gray!20}{white}
  \setlength{\tabcolsep}{0.2cm}
  \centering\vspace{3pt}
  \begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
    \textbf{Type} & \textbf{Name} \\\midrule
    Animal & Monkey \\\midrule
    Animal & Lion \\\midrule
    Fruit & Apple \\
    Fruit & Banana \\\bottomrule
  \end{tabular}
\end{table}
\end{document}

Have anyone an idea how to solve this without many changes referring the packages/markup? Are there some parameter to adjust?

Thanks a lot & best regards,

Felix

  • Providing both horizontal rules and alternating row colors is quite redundant and doesn't contribute to the table's readability. If you want to keep the alternating row colors, consider dispensing with all interior \midrule statements. – Mico Oct 27 '15 at 10:12
  • The main feature of the booktabs package is to add vertical space around horizontal rules. A you see here.... – David Carlisle Oct 27 '15 at 10:16
  • The reason for using both horizontal rules and alternating row colors is, that I wanna improve the readability (row colors) and grouping some of these rows (lines). – fheck Oct 27 '15 at 10:20
4

Use \hline instead of \midrule:

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}



\begin{document}

\begin{table}[htbp]
  \caption{My Title}
  \rowcolors{2}{gray!20}{white}
  \setlength{\tabcolsep}{0.2cm}
  \centering\vspace{3pt}
  \begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\hline
    \textbf{Type} & \textbf{Name} \\
    \hline
    Animal & Monkey \\
    \hline
    Animal & Lion \\
    \hline
    Fruit & Apple \\
    \hline
    Fruit & Banana \\
    \hline
  \end{tabular}
\end{table}
\end{document}

output:

enter image description here

  • Thanks a lot! That's what I was looking for! Do you know a possibility to combine \hline with higher rows? O:-) – fheck Oct 27 '15 at 10:24
  • 1
    An hackish solution should be : \vspace{1mm}Type\vspace{1mm} for each cell. – Romain Picot Oct 27 '15 at 10:28
  • Have \renewcommand{\arraystretch}{1.2} any negative side effects? – fheck Oct 27 '15 at 10:34
  • @fheck I don't think – Romain Picot Oct 27 '15 at 10:39
4

It is due to the fact the booktabs introduces the \aboverulesep and \belowrulesep before and after a rule (\toprule, \bottomrule and \midrule etc). The \rowcolors is not painting this space with the colors so we get the gap. This can be prevented by drawing a \hrule of height \aboverulesep above and \belowrulesep below with the same color as that of the row.

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\newcommand*{\belowrulesepcolor}[1]{%
  \noalign{%
    \vspace{-\belowrulesep}
    \bgroup
      \color{#1}%
      \hrule height\belowrulesep
    \egroup
  }%
}
\newcommand*{\aboverulesepcolor}[1]{%
  \noalign{%
    \bgroup
      \color{#1}%
      \hrule height\aboverulesep
    \egroup
    \vspace{-\aboverulesep}
  }%
}



\begin{document}

\begin{table}[htbp]
  \caption{My Title}
  \rowcolors{2}{gray!20}{white}
  \setlength{\tabcolsep}{0.2cm}
  \centering\vspace{3pt}
  \begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
    \textbf{Type} & \textbf{Name} \\\midrule
    Animal & Monkey \\\midrule
    \belowrulesepcolor{gray!20}
    Animal & Lion \\
    \aboverulesepcolor{gray!20}\midrule
    Fruit & Apple \\
%    \belowrulesepcolor{gray!20}   %% not needed since there is no midrule.
    Fruit & Banana \\\aboverulesepcolor{gray!20}
    \bottomrule
  \end{tabular}
\end{table}
\end{document}

enter image description here

But better way is to ditch those \midrules since the rows are highlighted with colours

  • thanks for your effort! Think I'll use the standard \hline instead und try additionally to increase the row height. – fheck Oct 27 '15 at 10:30
0

Here the code of the solution I've finally chosen:

% !TEX TS-program = pdflatex
% !TEX encoding = UTF-8 Unicode

\documentclass[a4paper, 11pt]{article}

\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{booktabs}
\usepackage[table]{xcolor}

\setlength{\aboverulesep}{0pt}
\setlength{\belowrulesep}{0pt}
\renewcommand*{\arraystretch}{1.2}

\begin{document}

\begin{table}[htbp]
  \caption{My Title}
  \rowcolors{2}{gray!20}{white}
  \setlength{\tabcolsep}{0.2cm}
  \centering\vspace{3pt}
  \begin{tabular}{p{2.5cm}>{\raggedright\arraybackslash}p{2.5cm}}\toprule
    \textbf{Type} & \textbf{Name} \\\midrule
    Animal & Monkey \\\midrule
    Animal & Lion \\\midrule
    Fruit & Apple \\
    Fruit & Banana \\\bottomrule
  \end{tabular}
\end{table}
\end{document}

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.