4

I have a problem compiling the following minimal example:

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}

\begin{document}
\begin{center}
    \begin{tcolorbox}
        \begin{lstlisting}
            alert(123);
        \end{lstlisting}

        boom

        \begin{enumerate}
            \item one
            \item two
        \end{enumerate}
    \end{tcolorbox}
\end{center}
\end{document}

I cannot reduce anything and don't know if the problem is dedicated to listings, tcolorbox, enumerate or center. (maybe all of them together)

The error is

! LaTeX Error: Something's wrong--perhaps a missing \item.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.15 ^^I^^I^^I\item o
                     ne

which is (like most LaTeX errors) not very useful…

3
  • 1
    Welcome to TeX.SE. If you remove \begin{center} and \end{center} and instead use {\centering.... }% the problem is resolved. Plus using the center environment is adding additional vertical space -- which is usually not desired. See When should we use \begin{center} instead of \centering?. Commented Nov 20, 2018 at 18:11
  • What's the center environment supposed to do? The lstlisting environment will use the whole text width anyway.
    – egreg
    Commented Nov 20, 2018 at 18:33
  • Why using the \begin{center} ... \end{center} environment or the \centering command while we could simply use the tcolorbox environment with the center option? \begin{tcolorbox}[center] ... \end{tcolorbox} Commented Nov 21, 2018 at 19:50

3 Answers 3

2

The \begin{center} confuses tcolorbox besides adding undesirable additional vertical space. Either add \leavevmode before \begin{tcolorbox} or better use {\centering.... } as suggested by @PeterGrill in a comment.

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}

\begin{document}

\begin{center}\leavevmode
\begin{tcolorbox}
    \begin{lstlisting}
        alert(123);
    \end{lstlisting}
    boom
    \begin{enumerate}
        \item one
        \item two
    \end{enumerate}
\end{tcolorbox}
\end{center}

\end{document}
1
  • I think you need to include a \par: {\centering ... \par}. Commented Nov 20, 2018 at 18:31
1

Update: removal of borders.

It seems to me that from your own response to your problem that you don't want the listing to be in a visible box. And as with tcolorbox, everything is configurable, just set it up.

  • With the skin=empty parameter, nothing is drawn at all: so there is no border (see page 237).
  • With the parameter lowerbox=ignored not to display the lower part of the box (see p 24).

It is therefore also possible to set the distances between the content of the listing and the rest of the box, but i did not do.

second box

\documentclass{article}
%\usepackage{listings}
\usepackage[most]{tcolorbox}

\begin{document}
\begin{center}
    \begin{tcolorbox}
        \begin{tcblisting}{skin=empty,lowerbox=ignored}
            alert(123);
        \end{tcblisting}

        boom

        \begin{enumerate}
            \item one
            \item two
        \end{enumerate}
    \end{tcolorbox}
\end{center}

\end{document}

Old answer:

It is possible to use the dispListing environment of the tcolorbox documentation library (see page 465 of the 4.14 manual).

displisting

\documentclass{article}
%\usepackage{listings}
\usepackage[most]{tcolorbox}
\tcbuselibrary{documentation}
\begin{document}
\begin{center}
    \begin{tcolorbox}
        \begin{dispListing}
            alert(123);
        \end{dispListing}

        boom

        \begin{enumerate}
            \item one
            \item two
        \end{enumerate}
    \end{tcolorbox}
\end{center}
\end{document}

Translated with www.DeepL.com/Translator

0

Thank's to @AboAmmar , @PeterGrill and @egreg who managed to find out the source of the error namely the center environment.

To get the the tcolorbox centered we can use its center option:

\documentclass{article}
\usepackage{listings}
\usepackage{tcolorbox}

\begin{document}

\noindent\hrulefill % this rule is added to show that the box is actually centered

    \begin{tcolorbox}[width=0.75\linewidth,center]
        \begin{lstlisting}
            alert(123);
        \end{lstlisting}

        boom

        \begin{enumerate}
            \item one
            \item two
        \end{enumerate}
    \end{tcolorbox}

    \noindent\hrulefill 


\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .