我正在使用以下解决方案来显示代码,该代码应该看起来像 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}