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: (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}
lstlisting
, but for simplelstlisting
-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.