# Strange behaviour using center, tcolorbox, listings and enumerate

I have a problem compiling the following minimal example:

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

\begin{document}
\begin{center}
\begin{tcolorbox}
\begin{lstlisting}
\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…

• 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?. Nov 20, 2018 at 18:11
• What's the center environment supposed to do? The lstlisting environment will use the whole text width anyway. 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} Nov 21, 2018 at 19:50

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}
\end{lstlisting}
boom
\begin{enumerate}
\item one
\item two
\end{enumerate}
\end{tcolorbox}
\end{center}

\end{document}

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

## 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.

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

\begin{document}
\begin{center}
\begin{tcolorbox}
\begin{tcblisting}{skin=empty,lowerbox=ignored}
\end{tcblisting}

boom

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

\end{document}


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

\documentclass{article}
%\usepackage{listings}
\usepackage[most]{tcolorbox}
\tcbuselibrary{documentation}
\begin{document}
\begin{center}
\begin{tcolorbox}
\begin{dispListing}
\end{dispListing}

boom

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


Translated with www.DeepL.com/Translator

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}
\end{lstlisting}

boom

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

\noindent\hrulefill

\end{document}