2

I am attempting to colour specific cells in a table. I have tried recommendations such as color only a cell of a table and Efficiently colouring block of table cells, but when I try the code, only a text appears in my table. My MWE (with help from Zarko and Mico) is shown below:

\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
    labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\UseTblrLibrary{siunitx}
\usepackage{lipsum}

\begin{document} \begin{table}[ht] \begin{tblr}{width=\linewidth, hlines, vlines, colspec={*{12}{X[c,m]}}, colsep=4pt, row{1}={ht=7ex}, row{2}={ht=5ex}, hspan=minimal } \SetCell[c=4]{c} I want this to align vertically & & & & \SetCell[c=4]{c} I want this to align vertically & & & & \SetCell[c=4]{c} I want this to align vertically & & & \ \SetCell[c=4]{c} Left & & & & \SetCell[c=4]{c} Center & & & & \SetCell[c=4]{c} Right & & & \ \SetCell[r=2]{c} {Same\ here} & \SetCell[r=2]{c} {Same\ here} & \SetCell[c=2]{c} {Ambient\ Conditions} & & \SetCell[r=2]{c} {Same\ here} & \SetCell[r=2]{c} {Same\ here} & \SetCell[c=2]{c} {Ambient\ Conditions} & & \SetCell[r=2]{c} {Same\ here} & \SetCell[r=2]{c} {Same\ here} & \SetCell[c=2]{c} {Ambient\ Conditions} & \ & & RH & Temp & & & RH & Temp & & & RH & Temp \ A & 200 & 200 & 200 & A & 200 & 200 & 200 & A & 200 & 200 & 200 \ B & 200 & 200 & 200 & B & 200 & 200 & 200 & B & 200 & 200 & 200 \ C & 200 & 200 & 200 & C & 200 & 200 & 200 & C & 200 & 200 & 200 \ D & 200 & 200 & 200 & D & 200 & 200 & 200 & D & 200 & 200 & 200 \ E & 200 & 200 & 200 & E & 200 & 200 & 200 & E & 200 & 200 & 200 \ \end{tblr} \end{table} \end{document}

In this example, I want to colour "Left" in blue, "Center" in green and "Right" in red. Thank you so much.

  • Interestingly, you use my answer on your previous question, but you accept other, non-tabularray one :-(. Why you not than stik with it? If meanwhile change the mind, than please accept my answer :-) – Zarko Aug 30 '21 at 11:25

2 Answers2

4

You can add the desired color through the \SetCell command, for example as follows:

\SetCell[c=4]{c, blue} Left
&   &   &   &   \SetCell[c=4]{c, green} Center
    &   &   &   &   \SetCell[c=4]{c, red} Right
        &   &   &                                                   \\

enter image description here

L.J.R.
  • 10,932
leandriis
  • 62,593
2

I mainly work with large tables generated by other applications and only imported into latex for publication.

For me, the most important feature of the tabularray package is that it can make very clean code by completely separating the styles from the content.

Even if the table changes before the final version, as it usually happens, it is easy to update the content without having to go through the painful review of the cells values, cell by cell, again.

At the same time, different co-workers' inputs on the style can be tested without accidentally changing a cell content.

Coloring the second row is a trivial task.

a

\documentclass[12pt]{article}
\usepackage[margin=3cm]{geometry}
\usepackage[font=scriptsize,
labelfont=bf]{caption}
\usepackage[xcdraw]{xcolor}
\usepackage{tabularray}
\usepackage{pdflscape}
\UseTblrLibrary{siunitx}
\usepackage{lipsum}

\begin{document} \begin{table}[ht] \begin{tblr}{width=\linewidth, hlines, vlines, colspec={*{12}{X[c,m]}}, colsep=4pt, row{1}={ht=7ex}, row{2}={ht=5ex}, hspan=minimal,
cell{1}{1}={c=4}{c}, cell{1}{5}={c=4}{c},
cell{1}{9}={c=4}{c}, cell{2}{1}={c=4}{c,blue!20}, % shaded colors added <<<<<<<<<<<<<<< cell{2}{5}={c=4}{c,green!50!black!10},
cell{2}{9}={c=4}{c,red!15}, cell{3}{1}={r=2}{c},
cell{3}{2}={r=2}{c}, cell{3}{3}={c=2}{c}, cell{3}{5}={r=2}{c},
cell{3}{6}={r=2}{c}, cell{3}{7}={c=2}{c}, cell{3}{9}={r=2}{c},
cell{3}{10}={r=2}{c}, cell{3}{11}={c=2}{c},
} I want this to align vertically&&& &I want this to align vertically&&&
&I want this to align vertically&&&\ Left&&&&Center&&&&Right&&&\ {Same\ here}&{Same\ here}&{Ambient\ Conditions}& &{Same\ here}&{Same\ here}&{Ambient\ Conditions}& &{Same\ here}&{Same\ here}&{Ambient\ Conditions}&\ & & RH & Temp & & & RH & Temp & & & RH & Temp \ A & 200 & 200 & 200 & A & 200 & 200 & 200 & A & 200 & 200 & 200 \ B & 200 & 200 & 200 & B & 200 & 200 & 200 & B & 200 & 200 & 200 \ C & 200 & 200 & 200 & C & 200 & 200 & 200 & C & 200 & 200 & 200 \ D & 200 & 200 & 200 & D & 200 & 200 & 200 & D & 200 & 200 & 200 \ E & 200 & 200 & 200 & E & 200 & 200 & 200 & E & 200 & 200 & 200 \ \end{tblr}
\end{table} \end{document}

Simon Dispa
  • 39,141
  • 1
    I always suggest to use colors in ninecolors package which is automatically loaded by tabularray if you use xcolor. It is rather easy to choose matching colors with ninecolors package. – L.J.R. Aug 30 '21 at 15:43
  • @L.J.R xcolor was there. I did not know of nicecolors . Perhaps it will be mentioned in the update of the tabularray manual? – Simon Dispa Aug 30 '21 at 15:52
  • In fact, I mentioned it since the first version of tabularray. You can just write blue9, green9 and red9 instead of these complicated mixing colors in the code. – L.J.R. Aug 30 '21 at 15:58