4

I would like to color the entirety of the row "Trail 1" and "Trail 3", other than those words themselves (ie just the numbers, but the coloring runs from the \midrule to the next). This would also remove the tiny white spaces in between, which I am not sure how to remove. How would I do so?

Also, is it possible to add a legend to this to signify colors? I know that Pgfplots does this, but I do not know how to incorporate it due to the difference between tikzpicture and tabular (correct me if I am wrong, I am new to this community).

Here is the runnable code:

\documentclass[11pt]{scrartcl} \usepackage[utf8]{inputenc} \usepackage[sexy]{evan} \usepackage{booktabs} \usepackage{siunitx} \usepackage[table]{xcolor} \newcommand{\mycc}{\cellcolor{lightgray}} \begin{document} \begin{center} \begin{tabular}{SSSSSSS} \toprule {$m$} & {$2$m} & {$4$m} & {$6$m} & {$8$m} & {$10$m} & {$12$m} \\ \midrule \text{Trail 1} & \mycc 1.48 & \mycc 2.76 & \mycc 4.02 & \mycc 5.22 & \mycc 6.43 & \mycc 7.76 \\ \midrule \text{Trail 2} & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ \midrule \text{Trail 3} & \mycc 1.53 & \mycc 2.94 & \mycc 4.53 & \mycc 5.96 & \mycc 7.36 & \mycc 8.83 \\ \midrule \text{Trail 4} & 1.80 & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ \bottomrule \end{tabular} \end{center} \end{document} 

I am not sure what to do, so any help who be greatly appreciated! Also, I am not sure why the data values are not aligned, so if that could be fixed as well, that would be great!

EDIT: Here are some of the sources for where the code comes from:

enter image description here

Tabling Link

Coloring Link

1
  • 2
    Incidentally, the code you posted doesn't generate the screenshot shown above. How should the s symbols (seconds?) come in? Commented Sep 17, 2020 at 5:13

4 Answers 4

4

By default booktabs and row colours do not play well together. You can work around this by adding the following to your preamble:

\addtolength{\extrarowheight}{\belowrulesep} \aboverulesep=0pt \belowrulesep=0pt 

For the legend I would just throw what you want into a tabular environment inside a minipage environment, which allows to you achieve something like this:

enter image description here

You will probably need to adjust this "legend" to suit your tastes.

This fixes the problem with the row colours not filling the full vertical size of the rows, which the OP does not ask about, but it does not fix the faint line between the columns, which is what the OP is actually asking about. This faint gap is ihmo a bug. The easiest way to fix this is to add @{} after each column specifier, which is essentially adding "nothing" between the columns but rather than doing nothing this actually removes a small amount of space and, as a result, it removes the thin faint white line between the columns.

With the new fix the output is:

enter image description here

In particular, the faint lines between the columns have now gone.

Finally, in terms of the row colouring, perhaps this is a result of a simplification of your real code for the MWE, but rather than adding \mycc to each of the cells that you want to colour it is more efficient to set the row colour and then use \cellcolor to change the background colour of just the cell in the first. In fact, using a macro like

\newcommand\colorrow{\rowcolor{lightgray}\cellcolor{white}} 

you can to this with just one command per coloured row.

Here is the updated code:

\documentclass[11pt]{scrartcl} \usepackage[utf8]{inputenc} %\usepackage[sexy]{evan} \usepackage{booktabs} \usepackage{siunitx} \usepackage[table]{xcolor} \newcommand{\mycc}{\cellcolor{lightgray}} \newcommand\colorrow{\rowcolor{lightgray}\cellcolor{white}} \addtolength{\extrarowheight}{\belowrulesep} \aboverulesep=0pt \belowrulesep=0pt \begin{document} \begin{center} \begin{tabular}{*6{S@{}}S} \toprule {$m$} & {$2$m} & {$4$m} & {$6$m} & {$8$m} & {$10$m} & {$12$m} \\ \midrule \colorrow \text{Trail 1} & 1.48 & 2.76 & 4.02 & 5.22 & 6.43 & 7.76 \\ \midrule \text{Trail 2} & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ \midrule \colorrow \text{Trail 3} & 1.53 & 2.94 & 4.53 & 5.96 & 7.36 & 8.83 \\ \midrule \text{Trail 4} & 1.80 & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ \bottomrule \end{tabular} \smallskip \hfil \begin{minipage}{0.3\textwidth} \textbf{Legend}\\ \begin{tabular}{ll} \mycc This colour & stuff \end{tabular} \end{minipage} \end{center} \end{document} 
6
  • 1
    +1. However, in the second screenshot, the 1 and 3 in Trail 1 and Trail 3 appear to have gone AWOL. :-( Fortunately, this issue disappears if one replaces \begin{tabular}{*6{S@{}}S} with \begin{tabular}{@{} *7{S[table-format=1.2]} } or, better still, \begin{tabular}{@{} l *6{S[table-format=1.2]} }. (I can see no reason for using S for the first column.) Commented Sep 17, 2020 at 5:49
  • 1
    I don't think @{} makes the faint line more or less likely it just changes when it occurs. colortbl can not colour a row with a single panel so you always get adjacent panels specified as touching but the pdf renderer needs to snap to pixel boundaries and so can end up with a pixel gap between them. The only foolproof way to avoid that is to draw a single panel behind the row but colortbl can not do that. Commented Sep 17, 2020 at 10:26
  • The faint white line appear only in some PDF viewers (for instance the PDF viewers based upon MuPDF of Artifex Software such as SumatraPDF). They appear when there are adjacent colored panels in the PDF stream. The environment {NiceTabular} of nicematrix provides tools to avoid that problem. Commented Sep 17, 2020 at 12:41
  • @F.Pantigny I keep meaning to check but as you are here... does it do that by making a single panel as I suggest above or does it just extend the panels a bit so they overlap if they have the same colour so avoid gaps? Commented Sep 17, 2020 at 15:18
  • @David Carlisle: I assume that your question is related to {NiceTabular} of nicematrix (presented in another answer). It's a single panel drawn before the construction of the array using informations (in particular PGF/Tikz nodes) written in the aux file (and, so, it needs several compilations). Commented Sep 17, 2020 at 15:25
4

Using horizontal lines, whether they're drawn by \hline or by \midrule, and colored (or shaded) rows of cells represents a mixture of two visual metaphores that can backfire quite easily. Don't do it. If you drop (or comment out) the \midrule directives, the readability of the table increases immediately -- and you needn't spend time worrying about how to get rid of the pesky whitespace that surrounds the horizontal rules.

A separate comment: Instead of issuing 2*6=12 separate \cellcolor directives, I'd issue 2 \rowcolor{lightgray} directives for two of the rows and 2 \cellcolor{white} directives for the first cells in these two rows, to override the effect of the \rowcolor directives for these cells.

enter image description here

\documentclass[11pt]{scrartcl} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} %\usepackage[sexy]{evan} \usepackage{booktabs,siunitx} \usepackage[table]{xcolor} \begin{document} \begin{center} \renewcommand\arraystretch{1.25} % for a more open "look" \begin{tabular}{@{} l *{6}{S[table-format=1.2]} } \toprule & {2m} & {4m} & {6m} & {8m} & {10m} & {12m} \\ %\midrule \rowcolor{lightgray}% \cellcolor{white}% Trail 1 & 1.48 & 2.76 & 4.02 & 5.22 & 6.43 & 7.76 \\ %\midrule Trail 2 & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ %\midrule \rowcolor{lightgray}% \cellcolor{white}% Trail 3 & 1.53 & 2.94 & 4.53 & 5.96 & 7.36 & 8.83 \\ %\midrule Trail 4 & 1.80 & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ \bottomrule \end{tabular} \end{center} \end{document} 
3
  • 2
    +1 I prefer your solution:) Commented Sep 17, 2020 at 6:34
  • Wow! Thanks! Sorry for the stupid question, but how do you change the size of the data table? I would like this to be bigger to fit most of the page. Thanks! Commented Sep 17, 2020 at 12:31
  • @djdumpling - I suppose you could run \setlength\tabcolsep{18pt} before \begin{tabular}. (In most document classes, the default value of \tabcolsep is 6pt.) Commented Sep 17, 2020 at 13:41
2

A solution with TiKz matrix -- the legend is inscribed in a blank row at the end

enter image description here

\documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \tikzset{ table/.style={ matrix of nodes, nodes={ rectangle, draw=none, align=center }, minimum height=1.5em, every even row/.style={ nodes={fill=black!30} }, column 1/.style={ nodes={text width=4em,font=\bfseries} }, } } \begin{tikzpicture} \matrix (first) [table,text width=4em] { {$m$} & {$2$m} & {$4$m} & {$6$m} & {$8$m} & {$10$m} & {$12$m} \\ |[fill=white]|{Trail 1} & 1.48 & 2.76 & 4.02 & 5.22 & 6.43 & 7.76 \\ {Trail 2} & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ |[fill=white]|{Trail 3} & 1.53 & 2.94 & 4.53 & 5.96 & 7.36 & 8.83 \\ {Trail 4} & 1.80 & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ &|[fill=white]|{}&&&&&\\ &&|[fill=black!30]|Legend&{defintion}&&&\\ }; \end{tikzpicture} \end{document} 

EDIT A solution with the excellent nicematrix package

\documentclass{article} \usepackage{nicematrix} \begin{document} \begin{NiceTabular}{rrrrrrr}[code-before = \rowcolors{1}{white}{blue!15} \columncolor{white}{1}] {$m$} & {$2$m} & {$4$m} & {$6$m} & {$8$m} & {$10$m} & {$12$m} \\ \text{Trail 1} & 1.48 & 2.76 & 4.02 & 5.22 & 6.43 & 7.76 \\ \text{Trail 2} & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ \text{Trail 3} & 1.53 & 2.94 & 4.53 & 5.96 & 7.36 & 8.83 \\ \text{Trail 4} & 1.80 & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ \end{NiceTabular} \end{document} 

and the result

enter image description here

The legend can be placed in the sixth row

2

If you want colored rows compatible with booktabs, you may use the environment {NiceTabular} of nicematrix.

\documentclass[11pt]{scrartcl} \usepackage{booktabs} \usepackage{siunitx} \usepackage{xcolor} \usepackage{nicematrix} \begin{document} \begin{center} \begin{NiceTabular}{SSSSSSS}[colortbl-like] \toprule {$m$} & {$2$m} & {$4$m} & {$6$m} & {$8$m} & {$10$m} & {$12$m} \\ \midrule \text{Trail 1} & \rowcolor{lightgray}1.48 & 2.76 & 4.02 & 5.22 & 6.43 & 7.76 \\ \midrule \text{Trail 2} & 1.53 & 3.21 & 4.76 & 6.32 & 7.81 & 9.47 \\ \midrule \text{Trail 3} & \rowcolor{lightgray}1.53 & 2.94 & 4.53 & 5.96 & 7.36 & 8.83 \\ \midrule \text{Trail 4} & & 3.46 & 4.94 & 6.49 & 8.20 & 9.85 \\ \bottomrule \end{NiceTabular} \end{center} \end{document} 

However, you need several compilations.

Output of the above code

You must log in to answer this question.