2

Please see the picture and code for my question.

\documentclass{article}
\usepackage[most]{tcolorbox}

    \begin{document}

    % define a new style named selfsetcontainer by container/.code
    \tcbset{container/.code={\tcbset{selfsetcontainer/.style={#1}}},
      container/.default={}}

    \begin{tcolorbox}[container,colback=yellow,selfsetcontainer={colback=green}]
      I want green background color, but get yellow. That is to say, the option 'colback=green' in nested style does not work. Why and how to solve it?
    \end{tcolorbox}

    \end{document}

enter image description here

2

I think you got some things mixed up. You need to pass the option to the code, not to the style. In the code, you define the style. So how would the code know that you want to define a green style? This will only work if you tell this to the code, i.e. pass the argument to it. (And I think it is somewhat advantageous to use \pgfkeysalso here simply because that decreases the chances of you getting trapped in a loop or other dismay.)

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}

    % define a new style named selfsetcontainer by container/.code
    \tcbset{container/.code={\pgfkeysalso{selfsetcontainer/.style={#1}}},
      container/.default={}}

    \begin{tcolorbox}[container={colback=green},colback=yellow,selfsetcontainer]
      I want green background color, and that's what I get. I set the style in
      the \texttt{container} code, where I have decided to switch to
      \verb|\pgfkeysalso| in order to reduce the chances of confusing
      Ti\emph{k}Z, which underlies the \texttt{tcolorbox} package.
    \end{tcolorbox}

\end{document}

enter image description here

Response to your comment: Yes, one can define a code that defines a possibly empty style. However, in order to set the style, you need to call another code. This leads to

\documentclass{article}
\usepackage[most]{tcolorbox}

\begin{document}

    % define a new style named selfsetcontainer by container/.code
    \tcbset{container/.code={\tcbset{selfsetcontainer/.style={#1}}},
      container/.default={},
      set style/.code n args={2}{\pgfkeysalso{#1/.style={#2}}}}

    \begin{tcolorbox}[container,colback=yellow,set
    style={selfsetcontainer}{colback=green},selfsetcontainer]
      This is a rather involved way of accomplishing the same:
      \begin{enumerate}
       \item \texttt{container} is a code that defines an empty style named 
       \texttt{selfsetcontainer}.
       \item \texttt{set style} is another code that sets the style of
       container, or more generally, of any style.
       \item Finally, \texttt{container} is now a style that makes the
       background green.
      \end{enumerate}
    \end{tcolorbox}

\end{document}

enter image description here

Notice that the last two steps may be combined to one (at the expense of having a more complicated construction). Are you sure you want to go that way?

4
  • Perfect and simplie solution!! I have a look at \pgfkeysalso in pgf manuel, but don't understand its use in this example of mine, could you please say more about it and why nested \tcbset is a wrong way?
    – lyl
    Oct 14 '18 at 1:32
  • @lyl I added a more complicated solution. Why \pgfkeysalso? Are you content with the information that whenever I got some really cool piece code by very advanced users like Jake or percusse, they tend to use that trick. It might be that the only reason is that you do not have to worry about the path, i.e. you could copy the above 1-to-1 to TikZ (of course not the colback part, but the principle).
    – user121799
    Oct 14 '18 at 1:39
  • Many thanks!! The vesion one you provide is simple and perfect, the veseon two gives me more helpfule info!
    – lyl
    Oct 14 '18 at 1:54
  • @ArtificialStupidity Please stop making such comments. The color was not chosen by me, nor has it any importance here. The only thing that matters here is that the color gets changed in the way the OP wanted it to change.
    – user121799
    Oct 15 '18 at 14:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.