0

I'm making a custom style for a presentation with beamer. I'm using \getbeamercolor to have the foreground and background colors of a given beamer color in fg and bg, respectively.

I realized I need to get those colors more than once, so I wrote a command to wrap the following code snippet up and call it every time I need it:

\newcommand{\getbeamercolors}[1]{%
  \usebeamercolor{#1}%
  \colorlet{fgColor}{fg}%
  \colorlet{bgColor}{bg}%
}

Unfortunately, I also need to call that command twice, and get all those colors to use them at the same time. Then I thought I could simply modify the above command to append a suffix to each color, like this:

\newcommand{\getbeamercolors}[2]{%
  \usebeamercolor{#1}%
  \colorlet{fg#2}{fg}%
  \colorlet{bg#2}{bg}%
}

But that didn't work. I even looked for an answer on this site, and I even read this, but to no avail.

Is there a way to append a text from a command argument inside the color name when defining it?

Simple MWE:

\documentclass{beamer}

\usetheme{Warsaw}

\usepackage{xcolor}

\newcommand{\getbeamercolors}[1]{%
  \usebeamercolor{#1}%
  \colorlet{fgColor}{fg}%
  \colorlet{bgColor}{bg}%
}

% This didn't work!
%\newcommand{\getbeamercolors}[2]{%
%  \usebeamercolor{#1}%
%  \colorlet{fg#2}{fg}%
%  \colorlet{bg#2}{bg}%
%}

\begin{document}
    \begin{frame}
        \frametitle{This is a test!}
        \getbeamercolors{frametitle}{title}
        Some text to color with \texttt{\textbackslash getbeamercolors}...

        \textcolor{bgColor}{Text with frame title background color.}

        \colorbox{black}{\textcolor{fgColor}{Text with frame title foreground color.}}
    \end{frame}
\end{document}
  • If I uncomment the second definition and change the frame body to have \textcolor{bgtitle} and \textcolor{fgtitle} I get the same picture in your answer. – egreg Mar 20 '17 at 10:39
4

Without defining new colours, you could simply use the already defined beamer colours:

\documentclass{beamer}

\usetheme{Warsaw}

\begin{document}
    \begin{frame}
        \frametitle{This is a test!}

            \usebeamercolor{frametitle}

            normal text

            \textcolor{frametitle.bg}{Text with frame title background color.}

            \colorbox{black}{\textcolor{frametitle.fg}{Text with frame title foreground color.}}
    \end{frame}
\end{document}

Off topic: you don't need xcolor as beamer already provides this functionality for you.

1

I finally got it! I used the etoolbox package, as this answer explained.

UPDATE: Be sure to load first the color theme, otherwise the command won't work!

MWE

\documentclass{beamer}

\usetheme{Warsaw}

\usepackage{etoolbox}
\usepackage{xcolor}

\newcommand{\getbeamercolors}[2]{%
  \usebeamercolor{#1}%
  \def\backName{back}
  \def\foreName{fore}
  \appto\backName{#2}
  \appto\foreName{#2}
  \colorlet{\foreName}{fg}%
  \colorlet{\backName}{bg}%
}

\begin{document}
    \begin{frame}
        \frametitle{This is a test!}
        \getbeamercolors{frametitle}{title}
        Some text to color with \texttt{\textbackslash getbeamercolors}...

        \textcolor{backtitle}{Text with frame title background color.}

        \colorbox{black}{\textcolor{foretitle}{Text with frame title foreground color.}}
    \end{frame}
\end{document}

Output:

Output of given MWE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy