7

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}
1
  • If I add gobble=x to the listings options, I get no error, which makes me think the option is completely ignored. – egreg May 29 '15 at 10:43
4

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.

3

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.