4

I am using following solution to display code which should look like the code from Jupyter Notebook. I got it from here: IPython Notebook cells with listings

The result looks like this: https://i.sstatic.net/C18GA.png (Source from the linked post)

Is there any way to add the possibility of captions and labels to the boxes?

Min working example:

\documentclass{article}
\usepackage{listings}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}


\newlength\inwd
\setlength\inwd{1.3cm}

\newcounter{ipythcntr}

\newtcblisting{ipythonnb}[1][\theipythcntr]{
    enlarge left by=\inwd,
    width=\linewidth-\inwd,
    enhanced,
    boxrule=0.4pt,
    colback=light-gray,
    listing only,
    top=0pt,
    bottom=0pt,
    overlay={
        \node[
        anchor=north east,
        text width=\inwd,
        font=\footnotesize\ttfamily\color{blue!50!black},
        inner ysep=2mm,
        inner xsep=0pt,
        outer sep=0pt
        ] 
        at (frame.north west)
        {\stepcounter{ipythcntr}In [#1]:};
    }
    listing options={
        basicstyle=\footnotesize\ttfamily,
        language=python,
        escapechar=¢,
        showstringspaces=false,
    },
}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}


\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}
n = 10
\end{ipythonnb}

\begin{ipythonnb}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\begin{ipythonnb}[13]
n = 10
\end{ipythonnb}

\end{document}
2
  • 1
    I don't know about how to pass these arguments from your custom environment to lstlisting, but for simple lstlisting-environments something like this should work: \begin{lstlisting}[caption={a caption}, label={lst:label}] ... \end{lstlisting}. Now, if you wanted to switch, while keeping the style, let me point you to this.
    – BadAtLaTeX
    Aug 22, 2019 at 22:02
  • Note that listings and longtable use custom \caption commands. As for labels, they are set up by the previous \refstepcounter command. Aug 23, 2019 at 4:05

1 Answer 1

4

You could use the blend into option (in the case of listings this also means you have to put the definition into \AtBeginDocument{...}, see the tcolorbox manual for the reasons) together with title. blend into picks up the corresponding float counter and sets the title accordingly.

With these changes to your code

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
    title=#2,
    enlarge left by=\inwd,
    ...
  }
}

and writing

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\begin{ipythonnb}[13]{And another one}
n = 10
\end{ipythonnb}

we get

enter image description here


If you want the caption to be outside instead of a being a title you could for example use comment above* listing instead of listing only together with a detached title:

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
    title=#2,
    detach title,
    coltitle=black,
    comment above* listing,
    comment=\centering\tcbtitle,
    enlarge left by=\inwd,
    ...
  }
}

enter image description here


If we change the code into

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
    label=#1,
    title=#2,
    ...
    {\stepcounter{ipythcntr}In [\theipythcntr]:};
    ...
  }
}

and type

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}[lst:another]{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\setcounter{ipythcntr}{12}
\begin{ipythonnb}{And another one}
n = 10
\end{ipythonnb}

see listing~\ref{lst:another}

we also can use labels:

enter image description here


The complete code for the last example:

\documentclass{article}
\usepackage{listings}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}


\newlength\inwd
\setlength\inwd{1.3cm}

\newcounter{ipythcntr}

\AtBeginDocument{
  \newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
    label=#1,
    title=#2,
    detach title,
    coltitle=black,
    comment above* listing,
    comment=\centering\tcbtitle,
    enlarge left by=\inwd,
    width=\linewidth-\inwd,
    enhanced,
    boxrule=0.4pt,
    colback=light-gray,
    top=0pt,
    bottom=0pt,
    overlay={
        \node[
        anchor=north east,
        text width=\inwd,
        font=\footnotesize\ttfamily\color{blue!50!black},
        inner ysep=2mm,
        inner xsep=0pt,
        outer sep=0pt
        ] 
        at (frame.north west)
        {\stepcounter{ipythcntr}In [\theipythcntr]:};
    }
    listing options={
        basicstyle=\footnotesize\ttfamily,
        language=python,
        escapechar=¢,
        showstringspaces=false
    }
  }
}
\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt}


\begin{document}

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.

Here are two IPython cells:
\begin{ipythonnb}{My caption}
n = 10
\end{ipythonnb}

\begin{ipythonnb}[lst:another]{Another caption}
for i in range(n):
    print('i = ', i)
\end{ipythonnb}

\setcounter{ipythcntr}{12}
\begin{ipythonnb}{And another one}
n = 10
\end{ipythonnb}

see listing~\ref{lst:another}

\end{document}

You must log in to answer this question.

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