3

To write the solution of an exercise, i want to fill in the tabular with the answers in red. The headlines of the table should stay in the same style than the exercise and only a part of the table should be colorized. I can achieve that by using \textcolor{red}{...} in each cell of my table but it's pretty brutal. I'm looking for a solution to declare that all the following words must be in red untill the end of the environment tabular.

I've tried some \color{red} but it doesn't work.

Here is a small example.

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}

\usepackage{colortbl}
\usepackage{chemformula}
\begin{document}
    \begin{center}
        \footnotesize
        \renewcommand{\arraystretch}{1.5}
        \hspace*{-1cm}\begin{tabular}{|c|c|c||c|c|c|c|c|c|c|c|}
            \hline \rowcolor{black}
            \multicolumn{11}{|c|}{\textcolor{white}{Corps purs}}\\\hline
            \rowcolor{gray!90}
            élémentaires&
            \multicolumn{2}{c||}{simples} &  \multicolumn{8}{c|}{composés}\\ 
            \hline
            \rowcolor{gray!70}
            &métal.& non-métal. & \multicolumn{2}{c|}{Oxydes} & Hydroxydes & \multicolumn{2}{c|}{Acides}  & \multicolumn{2}{c|}{Sels}  & Corps organiques\\ 
            \hline 
            \rowcolor{gray!50}
            &&  & métal. & non-métal. &  & bin. & tern. & bin. & tern. &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\ch{C6H12O6}\\ 
            \hline 
            && \ch{Fe2O3} &  &  &  &  &  &  &  &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\
            \hline 
        \end{tabular}
    \end{center}
\end{document}

Desired output :

enter image description here

PS : i'm aware that Fe2O3 is at the wrong place, it was only for a second element :) !

3
  • Should all the cells with a white background color be filled with red text? If so, why not use \color{red} before the table and locally change the textcolor of the header cells back to their original color?
    – leandriis
    Commented Dec 28, 2019 at 11:38
  • It's possible yes but i'm looking for a solution where i don't have to write \textcolor{<color>}{<text>} in each cell. For header cells or for later stuff. Here it asks to me to change the color for each 17 cells. I'm sure it's possible to change color from a starting point to an ending accross a table but i can't find a practical way.
    – Piroooh
    Commented Dec 28, 2019 at 11:44
  • Probably you can make use of tex.stackexchange.com/a/26364/134144
    – leandriis
    Commented Dec 28, 2019 at 11:49

2 Answers 2

3

Assuming that every entry in your table also uses \ch, you can define your own command as follows:

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}

\usepackage{colortbl}
\usepackage{chemformula}

\newcommand{\redch}[1]{\textcolor{red}{\ch{#1}}}
\begin{document}
    \begin{center}
        \footnotesize
        \renewcommand{\arraystretch}{1.5}
        \hspace*{-1cm}\begin{tabular}{|c|c|c||c|c|c|c|c|c|c|c|}
            \hline \rowcolor{black}
            \multicolumn{11}{|c|}{\textcolor{white}{Corps purs}}\\\hline
            \rowcolor{gray!90}
            élémentaires&
            \multicolumn{2}{c||}{simples} &  \multicolumn{8}{c|}{composés}\\ 
            \hline
            \rowcolor{gray!70}
            &métal.& non-métal. & \multicolumn{2}{c|}{Oxydes} & Hydroxydes & \multicolumn{2}{c|}{Acides}  & \multicolumn{2}{c|}{Sels}  & Corps organiques\\ 
            \hline 
            \rowcolor{gray!50}
            &&  & métal. & non-métal. &  & bin. & tern. & bin. & tern. &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\redch{C6H12O6}\\ 
            \hline 
            && \redch{Fe2O3} &  &  &  &  &  &  &  &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\
            \hline 
        \end{tabular}
    \end{center}
\end{document}

You could also use \setchemformula{format=\color{red}} inside of the center environment to change the color of a \ch inside of that environment:

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}

\usepackage{colortbl}
\usepackage{chemformula}


\begin{document}
    \begin{center}
    \setchemformula{format=\color{red}}
        \footnotesize
        \renewcommand{\arraystretch}{1.5}
        \hspace*{-1cm}\begin{tabular}{|c|c|c||c|c|c|c|c|c|c|c|}
            \hline \rowcolor{black}
            \multicolumn{11}{|c|}{\textcolor{white}{Corps purs}}\\\hline
            \rowcolor{gray!90}
            élémentaires&
            \multicolumn{2}{c||}{simples} &  \multicolumn{8}{c|}{composés}\\ 
            \hline
            \rowcolor{gray!70}
            &métal.& non-métal. & \multicolumn{2}{c|}{Oxydes} & Hydroxydes & \multicolumn{2}{c|}{Acides}  & \multicolumn{2}{c|}{Sels}  & Corps organiques\\ 
            \hline 
            \rowcolor{gray!50}
            &&  & métal. & non-métal. &  & bin. & tern. & bin. & tern. &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\ch{C6H12O6}\\ 
            \hline 
            && \ch{Fe2O3} &  &  &  &  &  &  &  &\\ 
            \hline 
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\ 
            \hline
            &&  &  &  &  &  &  &  &  &\\
            \hline 
        \end{tabular}
    \end{center}
\end{document}

enter image description here

1
  • All cells are using \ch{} so the second way is particularly convenient ! In addition, i can write the answers in the student paper now and turn on or off the color font by white or red to hide or write the answers. Thanks !
    – Piroooh
    Commented Dec 28, 2019 at 12:35
4

A possible solution (if I understand your question correct) is to direct a \color{red} command into all columns. I have defined a new column type R for the purpose. Thereafter, you enclose the heading text in \multicolumn{1}{c|}{<text>} to protect against colourisation.

You may adapt David Carlisle answer to this question to your code, and define a new column that accepts a \rowfont-command (also defined in the code):

Example 1 – defining a \rowfont-command

enter image description here

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{array}

\newcolumntype{C}{>{\rowfont}c}
\newcommand\setrowfont[1]{\noalign{\gdef\rowfont{#1}}}
\gdef\rowfont{}

\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}

\usepackage{colortbl}
\usepackage{chemformula}
\begin{document}
    \begin{center}
        \footnotesize
        \renewcommand{\arraystretch}{1.5}
        \hspace*{-1cm}\begin{tabular}{|*{3}{C|}|*{8}{C|}}
            \hline \rowcolor{black}
            \multicolumn{11}{|c|}{\textcolor{white}{Corps purs}}\\\hline
            \rowcolor{gray!90}
            élémentaires &
            \multicolumn{2}{c||}{simples} &
            \multicolumn{8}{c|}{composés}\\ 
            \hline
            \rowcolor{gray!70}
            &
            métal. &
            non-métal. &
            \multicolumn{2}{c|}{Oxydes} &
            Hydroxydes &
            \multicolumn{2}{c|}{Acides} &
            \multicolumn{2}{c|}{Sels} &
            Corps organiques\\ 
            \hline 
            \rowcolor{gray!50}
            &&  &
            métal. &
            non-métal. &
            &
            bin. &
            tern. &
            bin. &
            tern. &\\ 
            \hline \setrowfont{\color{red}}
            &&  X&  &  &  &  &  &  &  &\ch{C6H12O6}\\ 
            \hline 
            && \ch{Fe2O3} &  &  &  &  &  &  &  &\\ 
            \hline 
            X&X& X & X & X & X & X & X & X & X &X\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\
            \hline 
        \end{tabular}
    \end{center}
\end{document}

Example 2 – >{color{red} and\multicolomn}}

enter image description here

\documentclass[11pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage{array}

\usepackage[left=1.00cm, right=1.00cm, top=1.00cm, bottom=1.00cm]{geometry}

\newcolumntype{R}{>{\color{red}}c}

\usepackage{colortbl}
\usepackage{chemformula}
\begin{document}
    \begin{center}
        \footnotesize
        \renewcommand{\arraystretch}{1.5}
        \hspace*{-1cm}\begin{tabular}{|*{3}{R|}|*{8}{R|}}
            \hline \rowcolor{black}
            \multicolumn{11}{|c|}{\textcolor{white}{Corps purs}}\\\hline
            \rowcolor{gray!90}
            \multicolumn{1}{c|}{élémentaires} &
            \multicolumn{2}{c||}{simples} &
            \multicolumn{8}{c|}{composés}\\ 
            \hline
            \rowcolor{gray!70}
            &
            \multicolumn{1}{c|}{métal.} &
            \multicolumn{1}{c||}{non-métal.} &
            \multicolumn{2}{c|}{Oxydes} &
            \multicolumn{1}{c|}{Hydroxydes} &
            \multicolumn{2}{c|}{Acides}  &
            \multicolumn{2}{c|}{Sels} &
            \multicolumn{1}{c|}{Corps organiques}\\ 
            \hline 
            \rowcolor{gray!50}
            &&  &
            \multicolumn{1}{c|}{métal.} &
            \multicolumn{1}{c|}{non-métal.} &
            &
            \multicolumn{1}{c|}{bin.} &
            \multicolumn{1}{c|}{tern.} &
            \multicolumn{1}{c|}{bin.} &
            \multicolumn{1}{c|}{tern.} &\\ 
            \hline 
            &&  X&  &  &  &  &  &  &  &\ch{C6H12O6}\\ 
            \hline 
            && \ch{Fe2O3} &  &  &  &  &  &  &  &\\ 
            \hline 
            X&X& X & X & X & X & X & X & X & X &X\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\ 
            \hline
            &&  X&  &  &  &  &  &  &  &\\
            \hline 
        \end{tabular}
    \end{center}
\end{document}
3
  • It's a good way to define a new column type but i have to enclose each header cells in \multicolumn{} to protect it. It's the same than set \color{red} before the table and turn off by using \textcolor{black}{text} on each header cells too. I'm looking for a way to colourize without changing all cells by hands. Something like the \rowfont command used here : tex.stackexchange.com/a/26363/169294
    – Piroooh
    Commented Dec 28, 2019 at 12:32
  • @Piroooh I thought that since you already had several multicolumn in the heading, five more did not matter. To achieve you goal, you have to either test each cell for its content, or program a rowfont-macro.
    – Sveinung
    Commented Dec 28, 2019 at 13:34
  • @Piroooh See updated answer! Now you have a \rowfont command that works from the place it it applied until it is switched off.
    – Sveinung
    Commented Dec 28, 2019 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .