Tcolorbox with theorem and lstlisting

tcblistingtcolorboxtheorems

In some of my notes, I want to include an example question as well as code listings on how to carry out the function in one tcolorbox, something like the following image all included inside a box instead of leaving out the code lines:

enter image description here

When I use begin{lstlistings} and end{lstlistings}, I create a compile error and I can't manage to fit a code block inside the tcbtheorem box.

Right now, I'm using a math-based tcb environment, and the settings for it are here:

\newtcbtheorem[number within=section, list inside=examplelist]{tcbexample}{Example}{%
        colback=gray!5, colbacktitle=gray!40, coltitle=black,
        frame hidden, arc=2pt, titlerule=0pt, toptitle=2pt, bottomtitle=2pt,
        fonttitle=\bfseries, breakable, enhanced, parbox=false
}{ex}

Is it possible to change the tcolorbox so that it supports both code (lstlistings environment) and math/text? If so, how can it be done? If not, is there any alternative idea that makes the box as clean as possible? (e.g. no gaps between the boxes, the color and heading can be easily reset)

Thanks!

Best Answer

To include listings with some other content you can use a listing and comment or comment and listing box. The listing part is written into the tcolorbox environment and the comment into the options.

An example:

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

\newtcblisting[auto counter, number within=section, list inside=examplelist]{tcbexample}[2][]{%
        colback=gray!5, colbacktitle=gray!40, coltitle=black,
        frame hidden, arc=2pt, titlerule=0pt, toptitle=2pt, bottomtitle=2pt,
        fonttitle=\bfseries, breakable, enhanced, parbox=false,
        comment and listing,
        title=Example~\thetcbcounter,
        comment={#2},#1
}

\begin{document}

\section{Section}

\begin{tcbexample}{This is a comment which will be shown on upper part\\ \begin{equation}x=3\end{equation}}
First line of a listing
Secon line
third line
\end{tcbexample}

\begin{tcbexample}[colback=blue!30, listing side comment]{This is a comment which will be shown on upper part\\ \begin{equation}x=3\end{equation}}
First line of a listing
Secon line
third line
\end{tcbexample}

\end{document}

enter image description here

Update: combining listing and only text boxes

It's possible to use a tcblisting box to present only textual contents, without any listing. This can be done with options text only or comment only. In the second case, the box contents is still declared into comment options and the environment content is avoided. With a text only options, there's no need for comment and the text is written inside the environment. With this options, there's no need to declare different tcolorboxes for listing or non listing contents and all boxes share the same counter.

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

\newtcblisting[auto counter, number within=section, list inside=examplelist]{tcbexample}[2][]{%
        colback=gray!5, colbacktitle=gray!40, coltitle=black,
        frame hidden, arc=2pt, titlerule=0pt, toptitle=2pt, bottomtitle=2pt,
        fonttitle=\bfseries, breakable, enhanced, parbox=false,
        comment and listing,
        title=Example~\thetcbcounter,
        comment={#2},#1
}

\begin{document}

\section{Section}

\begin{tcbexample}{This is a comment which will be shown on upper part\\ \begin{equation}x=3\end{equation}}
First line of a listing
Secon line
third line
\end{tcbexample}

\begin{tcbexample}[comment only]{This is a comment which will be shown on upper part\\ \begin{equation}x=3\end{equation}}
This listing is not shown. It could be empty.
First line of a listing
Secon line
third line
\end{tcbexample}

\begin{tcbexample}[text only]{This comment can be left empty}
this is the contents of a \texttt{text only} box
\end{tcbexample}

\end{document}

enter image description here