6

I'd like to store tcolorbox' (multiple) settings in a (single) macro (in fact, a xkeyval's \cmdKV@fam@key macro) and use them afterwards via this macro (I know I should use pgfkeys instead, but my whole class is using xkeyval). Probably because of a pgfkeys expansion trouble, I can't succeed: for instance, colback=red is a regular option of the tcolorbox's tcolorbox environment, but it can't be stored in a macro, as pointed out by the following MWE (which involves only a single setting for simplicity):

\documentclass{article}
\usepackage{tcolorbox}
%
\newcommand*\foo{colback=red}
%
\begin{document}
\begin{tcolorbox}[\foo]
Test.
\end{tcolorbox}
\end{document}

which leads to the following error:

! Package pgfkeys Error: I do not know the key '/tcb/colback=red'

How could I circumvent this problem?

3
  • 3
    I can understand you're used to xkeyval; however, \tcbset{foo/.style={colback=red}} and \begin{tcolorbox}[foo] does the trick. You'd have the same problem with xkeyval, as it doesn't expand the tokens passed in \setkeys.
    – egreg
    Jun 4, 2014 at 10:37
  • @egreg I already knew how to "manually" define styles and how to use them. My question was about defining them in "one go" from a macro. Jun 4, 2014 at 16:48

1 Answer 1

8

With the combination of the code option plus \pgfkeysalsofrom from pgfkeys you can use options stored in a macro:

\documentclass{article}
\usepackage{tcolorbox}
%
\newcommand*\foo{colback=red,colframe=red!50!black}
%
\begin{document}
\begin{tcolorbox}[code={\pgfkeysalsofrom\foo}]
Test.
\end{tcolorbox}
\end{document}
2
  • Is there a similar mechanism in \begin{tikzpicture}[\pgfkeysalsofrom...]...\end{tikzpicture}?
    – lyl
    Jul 22, 2022 at 1:22
  • @lyl Not by default, but you can set \tikzset{code/.code={#1}}. Then, you can do \begin{tikzpicture}[code={\pgfkeysalsofrom\foo}]. Jul 27, 2022 at 7:47

You must log in to answer this question.

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