tcolorbox add tag
samcarter
I'm trying to create a tcolorbox which sets the title depending on a boolean `\iftitle` and leaves it empty otherwise. 

My current workaround is to set it outside the tcolorbox via `\tcbsetforeverylayer` (using this instead of `\tcbset` to make sure it also works in nested boxes). However this has the unintended side effect that the title will be shown twice for boxes with the `attach boxed title to top` option.

As a workaround, I could force a reset of the title's title with `boxed title style={title={}}`, but I was wondering if there is another way to set the title only conditionally? 

```
\documentclass{article}

\usepackage[most]{tcolorbox}

\newif\iftitle
\titletrue

\newenvironment{foo}{
  \iftitle
    \tcbsetforeverylayer{title={Title}}
  \fi  
  \begin{tcolorbox}[
    enhanced,
    attach boxed title to top,
%    boxed title style={title={}}
  ]
}{\end{tcolorbox}}

\begin{document}

\begin{foo}
test
  \begin{foo}
    test
  \end{foo}
\end{foo}


\end{document}
```

![Screenshot 2022-09-02 at 18.25.32.png](/image?hash=20443882ebb87c7faa514cc544f2f9fe5c131f80dda86e470b9f27874844b4bf)
Top Answer
samcarter
So, rubber duck debugging worked great! 

Just after posting this question, I had the idea to check how `IfValue` etc. are defined in tcolorbox:

```
IfValueTF/.code~n~args={3}{\tl_if_novalue:nTF{#1}{\pgfkeysalso{#3}}{\pgfkeysalso{#2}}}
```

With a bit of modification, one can do something like this:

```
\documentclass{article}

\usepackage[most]{tcolorbox}

\newif\iftitle

\tcbset{
  if/.code n args={3}{
    #1
      \pgfkeysalso{#2}
    \else
      \pgfkeysalso{#3}
    \fi
  },
}

\newenvironment{foo}{
  \begin{tcolorbox}[
    enhanced,
    attach boxed title to top,
    if={\iftitle}{title={ttt}}{}
  ]
}{\end{tcolorbox}}

\begin{document}

\begin{foo}
test
  \begin{foo}
    test
  \end{foo}
\end{foo}

\titletrue

\begin{foo}
test
  \begin{foo}
    test
  \end{foo}
\end{foo}

\end{document}
```
Answer #2
Skillmon
Though there is nothing inherently wrong with the [nice answer by samcarter](https://topanswers.xyz/tex?q=2097#a2349), the following builds upon it to increase its robustness (since TeX's rules for nesting `\if`s can lead to surprises, and so having an unbalanced `\if` in some code or nesting `\if`s can lead to breakage).

For this reason this answer slightly alters the syntax to only get the conditionals name and build the real control sequence using `\csname`, while also not using the `\pgfkeysalso` code inside the `\if`-structure but using LaTeX2e-style branching.

```
\makeatletter
\providecommand\@secondofthree[3]{#2}
\tcbset  
  {  
    if/.code n args = {3}{%  
      \csname #1\endcsname\expandafter\@secondofthree\fi  
      \@secondoftwo{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}}  
  }
\makeatother
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.