
Estou usando a solução a seguir para exibir o código que deve se parecer com o código do Jupyter Notebook. Eu peguei daqui:Células IPython Notebook com listagens
O resultado é assim:
(Fonte da postagem vinculada)
Existe alguma maneira de adicionar a possibilidade delegendas e rótulospara as caixas?
Exemplo de trabalho mínimo:
\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}
Responder1
Você pode usar a blend into
opção (neste caso, listings
isso também significa que você deve colocar a definição em \AtBeginDocument{...}
, consulte o tcolorbox
manual para saber os motivos) junto com title
. blend into
pega o contador flutuante correspondente e define o título de acordo.
Com essas mudanças no seu código
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
enlarge left by=\inwd,
...
}
}
e escrevendo
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}
Nós temos
Se você quiser que a legenda fique fora em vez de ser um título, você pode, por exemplo, usarcomment above* listing
em vez dede listing only
junto com um título separado:
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\centering\tcbtitle,
enlarge left by=\inwd,
...
}
}
Se mudarmos o código para
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
label=#1,
title=#2,
...
{\stepcounter{ipythcntr}In [\theipythcntr]:};
...
}
}
e digite
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}
também podemos usar rótulos:
O código completo do último exemplo:
\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}