2

I have seen the following usage:

\NewTColorBox[]{containerbox}{o o}{
    %invisible boxes for sections (now grey)
    boxsep=0cm,
    boxrule=0cm,
    sharp corners,
    left=0cm,
    right=0cm,
    top=0cm,
    bottom=0cm,
    leftright skip=0cm,
    nobeforeafter,
    IfValueT={#1}{width=#1},
    IfValueT={#2}{equal height group=#2},
}

I want to understand how the last two lines before the ending } work. I have seen how IfValueT works as a conditional statement, as in IfValueT{condition}{code_if_condition_met}{code_if_condition_unmet}. But the usage

IfValueT = {#1}{width=#1} 

seems strange because it feels like the \NewTColorBox should recognize IfValueT as an argument... yet it somehow doesn't??

It seems like the way it's used is that if the first optional parameter to containerbox will be passed, then the width parameter gets passed to \NewTColorBox when creating an instance of containerbox, and if not, then the width parameter is not passed to \NewTColorBox. Am I getting this right? And if so, how does it work? Is the compiler simply treating the IfValueT as an argument in a special way? And would something like

\NewTColorBox[]{containerbox}{o}{
IfValueT = {#1}{width=#1}{width=1.0}
}

also work if I want to have a width of 1 in case no optional parameter gets passed?

What about the usage

\NewTColorBox[]{containerbox}{o}{
left=0cm,
IfValueT{#1}{width=#1}{width=1.0}
}

Would this work and be equivalent to what I had before?

1
  • 1
    that is key from the tcolorbox package, so check its (quite good and complete) documentation about how to use the key. Dec 29, 2022 at 14:47

1 Answer 1

4

The IfValueT key is defined as a key taking three arguments, the definition looks like this (from tcolorbox.sty -- line numbering added by me):

875   \ExplSyntaxOn
876   \tcbset
877     {
...   
888       IfValueT/.code~n~args={2}{\tl_if_novalue:nF{#1}{\pgfkeysalso{#2}}},
...   
893     }
894   \ExplSyntaxOff

So what the key does is take two arguments after the equals sign, then it checks whether the first argument is the special no-value marker (which is the default value for an o type argument), and if it isn't it uses \pgfkeysalso{#2} which will set additional keys. So IfValueT is just a simple key that executes some useful code.

Another such useful key is IfValueTF which allows what you're asking for, namely setting different keys if the value was given or not:

\NewTColorBox[]{containerbox}{o}
  {
    left=0cm,
    IfValueTF={#1}{width={#1}}{width=1.0}
  }

But in the case that you just want to use a default value for width and always use it (in which case the behaviour is different from omitting the width key with the IfValueT={#1}{width={#1}} solution) it is much simpler to just set a default value for your optional width argument:

\NewTColorBox[]{containerbox}{O{1.0cm}}
  {
    left=0cm,
    width={#1}
  }

Example document using the suggested optional argument with a default value:

\documentclass{article}

\usepackage{tcolorbox}

\NewTColorBox{containerbox}{O{1cm}}{left=0cm, width={#1}}

\begin{document}
\begin{containerbox}
  some thin box
\end{containerbox}
\begin{containerbox}[5cm]
  some thick box
\end{containerbox}
\end{document}

Result:

enter image description here

You must log in to answer this question.

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