Following code shows the problem. I've declared a \newtcblisting
with an optional argument.
\newtcblisting{codiltxb}[1][]{listing only, #1}
When the optional argument is present in further declarations, the listing is correct. (1st and 2nd boxes in figure)
\begin{codiltxb}[colback=green!20]
\Some code. Correct if the optional argument is present.
\end{codiltxb}
\begin{codiltxb}[]
\Some code. Correct if the optional argument, although empty, is present.
\end{codiltxb}
But if the optional argument is not present and the first word inside listing starts with \
(like \LaTeX
commands), this first command disappears from resulting listing. (3rd box)
\begin{codiltxb}
\Some code. FAIL! No optional argument. First word (\LaTeX command) missing.
\end{codiltxb}
If the optional argument is not present and the first word doesn't start with \
, the listing is correct again. (4th box)
\begin{codiltxb}
Some code. Correct! No optional argument. First word not \LaTeX command.
\end{codiltxb}
What's wrong with my code?
\documentclass{beamer}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[most]{tcolorbox}
\newtcblisting{codiltxb}[1][]{listing only, #1}
\begin{document}
\begin{frame}[fragile]
\frametitle{Listing}
\begin{codiltxb}[colback=green!20]
\Some code. Correct if the optional argument is present.
\end{codiltxb}
\begin{codiltxb}[]
\Some code. Correct if the optional argument, although empty, is present.
\end{codiltxb}
\begin{codiltxb}
\Some code. FAIL! No optional argument. First word (\LaTeX command) missing.
\end{codiltxb}
\begin{codiltxb}
Some code. Correct! No optional argument. First word not \LaTeX command.
\end{codiltxb}
\end{frame}
\end{document}
Note (2018-06-13): Although Thomas F. Sturm answer worked until now, some recent changes (2018-05-12) in xparse
(see: xparse gobbles character(s) using an optional argument) make it fail again. A working solution while waiting for a better one is
\NewTCBListing{codiltxb}{ !O{} }{listing only, #1}
\Some
token is not written out in the auxiliary file written bytcolorbox
(with extension.listing
). It seems to have to do with the grabbing of the optional argument.