2

Following the idea in this post: http://www.benjaminbiegel.com/a-latex-table-that-actually-looks-nice-2-106.html I was able to create this table:

enter image description here

It changes the rowcolor for every row, and I need it to change when a new word (in column 2) is entered. So the two rows of "joya" would be 'green' and the seven rows of "juicio" would be light-brown. Is there a "counter" that changes the row color when the word-item (in second column) changes? The code I use is this:

\documentclass{article}
\usepackage[left=1.5cm,right=1.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{txfonts}
\usepackage{multirow}

\definecolor{TableBorder}{RGB} {255,211,32}
\definecolor{TableOdd}   {RGB} {204,153,0}
\definecolor{TableEven}  {RGB} {168,166,0}
\definecolor{colortexto} {RGB} {5,36,62}
%
\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}
%
\begin{document}

\rowcolors{1}{TableOdd}{TableEven}          

\begin{center}
\begin{tabular}{
!{\color{TableBorder}\VRule[1pt]}           
>{\bf\color{colortexto}}l                   
!{\color{TableBorder}\vline}
>{\bf\color{colortexto}}l                   
!{\color{TableBorder}\vline}
>{\bf\color{colortexto}}l                   
!{\color{TableBorder}\vline}
>{\bf\color{colortexto}}l                   
!{\color{TableBorder}\vline}>{\bf\color{colortexto}}l 
!{\color{TableBorder}\vline}>{\bf\color{colortexto}}l
!{\color{TableBorder}\VRule[1pt]}}          

\arrayrulecolor{TableBorder}                
\specialrule{1pt}{0pt}{0pt}                 
\rowcolor{TableOdd}                         
Rating&concepto&subconcepto&página&idea&capítulo\\ 
\specialrule{2pt}{0pt}{0pt}                 
$\varheartsuit$ $\varheartsuit$&jardín&&337&mujer buena como un jardín&XXXIII – I\\
\hline  
$\varheartsuit$ $\varheartsuit$&jaula&&487&voy encantado por envidia&XLVII – I\\
\hline  
&joya&&393&paz (la)&XXXVII – I\\       
&&&519&que si se pierde …&LI – I\\
\hline     
&juicio&&1087&con más juicio discurría&LXXI – II\\
&&&493&confuso juicio del vulgo& XLVIII – I\\
&&&334&discurso (sin) y temerario&XXXIII – I\\
$\varheartsuit$&&&572&gran juicio para componer historias& III – II\\
&&&512&justicia (buen juicio para administrar justicia)&L – I\\
&&&1100&libre (tengo juicio ya libre)& LXXIV – II\\
&&&554&volverme mi juicio&I – II\\
\specialrule{1pt}{0pt}{0pt}
\end{tabular}
\end{center}
\end{document}

Any idea will be of great help. Thanks.

  • The \rowcolor has to be set within the first column. So, if you're willing to change the layout then it's probably doable. However, for such a small table, I'd suggest just setting the \rowcolor manually. – Werner Sep 10 '17 at 4:46
  • I put this small example just to show my need, but I really have 28 tables, some of which are of little more than 300 rows (93 pages), so "manually" would be a nightmare... – Antonio Ortiz Sep 10 '17 at 13:32
2

You need to set \rowcolor as the first item in a row, so you have to input the first two row items together.

\documentclass{article}
\usepackage[left=1.5cm,right=1.5cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[table]{xcolor}
\usepackage{booktabs}
\usepackage{newtxtext,newtxmath}
\usepackage{multirow}

\definecolor{TableBorder}{RGB} {255,211,32}
\definecolor{TableOdd}   {RGB} {204,153,0}
\definecolor{TableEven}  {RGB} {168,166,0}
\definecolor{colortexto} {RGB} {5,36,62}

\newcommand\VRule[1][\arrayrulewidth]{\vrule width #1}
\newcommand{\RC}[2]{%
  % #1 is the rating, #2 is the concept;
  % if #2 is empty, it is assumed to repeat the previous one
  \rowcolor{\TableEvenOrOdd}
  \if\relax\detokenize{#2}\relax
  \else
    \stepcounter{rowcolors}%
  \fi
  #1 & #2
}
\newcommand{\TableEvenOrOdd}{%
  \ifodd\value{rowcolors}TableEven\else TableOdd\fi
}
\newcounter{rowcolors}

\begin{document}

\begin{center}
\begin{tabular}{
  !{\color{TableBorder}\VRule[1pt]}           
  >{\bfseries\color{colortexto}}l                   
  !{\color{TableBorder}\vline}
  >{\bfseries\color{colortexto}}l                   
  !{\color{TableBorder}\vline}
  >{\bfseries\color{colortexto}}l                   
  !{\color{TableBorder}\vline}
  >{\bfseries\color{colortexto}}l                   
  !{\color{TableBorder}\vline}>{\bfseries\color{colortexto}}l 
  !{\color{TableBorder}\vline}>{\bfseries\color{colortexto}}l
  !{\color{TableBorder}\VRule[1pt]}}          

\arrayrulecolor{TableBorder}                
\specialrule{1pt}{0pt}{0pt}                 
\rowcolor{TableOdd}                         
Rating&concepto&subconcepto&página&idea&capítulo\\ 
\specialrule{2pt}{0pt}{0pt}                 
\RC{$\varheartsuit$ $\varheartsuit$}{jardín}&&337&mujer buena como un jardín&XXXIII – I\\
\hline  
\RC{$\varheartsuit$ $\varheartsuit$}{jaula}&&487&voy encantado por envidia&XLVII – I\\
\hline  
\RC{}{joya}&&393&paz (la)&XXXVII – I\\       
\RC{}{}&&519&que si se pierde …&LI – I\\
\hline
\RC{}{juicio}&&1087&con más juicio discurría&LXXI – II\\
\RC{}{}&&493&confuso juicio del vulgo& XLVIII – I\\
\RC{}{}&&334&discurso (sin) y temerario&XXXIII – I\\
\RC{$\varheartsuit$}{}&&572&gran juicio para componer historias& III – II\\
\RC{}{}&&512&justicia (buen juicio para administrar justicia)&L – I\\
\RC{}{}&&1100&libre (tengo juicio ya libre)& LXXIV – II\\
\RC{}{}&&554&volverme mi juicio&I – II\\
\specialrule{1pt}{0pt}{0pt}
\end{tabular}
\end{center}
\end{document}

A counter steps only if the second argument to \RC is not empty.

enter image description here

I changed txfonts into the better combination of newtxtext and newtxmath; also \bf, which has been deprecated for more than 20 years, should be \bfseries.

  • Just what I needed! Works wonderfully! Thank you egreg :-) – Antonio Ortiz Sep 10 '17 at 15:51

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.