[Tex/LaTex] Problem with gobble listing option inside a tcblisting

listingstcolorbox

Please, compare next two listings inside a beamer frame. The upper one is made with listings and the second with tcblisting (form tcolorbox). Both use same source and listing options.

enter image description here

Following code shows that although latex code is indented by 2 tabs, once tabs are converted to spaces (tab=2), they are partially suppressed with gobble (gobble=2), but only in listings output. tcblisitng fails and all tabs are shown into the result. Is this a tcolorbox known behavior or a bug? Do you know some way of suppressing indenting spaces with tcblisting?

\documentclass{beamer}
\usepackage{lmodern}
\usepackage[listings]{tcolorbox}

\lstdefinestyle{mystyle}{language=[LaTeX]TeX,
basicstyle=\ttfamily,
texcsstyle=*\color{blue},
breaklines=true,
keywordstyle=\color{green!30!black},
commentstyle=\color{red},
morekeywords={},
otherkeywords={$,\{,\},[,],&},
backgroundcolor=\color{gray!30},
escapeinside=<>,
moretexcs={maketitle},
showtabs=true,
showspaces=true,
tabsize=2,
gobble=2
}

\begin{document}
\begin{frame}[fragile]
\frametitle{Listing}
\begin{lstlisting}[style=mystyle]
    \documentclass{}
        preamble
    \begin{document}
        document text
    \end{document}
\end{lstlisting}

\begin{tcblisting}{listing options={style=mystyle}, listing only}
    \documentclass{}
        preamble
    \begin{document}
        document text
    \end{document}
\end{tcblisting}
\end{frame}

\end{document}

Best Answer

Unfortunately, gooble really is ignored. This key has no effect on \lstinline or \lstinputlisting (taken from the listings manual) and tcblisting uses \lstinputlisting internally.

The following links consider some tricks to circumvent the problem:

autogobble for lstinputlistings

How to extend the \lstinputlisting command

For the current MWE, this could be applied as the following:

\documentclass{beamer}
\usepackage{lmodern}
\usepackage[listings]{tcolorbox}

\lstdefinestyle{mystyle}{language=[LaTeX]TeX,
basicstyle=\ttfamily,
texcsstyle=*\color{blue},
breaklines=true,
keywordstyle=\color{green!30!black},
commentstyle=\color{red},
morekeywords={},
otherkeywords={$,\{,\},[,],&},
backgroundcolor=\color{gray!30},
escapeinside=<>,
moretexcs={maketitle},
showtabs=true,
showspaces=true,
tabsize=2,
gobble=2
}

\begin{document}
\begin{frame}[fragile]
\frametitle{Listing}
\begin{lstlisting}[style=mystyle]
    \documentclass{}
        preamble
    \begin{document}
        document text
    \end{document}
\end{lstlisting}

\begin{tcblisting}{
  listing options={style=mystyle,framexleftmargin=-14pt,numbersep=-7pt,xleftmargin=-14pt},
  listing only,
   }
    \documentclass{}
        preamble
    \begin{document}
        document text
    \end{document}
\end{tcblisting}
\end{frame}

\end{document}

enter image description here

Without showspaces this would look OK. Another option for tcblisting would be to play with the left key to shift everything to the left.

Hopefully, \lstinputlisting will support gobble some day.

Related Question