4

I have the following code working just fine:

\documentclass[letterpaper,10pt]{article}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{boolexpr,pdftexcmds,trace}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}

\makeatletter
    \long\def\isequal#1#2{\pdf@strcmp{#1}{#2}}
\makeatother

\providecommand{\denoun}[3][---]{%
    \def\Artikel{#1}%
    \def\colornoun{}%
    \switch%
        \case{\isequal{\Artikel}{der}}%
            \renewcommand{\colornoun}[1]{\textcolor{mediumtealblue}{##1}}%
        \case{\isequal{\Artikel}{die}}%
            \renewcommand{\colornoun}[1]{\textcolor{red(munsell)}{##1}}%
        \case{\isequal{\Artikel}{das}}%
            \renewcommand{\colornoun}[1]{\textcolor{forestgreen}{##1}}%
        \otherwise%
            \renewcommand{\colornoun}[1]{\textcolor{black}{##1}}%
    \endswitch%
    \colornoun{#1 #2} & \textcolor{ginger}{die #3} \\%
}

\begin{document}

    \begin{tabular}{|c|c|}
        \hline
            \multicolumn{2}{|c|}{Nouns in german} \\
        \hline
            \denoun[der]{Apfel}{Äpfel}
            \denoun[die]{Katze}{Katzen}
            \denoun[das]{Beispiel}{Beispiele}
        \hline
    \end{tabular}
    
\end{document}

Table from the first code.

Then I thought that maybe I could modify the code from the \denoun command to get something like this:

\documentclass[letterpaper,10pt]{article}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{boolexpr,pdftexcmds,trace}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}

\begin{document}

    \begin{tabular}{|c|c|}
        \hline
            \multicolumn{2}{|c|}{Nouns in german} \\
        \hline
            \textcolor{mediumtealblue}{der Apfel} & \colorbox{mediumtealblue}{\textcolor{white}{die Äpfel}} \\
            \textcolor{red(munsell)}{die Katze} & \colorbox{red(munsell)}{\textcolor{white}{die Katzen}} \\
            \textcolor{forestgreen}{das Beispiel} & \colorbox{forestgreen}{\textcolor{white}{die Beispiele}} \\
        \hline
    \end{tabular}
    
\end{document}

Desired table.

So I've tried modifying the \denoun command as following:

\documentclass[letterpaper,10pt]{article}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{boolexpr,pdftexcmds,trace}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}

\makeatletter
    \long\def\isequal#1#2{\pdf@strcmp{#1}{#2}}
\makeatother

\providecommand{\denoun}[3][---]{%
    \def\Artikel{#1}%
    \switch%
        \case{\isequal{\Artikel}{der}}%
            \colorlet{nouncolor}{mediumtealblue}%
        \case{\isequal{\Artikel}{die}}%
            \colorlet{nouncolor}{red(munsell)}%
        \case{\isequal{\Artikel}{das}}%
            \colorlet{nouncolor}{forestgreen}%
        \otherwise%
            \colorlet{nouncolor}{black}%
    \endswitch%
    \textcolor{nouncolor}{#1 #2} & \colorbox{nouncolor}{\textcolor{white}{die #3}} \\%
}

\begin{document}

    \begin{tabular}{|c|c|}
        \hline
            \multicolumn{2}{|c|}{Nouns in german} \\
        \hline
            \denoun[der]{Apfel}{Äpfel}
            \denoun[die]{Katze}{Katzen}
            \denoun[das]{Beispiel}{Beispiele}
        \hline
    \end{tabular}
    
\end{document}

But then I get the following error message:

! Package xcolor Error: Undefined color `nouncolor'.

So I want to know what's the mistake that I'm making? or, is there another way for doing this?

I would really appreciate your answers, thanks!

4
  • 2
    please make a complete example from your snippets. That makes it much easier to test the issue. – Ulrike Fischer Jun 25 '20 at 11:13
  • Ok, it's done. Thanks for answering. – Leonardo Alvaro Jun 25 '20 at 11:20
  • 1
    (Can't test right now so only a comment instead of an answer) The color is defined in a table cell, which acts as a group and makes the definition local to the cell where it is declared. Possible solution: add \xglobal in front of \colorlet, i.e. replace the four instances of \colorlet by \xcolor\colorlet. – campa Jun 25 '20 at 11:29
  • You can use \globalcolorstrue to make the color definitions global. – Ulrike Fischer Jun 25 '20 at 11:48
7

You can use \globalcolorstrue to make the color definitions global.

But I would avoid to test all the time the argument and reset colors. I would define them first and use them by name:

\documentclass[letterpaper,10pt]{article}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{boolexpr,pdftexcmds,trace}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}


\colorlet{dernouncolor}{mediumtealblue}
\colorlet{dienouncolor}{red(munsell)}
\colorlet{dasnouncolor}{forestgreen}

\providecommand{\denoun}[3][---]{%
    \xglobal\providecolor{#1nouncolor}{named}{black}%
    \textcolor{#1nouncolor}{#1 #2} & \colorbox{#1nouncolor}{\textcolor{white}{die #3}} 
    \\%
}

\begin{document}

    \begin{tabular}{|c|c|}
        \hline
            \multicolumn{2}{|c|}{Nouns in german} \\
        \hline
            \denoun[der]{Apfel}{Äpfel}
            \denoun[die]{Katze}{Katzen}
            \denoun[das]{Beispiel}{Beispiele}
            \denoun[blub]{Beispiel}{Beispiele}
        \hline
    \end{tabular}

\end{document}

enter image description here

4

The problem is that the \switch gets lost once the & is reached, because each tabular cell is conceived in its own group. The solution is to globally define the \switch (which I do in \getnouncolor), and invoke it in each cell, as needed.

\documentclass[letterpaper,10pt]{article}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{boolexpr,pdftexcmds,trace}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}

\makeatletter
    \long\def\isequal#1#2{\pdf@strcmp{#1}{#2}}
\makeatother

\providecommand{\denoun}[3][---]{%
    \gdef\getnouncolor{%
    \def\Artikel{#1}%
    \switch%
        \case{\isequal{\Artikel}{der}}%
            \colorlet{nouncolor}{mediumtealblue}%
        \case{\isequal{\Artikel}{die}}%
            \colorlet{nouncolor}{red(munsell)}%
        \case{\isequal{\Artikel}{das}}%
            \colorlet{nouncolor}{forestgreen}%
        \otherwise%
            \colorlet{nouncolor}{black}%
    \endswitch%
    }
    \getnouncolor\textcolor{nouncolor}{#1 #2} & 
    \getnouncolor\colorbox{nouncolor}{\textcolor{white}{die #3}} \\%
}

\begin{document}

    \begin{tabular}{|c|c|}
        \hline
            \multicolumn{2}{|c|}{Nouns in german} \\
        \hline
            \denoun[der]{Apfel}{Äpfel}
            \denoun[die]{Katze}{Katzen}
            \denoun[das]{Beispiel}{Beispiele}
        \hline
    \end{tabular}
    
\end{document}

enter image description here

3

There's a much simpler solution, without case switch (well, we need to cope with just two cases): make the argument part of the color name.

\documentclass[letterpaper,10pt]{article}
\usepackage[T1]{fontenc}
\usepackage[english,german]{babel}
\usepackage{xcolor}
\usepackage{etoolbox}

\definecolor{mediumtealblue}{rgb}{0.0, 0.33, 0.71}
\definecolor{red(munsell)}{rgb}{0.95, 0.0, 0.24}
\definecolor{forestgreen}{rgb}{0.13, 0.55, 0.13}
\definecolor{ginger}{rgb}{0.69, 0.4, 0.0}
\colorlet{dercolor}{mediumtealblue}
\colorlet{diecolor}{red(munsell)}
\colorlet{dascolor}{forestgreen}
\colorlet{---color}{black}

\newcommand{\denoun}[3][---]{%
  \textcolor{#1color}{#1 #2} &
  \maybecolorbox{#1}{#3\vphantom{Äp}} \\
}
\newcommand{\maybecolorbox}[2]{%
  \ifstrequal{#1}{---}{#2}{\colorbox{#1color}{\textcolor{black}{#2}}}%
}

\begin{document}

\begin{tabular}{|c|c|}
\hline
\multicolumn{2}{|c|}{Nouns in german} \\
\hline
\denoun[der]{Apfel}{Äpfel}
\denoun[die]{Katze}{Katzen}
\denoun[das]{Beispiel}{Beispiele}
\denoun{Foo}{Foo}
\hline
\end{tabular}
    
\end{document}

enter image description here

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.