4

I've got few tables created with few packages included booktabs where I use the command \rowcolors from the xcolor package. But in the last I notice that the colour is wider that the lines of the same table. Since the way to create the tables and the packages used in all are the same I don't know what could happen. This is my MWE:

\documentclass[fontsize=10pt,paper=letter,headings=big,bibliography=totoc,DIV=8]{scrbook}
\KOMAoptions{headsepline=true,draft=false}
\usepackage{typearea}
\usepackage[usenames,dvipsnames,x11names,table]{xcolor}
\usepackage[single=false,macros=false,xspace=false]{acro}
\usepackage[spanish,mexico]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[sc,osf]{mathpazo}
\usepackage[defaultsans]{opensans}
\usepackage{array,multirow,tabularx,ragged2e,booktabs}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating}
\usepackage{colortbl}
\usepackage[per-mode=symbol]{siunitx}
\DeclareSIUnit\poise{P}
\DeclareSIUnit\stokes{St}
%\DeclareSIUnit\radian{rad}
\DeclareSIUnit\dyna{dyn}

\begin{document}

\begin{table}   
\centering
\rowcolors{2}{}{lightgray!20}
\begin{tabularx}{3.4in}{@{} CS[table-format=4.4] @{}}             
\toprule
Propiedades materiales en la media & Valor \\ 
\midrule 
$c_M$      & \SI{3.0}{[\kilo\pascal]} \\
$k_{1_M}$  & \SI{2.3632}{[\kilo\pascal]} \\
$k_{2_M}$  & \num{0.8393} \\ 
$\beta_M$  & \ang{29.0} \\
$\kappa_M$ & \SI{1000}{[\mega\pascal]}\\
\midrule 
Propiedades materiales en la adventicia & Valor \\
\midrule
$c_A$      & \SI{0.3}{[\kilo\pascal]} \\
$k_{1_A}$  & \SI{0.5620}{[\kilo\pascal]} \\
$k_{2_A}$  & \num{0.7112} \\ 
$\beta_A$  & \ang{62.0} \\
$\kappa_A$ & \SI{1000}{[\mega\pascal]} \\
\bottomrule
\end{tabularx}            
\caption[Parámetros materiales de la arteria]{Parámetros materiales de la arteria en sus dos túnicas externas: media y adventicia.}
\label{taula:param}
%\setfloatalignment{t}
\end{table}

\begin{table}   
\centering
\rowcolors{2}{}{lightgray!20}
\begin{tabularx}{3.2in}{@{} lYS[table-format=4.4] @{}}
\toprule
\multicolumn{2}{c}{Propiedades materiales} & Valor \\ 
\midrule 
\multirow{2}{*}{\rotatebox{90}{{\footnotesize Sangre}}}
& $\rho_s$   & \SI{1060}{[\kilo\gram\per\metre^3]} \\
& $\mu_{s}$  & \SI{3.8e-6}{[\metre^2\per\second]} \\ 
\midrule
\rowcolor{white} \multicolumn{3}{c}{Modelo hiperelástico de Mooney-Rivlin}\\
\midrule
\multirow{6}{*}{\rotatebox{90}{{\footnotesize Arteria}}}
& $C_1$  & \SI{0.020}{[\mega\pascal]} \\
& $C_2$  & \SI{0.003}{[\mega\pascal]} \\
& $C_3$  & \SI{0.175}{[\mega\pascal]} \\
& $C_4$  & \SI{0.090}{[\mega\pascal]} \\
& $C_5$  & \SI{0}{[\mega\pascal]} \\
& $C_6$  & \SI{0}{[\mega\pascal]} \\          
\midrule 
\multicolumn{3}{c}{Modelo hiperelástico de Mooney-Rivlin}\\
\midrule
\multirow{6}{*}{\rotatebox{90}{{\footnotesize Placa}}}
& $C_1$  & \SI{-0.452}{[\mega\pascal]} \\
& $C_2$  & \SI{0.510}{[\mega\pascal]} \\
& $C_3$  & \SI{0.101}{[\mega\pascal]} \\
& $C_4$  & \SI{1.256}{[\mega\pascal]} \\
& $C_5$  & \SI{0}{[\mega\pascal]} \\
& $C_6$  & \SI{0.301}{[\mega\pascal]} \\
\bottomrule
\end{tabularx}            
\caption[Parámetros materiales de la sangre, arteria y placa]{Parámetros materiales de la sangre, arteria y placa que los definen como materiales para COMSOL.}
\label{taula:sangue}
\end{table}     
\end{document}

Both tables have the same problem, but the second one has a difficult more: I try to add a column with text rotated 90º, but I don't know how to fit it for use the same colour scheme without cover with the colour part of that rotated column... if it is possible.

Result from the MWE

Aradnix
  • 3,735
  • 2
  • 20
  • 43

2 Answers2

6
\documentclass[fontsize=10pt,paper=letter,headings=big,bibliography=totoc,DIV=8]{scrbook}
\KOMAoptions{headsepline=true,draft=false}
\usepackage{typearea}
\usepackage[usenames,dvipsnames,x11names,table]{xcolor}
\usepackage[single=false,macros=false,xspace=false]{acro}
\usepackage[spanish,mexico]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[sc,osf]{mathpazo}
\usepackage[defaultsans]{opensans}
\usepackage{array,multirow,tabularx,ragged2e,booktabs}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash\hspace{0pt}}X}
\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}
\usepackage{rotating}
\usepackage{colortbl}
\usepackage[per-mode=symbol]{siunitx}
\DeclareSIUnit\poise{P}
\DeclareSIUnit\stokes{St}
%\DeclareSIUnit\radian{rad}
\DeclareSIUnit\dyna{dyn}

\begin{document}

\begin{table}
\centering
\rowcolors{2}{}{lightgray!20}
\begin{tabularx}{3.4in}{@{\kern\tabcolsep}CS[table-format=4.4]<{\kern\tabcolsep}}
\toprule
Propiedades materiales en la media & Valor \\
\midrule
$c_M$      & \SI{3.0}{[\kilo\pascal]} \\
$k_{1_M}$  & \SI{2.3632}{[\kilo\pascal]} \\
$k_{2_M}$  & \num{0.8393} \\
$\beta_M$  & \ang{29.0} \\
$\kappa_M$ & \SI{1000}{[\mega\pascal]}\\
\midrule
Propiedades materiales en la adventicia & Valor \\
\midrule
$c_A$      & \SI{0.3}{[\kilo\pascal]} \\
$k_{1_A}$  & \SI{0.5620}{[\kilo\pascal]} \\
$k_{2_A}$  & \num{0.7112} \\
$\beta_A$  & \ang{62.0} \\
$\kappa_A$ & \SI{1000}{[\mega\pascal]} \\
\bottomrule
\end{tabularx}
\caption[Parámetros materiales de la arteria]{Parámetros materiales de la arteria en sus dos túnicas externas: media y adventicia.}
\label{taula:param}
%\setfloatalignment{t}
\end{table}

\begin{table}
\centering
\rowcolors{2}{}{lightgray!20}
\begin{tabularx}{3.2in}{@{\kern\tabcolsep} lYS[table-format=4.4]<{\kern\tabcolsep}}
\toprule
\multicolumn{2}{c}{Propiedades materiales} & Valor \\
\midrule
\cellcolor{lightgray!20}               %%% remove this if you don't want
 & $\rho_s$   & \SI{1060}{[\kilo\gram\per\metre^3]} \\
\multirow{-2}{*}{\rotatebox{90}{{\cellcolor{lightgray!20}\footnotesize Sangre}}}& $\mu_{s}$  & \SI{3.8e-6}{[\metre^2\per\second]} \\
\midrule
\rowcolor{white} \multicolumn{3}{c}{Modelo hiperelástico de Mooney-Rivlin}\\
\midrule
\cellcolor{lightgray!20}
\cellcolor{lightgray!20}& $C_1$  & \SI{0.020}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_2$  & \SI{0.003}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_3$  & \SI{0.175}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_4$  & \SI{0.090}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_5$  & \SI{0}{[\mega\pascal]} \\
\multirow{-6}{*}{\rotatebox{90}{{\footnotesize Arteria}}}& $C_6$  & \SI{0}{[\mega\pascal]} \\
\midrule
\multicolumn{3}{c}{Modelo hiperelástico de Mooney-Rivlin}\\
\midrule
\cellcolor{lightgray!20}
\cellcolor{lightgray!20}& $C_1$  & \SI{-0.452}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_2$  & \SI{0.510}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_3$  & \SI{0.101}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_4$  & \SI{1.256}{[\mega\pascal]} \\
\cellcolor{lightgray!20}& $C_5$  & \SI{0}{[\mega\pascal]} \\
\multirow{-6}{*}{\rotatebox{90}{{\cellcolor{lightgray!20}\footnotesize Placa}}}& $C_6$  & \SI{0.301}{[\mega\pascal]} \\
\bottomrule
\end{tabularx}
\caption[Parámetros materiales de la sangre, arteria y placa]{Parámetros materiales de la sangre, arteria y placa que los definen como materiales para COMSOL.}
\label{taula:sangue}
\end{table}
\end{document}

enter image description here

I have introduced some \kern in the first and last columns. To have rotated text over the color, I have moved \multirow to the last line with negative number of `rows like

\multirow{-2}{*}.... 

And last, I have used \cellcolor to fill just one cell at suitable places. If you don't want to color the entire first column that has multi rows, remove the \cellcolor{lightgray!20} from those cells like

\midrule
& $C_1$  & \SI{0.020}{[\mega\pascal]} \\
& $C_2$  & \SI{0.003}{[\mega\pascal]} \\
& $C_3$  & \SI{0.175}{[\mega\pascal]} \\
& $C_4$  & \SI{0.090}{[\mega\pascal]} \\
& $C_5$  & \SI{0}{[\mega\pascal]} \\
\multirow{-6}{*}{\rotatebox{90}{{\footnotesize Arteria}}}& $C_6$  & \SI{0}{[\mega\pascal]} \\
\midrule

to get

enter image description here

  • Wow, it looks great, thanks a lot, \cellcolor was the option I need, let me study the code and apply it to my document. – Aradnix Oct 29 '14 at 06:11
  • I included a new third table with problems, what do you suggest me in this case? – Aradnix Oct 29 '14 at 10:09
  • @Aradnix By \rowcolors{5}{}{lightgray!20} coloring starts from rwo number 5. In your table row number 5 is starting with contents 0 & {-} & {-} & 1.00 & - \\. So every thing is working as per rule. If you want coloring to start from the row 4 & 4.10 & 10.9 & 0.32 & 193 \\ , use \rowcolors{9}{}{lightgray!20} since it is 9th row. Also better use braces around -. Hope I didn't miss anything. –  Oct 29 '14 at 11:08
  • line or row 5 is right for start colouring table, but when I try to use C or Y column type starts from row 1 instead of 5. That's strange. I'll add brackets to the dashes, thanks. – Aradnix Oct 29 '14 at 20:04
  • @Aradnix I got it now. I think it deserves to be a separate question. As a short cut, you may be hiderowcolors and \showrowcolors from xcolor for the time being. –  Oct 29 '14 at 23:21
  • ok I'll post it as a different question later, thanks again. – Aradnix Oct 30 '14 at 00:03
1

The environment {NiceTabular} of nicematrix have built-in tools to color rows, columns and cells and it is directly compatible with booktabs.

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

\usepackage[per-mode=symbol]{siunitx}

\begin{document}

\begin{table} \centering \begin{NiceTabular}{cS[table-format=4.4]} \CodeBefore \rowcolors{2}{}{lightgray!20} \Body \toprule Propiedades materiales en la media & Valor \ \midrule $c_M$ & \SI{3.0}{[\kilo\pascal]} \ $k_{1_M}$ & \SI{2.3632}{[\kilo\pascal]} \ $k_{2_M}$ & \num{0.8393} \ $\beta_M$ & \ang{29.0} \ $\kappa_M$ & \SI{1000}{[\mega\pascal]}\ \midrule Propiedades materiales en la adventicia & Valor \ \midrule $c_A$ & \SI{0.3}{[\kilo\pascal]} \ $k_{1_A}$ & \SI{0.5620}{[\kilo\pascal]} \ $k_{2_A}$ & \num{0.7112} \ $\beta_A$ & \ang{62.0} \ $\kappa_A$ & \SI{1000}{[\mega\pascal]} \ \bottomrule \end{NiceTabular} \caption[Parámetros materiales de la arteria]{Parámetros materiales de la arteria en sus dos túnicas externas: media y adventicia.} \label{taula:param} \end{table}

\end{document}

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

Output of the first code

For the second table.

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

\usepackage[per-mode=symbol]{siunitx}

\begin{document}

\begin{table} \centering

\begin{NiceTabular}{llS[table-format=4.4]} \CodeBefore \rowcolors{2-3}{}{lightgray!20}[cols={2,3}] \rowcolors{5-10}{lightgray!20}{}[cols={2,3}] \rowcolors{12-17}{lightgray!20}{}[cols={2,3},restart] \Body \toprule & Propiedades materiales & Valor \ \midrule \Block{2-1}{\rotate \footnotesize Sangre} & $\rho_s$ & \SI{1060}{[\kilo\gram\per\metre^3]} \ & $\mu_{s}$ & \SI{3.8e-6}{[\metre^2\per\second]} \ \midrule \Block{1-3}{Modelo hiperelástico de Mooney-Rivlin}\ \midrule \Block{6-1}{\rotate\footnotesize Arteria} & $C_1$ & \SI{0.020}{[\mega\pascal]} \ & $C_2$ & \SI{0.003}{[\mega\pascal]} \ & $C_3$ & \SI{0.175}{[\mega\pascal]} \ & $C_4$ & \SI{0.090}{[\mega\pascal]} \ & $C_5$ & \SI{0}{[\mega\pascal]} \ & $C_6$ & \SI{0}{[\mega\pascal]} \ \midrule \Block{1-3}{Modelo hiperelástico de Mooney-Rivlin}\ \midrule \Block{6-1}{\rotate \footnotesize Placa} & $C_1$ & \SI{-0.452}{[\mega\pascal]} \ & $C_2$ & \SI{0.510}{[\mega\pascal]} \ & $C_3$ & \SI{0.101}{[\mega\pascal]} \ & $C_4$ & \SI{1.256}{[\mega\pascal]} \ & $C_5$ & \SI{0}{[\mega\pascal]} \ & $C_6$ & \SI{0.301}{[\mega\pascal]} \ \bottomrule \end{NiceTabular} \caption[Parámetros materiales de la sangre, arteria y placa]{Parámetros materiales de la sangre, arteria y placa que los definen como materiales para COMSOL.} \label{taula:sangue} \end{table} \end{document}

Output of the second code (second table)

F. Pantigny
  • 40,250