2

I'm trying to set a colour depending on the content of a variable. Please have a look at the MWE below. In case of \junk{A} the text should be red on a green background. In case the content of \junk is not A, then the background should be blue. However the background is always blue. What am I doing wrong?

beamersthememwe.sty

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerthememwe}
\def\junk#1{\def\@junk{#1}}
\junk{A}
\RequirePackage{etoolbox}
\RequirePackage{xcolor}
\definecolor{foreground}{RGB}{255, 0, 0}
\ifstrequal{\@junk}{A}{
   \definecolor{background}{RGB}{0, 128, 0}
}{
   \definecolor{background}{RGB}{0, 0, 128}
}
\setbeamercolor{normal text}{fg = foreground, bg = background}

mwe.tex

\documentclass{beamer}
  \usepackage{lipsum}
  \usetheme{mwe}
  \junk{A}
\begin{document}
  \begin{frame}
    \frametitle{Lipsum}
    \lipsum[1]
  \end{frame}
\end{document}
0

1 Answer 1

2

You are comparing \@junk with A and of course “false” is returned. This is because \ifstrequal performs no expansion.

So you need to expand \@junk before the comparison takes place.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{beamerthememwe}
\def\junk#1{\def\@junk{#1}}
\junk{A}
\RequirePackage{etoolbox}
\RequirePackage{xcolor}
\definecolor{foreground}{RGB}{255, 0, 0}
\expandafter\ifstrequal\expandafter{\@junk}{A}{%
   \definecolor{background}{RGB}{0, 128, 0}%
}{%
   \definecolor{background}{RGB}{0, 0, 128}%
}
\setbeamercolor{normal text}{fg = foreground, bg = background}

enter image description here

You must log in to answer this question.

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