我有以下代码运行正常:
\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}
然后我想也许我可以修改\denoun命令来得到如下结果:
\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}
所以我尝试修改\denoun命令如下:
\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}
但随后我收到以下错误消息:
! 包 xcolor 错误:未定义颜色‘nouncolor’。
所以我想知道我犯了什么错误?或者还有其他方法可以做到这一点吗?
我将非常感谢您的回答,谢谢!
答案1
您可以使用它\globalcolorstrue
来使颜色定义变得全局化。
但我会避免一直测试参数并重置颜色。我会先定义它们并按名称使用它们:
\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}
答案2
问题是,\switch
一旦&
达到,就会丢失,因为每个表格单元都是在自己的组中构思的。解决方案是全局定义\switch
(我在中这样做\getnouncolor
),并根据需要在每个单元中调用它。
\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}
答案3
有一个更简单的解决方案,无需切换大小写(好吧,我们只需要应对两种情况):将参数作为颜色名称的一部分。
\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}