Is it possible to have optional arguments for newtcblisting?
Do I use \ifthenelse?
Edit: added the solution suggested by @cmhughes in his comment using the xparse library.
Here is an example of the tcolorbox manual section 4.13 p 302, the argument in square brackets [ ] is optional :
\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{mybox}[2][]{%
colback=red!5!white,
colframe=red!75!black,
fonttitle=\bfseries,
title=#2,#1}
\begin{document}
\begin{mybox}%[listing only]
{Listing Box}
This is my \LaTeX\ box.
\end{mybox}
\bigskip
\begin{mybox}[listing only]
{Listing Box}
This is my \LaTeX\ box.
\end{mybox}
\bigskip
\begin{mybox}[listing side text]
{Listing Box}
This is my
\LaTeX\ box.
\end{mybox}
\end{document}
Output:
Second solution with the xparse library
The format is \DeclareTCBListing[init options]{name}{specification}{options}
where {specifications} are those of the xparse package.
In the example, from page 441 of manual 4.13 the specifications are { s O{} m } where :
See the xparse package documentation for more information: xparse
\documentclass{article}
\usepackage[most]{tcolorbox}
\DeclareTCBListing{mybox}{ s O{} m }{%
colback=red!5!white,
colframe=red!75!black,
fonttitle=\bfseries,
IfBooleanTF={#1}
{listing side text}
{text side listing},
title=#3,#2}
\begin{document}
\begin{mybox}{Listing Box}
This is my
\LaTeX\ box.
\end{mybox}
\bigskip
\begin{mybox}*{Listing Box}
This is my
\LaTeX\ box.
\end{mybox}
\bigskip
\begin{mybox}[colback=yellow]
{Listing Box}
This is my
\LaTeX\ box.
\end{mybox}
\end{document}
\DeclareTCBListing{mybox}{O{}m}{% colback=red!5!white, colframe=red!75!black, fonttitle=\bfseries, title=#2,#1}
– cmhughes
Apr 27 '18 at 10:01
\DeclareTCBlisting, Section 22.4 of the documentation – cmhughes Apr 26 '18 at 14:08