# tcolorbox - Define a macro for the title that can be empty

I fail to use a macro that give a possibly empty title for a tcolorbox frame.

Here is the code used.

\documentclass{article}
\RequirePackage{tcolorbox}

\begin{document}

\def\thistitle{One title}
\begin{tcolorbox}[title = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\def\thistitle{}
\begin{tcolorbox}[title = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\begin{tcolorbox}[title = {}]
Bla, bla, bla....
\end{tcolorbox}

\end{document}

• There should be sth. with IfNoValueTF={\thistitle}{true}{false} (tcolorbox manual, p. 448), but I don't get it at this moment. – cis Sep 6 '20 at 6:58
• I can't make the examples work. I thinks I will use a homade wrapper but I was hoping to find an "easy-to-use" solution... – projetmbc Sep 6 '20 at 7:19

You should read the section about pgfkeys in the TikZ & PGF manual. .expand once as well as .expanded can solve the problem here, since you want to obtain the same as title={} when the expansion of \thistitle is empty (.expand once is in my opinion a better choice than .expanded here, since it doesn't expand more than necessary).

\documentclass{article}
\usepackage{tcolorbox}

\begin{document}

\newcommand{\thistitle}{One title}
\begin{tcolorbox}[title/.expand once = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\renewcommand{\thistitle}{}
\begin{tcolorbox}[title/.expand once = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\begin{tcolorbox}[title = {}]
Bla, bla, bla....
\end{tcolorbox}

\end{document}


Here is another way using etoolbox and a custom mytitle key. It works differently from the previous technique in that it never feeds any expansion of \thistitle to title. The title key is either called with \thistitle, or not called at all.

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{etoolbox}

\tcbset{mytitle/.code={%
\expandafter\ifstrempty\expandafter{\thistitle}
{}
{\pgfkeysalso{title=\thistitle}}%
}}

\begin{document}

\newcommand{\thistitle}{One title}
\begin{tcolorbox}[mytitle = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\renewcommand{\thistitle}{}
\begin{tcolorbox}[mytitle = \thistitle]
Bla, bla, bla....
\end{tcolorbox}

\begin{tcolorbox}[mytitle = {}]
Bla, bla, bla....
\end{tcolorbox}

\end{document}


Same output as above.

• Ok, this is way easier then the xparse-stuff. ;) – cis Sep 6 '20 at 7:36
• @cis Yep. But realize that since with title=\thistitle, the title key doesn't “see” that \thistitle is empty, one can infer that no expandable emptiness test \foo would give a better result when used as title=\foo{\thistitle}. title just doesn't expand its argument by itself, which is good in case it might contain stuff that should only be expanded at the very last moment, when doing the typesetting. – frougon Sep 6 '20 at 7:40
• @cis I've added another way, in case you're interested. – frougon Sep 6 '20 at 7:57
• @frougon Thanks for the .expand once. Your homemade solution allows to add materials automatically to the title. This is very intersting ! – projetmbc Sep 6 '20 at 7:58
• @projetmbc Yes, you could even parse the 1st-level expansion of \thistitle using regular expressions as provided by the l3regex module of expl3 if you want... or for simpler things, \str_case:... and its sisters. – frougon Sep 6 '20 at 8:01