3

Is it possible to have optional arguments for newtcblisting?

Do I use \ifthenelse?

  • Can you make a MWE to give us something to start with? – user36296 Apr 26 '18 at 14:00
  • take a look at, for example, \DeclareTCBlisting, Section 22.4 of the documentation – cmhughes Apr 26 '18 at 14:08
4

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:

enter image description here

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 :

  • "s" is an optional star, which will result in a value \BooleanTrue if a star is present and \BooleanFalse otherwise (as described later).
  • O{} is as for "o", but returns hdefaulti if no value is given. Should be given as O{hdefaulti}.
  • m is a standard mandatory argument, which can either be a single token alone or multiple tokens surrounded by curly braces. Regardless of the input, the argument will be passed to the internal code surrounded by a brace pair. This is the xparse type specifier for a normal T E X argument

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}

xparse-listing

  • +1 just for reference, you can also use, for example, \DeclareTCBListing{mybox}{O{}m}{% colback=red!5!white, colframe=red!75!black, fonttitle=\bfseries, title=#2,#1} – cmhughes Apr 27 '18 at 10:01

Your Answer

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