在 mdframed in latex 中条件着色

问题描述 投票:0回答:1

我对 mdframed 的条件着色有疑问。下面的代码不起作用,但我不知道为什么。

\documentclass{article}
\usepackage{xstring}
\usepackage{mdframed}

\newcommand{\colorMap}[1]{%
\IfStrEqCase{#1}{{a}{green}{b}{yellow}{c}{red}}[nada]}

\begin{document}

\begin{mdframed 
[backgroundcolor=\colorMap{a}]
\textbf{Color}
\end{mdframed}

\end{document}
latex
1个回答
0
投票

您可以使用简单的

xstring
,而不是使用
\if ... \fi
包中的测试,从而避免扩展问题:

\documentclass{article}
\usepackage{xstring}
\usepackage{mdframed}

\def\astring{a}
\def\bstring{b}
\def\cstring{c}

\usepackage{xcolor}
\newcommand{\colorMap}[1]{%
  \if\astring#1
    green%
  \else
    \if\bstring#1
      yellow%
    \else
      \if\cstring#1
        red%
      \else
        white%
      \fi
    \fi
  \fi
}

\begin{document}

\begin{mdframed}[backgroundcolor=\colorMap{a}]
\textbf{Color}
\end{mdframed}

\end{document}

我不确定您的实际用例是什么,但与其尝试映射颜色,不如创建具有合适名称的颜色会更容易?

\documentclass{article}
\usepackage{xstring}
\usepackage{mdframed}

\usepackage{xcolor}

\colorlet{good}{green}
\colorlet{medium}{yellow}
\colorlet{bad}{red}

\newenvironment{assessment}[1]{
  \begin{mdframed}[backgroundcolor=#1]
  \textbf{This assessment is marked as #1}\par
}{
  \end{mdframed}
}

\begin{document}

\begin{assessment}{good}
some text
\end{assessment}

\begin{assessment}{medium}
some text
\end{assessment}

\begin{assessment}{bad}
some text
\end{assessment}

\end{document}

© www.soinside.com 2019 - 2024. All rights reserved.