Problem with gobble listing option inside a tcblisting

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.


Another simple example to artificially gobble leading spaces (or tabs) in a \begin{tcblisting} block. That is, based on @ThomasFSturm's tcolorbox package and his above example.

\documentclass[12pt,a4paper]{article}

\usepackage[listings]{tcolorbox}
% Uncomment to reverse the default  order.
%\tcbset{text and listing} 

 % tabsize: increasing this increases the xleftmargin required.
 % xleftmargin: only acts on the text, rather than the listing. 
\lstdefinestyle{mystyle}{
    tabsize=1,
    xleftmargin=-14pt
}

\begin{document}

\begin{itemize}
    \item Lorem ipsum dolor sit amet. 

    \begin{tcblisting}{
            listing options={style=mystyle},
            title=Nibh nunc massa mauris enim dolor}
        Quis libero Ut habitant Phas \LaTeX.
    \end{tcblisting}
\end{itemize}

\end{document}

enter image description here

Without styling the undesirable result is:

enter image description here In my IDE I've got intents set to tabs (rather than spaces) and each tab is set to equal 4 spaces. Nevertheless in the code abovetabsize is set to 1 in order to keep the required multiple of xleftmargin low. It's strange behaviour but it produces the required result in my environment.

Thomas, your package is very impressive.