4

I want to store some options of tcolorbox in a macro (because they may changes) for later use. But the following method fails.

\usepackage{tcolorbox}
\begin{document}

\newcommand{\theoption}{colback=red,boxrule=3pt}
\tcbset{myoption/.style={code={\theoption}}}

\begin{tcbitemize}[myoption]
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{tcbitemize}

\end{document}

But the option colback and boxrule have not any effect. So, how to make a marco as option of tcolorbox

6
  • 1
    Welcome to TeX.Stackexchange! Aug 5, 2017 at 12:54
  • 3
    I don't understand why you need to use a macro here, when you have defined a style. If you need to change it at some later stage, just do \tcbset{myoption/.style={colback=blue,boxrule=1pt} or something. Aug 5, 2017 at 13:26
  • Your document misses the class and the correct loading of tcolorbox
    – user31729
    Aug 5, 2017 at 14:34
  • There is a way of doing what you want, but in many instances a solution like Christian Hupfer's or Sam Carter's below is superior.
    – JPi
    Aug 6, 2017 at 1:21
  • 1
    @lyl: The correct way to thank users here is to accept one of the provided answers and upvote all useful ones
    – user31729
    Aug 6, 2017 at 20:57

2 Answers 2

9

The 'correct' (but most likely not the only one) way of adding options and evaluating them from macros is to use

code={\pgfkeysalsofrom{\someoption}}

Please don't use \the... names, those are basically 'reserved' for counter output names like \thesection etc.

\documentclass{article}

\usepackage[most]{tcolorbox}
\begin{document}

\newcommand{\someoption}{colback=red,boxrule=3pt}
\tcbset{myoption/.style={code={\pgfkeysalsofrom{\someoption}}}}

\begin{tcbitemize}[myoption]
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{tcbitemize}

\end{document}

enter image description here

5
  • Thank you so much for your help. This is a perfect soluntion!!!
    – lyl
    Aug 6, 2017 at 4:54
  • Still, why is "\tcbset{myoption/.style={code={\someoption}}}" or "\tcbset{myoption/.style={\someoption}" a wrong way?
    – lyl
    Aug 6, 2017 at 8:10
  • @lyl: \someoption is not expanded to give the keys you desire. You must inform tcolorbox first, that \someoption contains keys with \pgfkeysalsofrom -- this is a very well-known problem with many key-value interfaces
    – user31729
    Aug 6, 2017 at 8:12
  • Does that mean the function of the marco \pgfkeysalsofrom is to expnad firstly its arguments, that is to say, the marco following it?
    – lyl
    Aug 6, 2017 at 8:18
  • @lyl: Basically yes, it prepares the pgfkeys parser used in tcolorbox to understand the content of \someoption (i.e. the keys inside there), hands over this information to the tcolorbox; code={\someoption} just prints the content of \someoption before anything of the box is drawn.
    – user31729
    Aug 6, 2017 at 8:21
3

Not an answer to your question, but maybe a workaround:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{raster}

\newenvironment{myitemize}[1][]{%
 \begin{tcbitemize}[colback=red,boxrule=3pt,#1]}
{\end{tcbitemize}}

\begin{document}

\begin{myitemize}
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
    \tcbitem some text
\end{myitemize}

\end{document}
1
  • Thank you very much for all. The reason why I want to store some tcolorbox options in a marco is: the options may comes from an argument of the other marco, and this argument may vary and it is a string which need to be handled to the form of tcolorbox options. So, anyone can help me with this? Thanks a lot.
    – lyl
    Aug 6, 2017 at 4:45

You must log in to answer this question.

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