
Я использую следующее решение для отображения кода, который должен выглядеть как код из Jupyter Notebook. Я взял его отсюда:Ячейки IPython Notebook со списками
Результат выглядит так:
(Источник из связанного поста)
Есть ли способ добавить возможностьподписи и меткив коробки?
Минимальный рабочий пример:
\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}
решение1
Вы можете использовать blend into
опцию (в этом случае listings
это также означает, что вам нужно поместить определение в \AtBeginDocument{...}
, см. tcolorbox
руководство для получения информации о причинах) вместе с title
. blend into
выбирает соответствующий счетчик с плавающей точкой и устанавливает заголовок соответствующим образом.
С этими изменениями в вашем коде
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
enlarge left by=\inwd,
...
}
}
и письмо
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}
мы получаем
Если вы хотите, чтобы подпись была снаружи, а не была заголовком, вы можете, например, использоватьcomment above* listing
вместовместе listing only
с отдельным заголовком:
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\centering\tcbtitle,
enlarge left by=\inwd,
...
}
}
Если мы изменим код на
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
label=#1,
title=#2,
...
{\stepcounter{ipythcntr}In [\theipythcntr]:};
...
}
}
и тип
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}
мы также можем использовать метки:
Полный код для последнего примера:
\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}