
Ich verwende die folgende Lösung, um Code anzuzeigen, der wie der Code von Jupyter Notebook aussehen soll. Ich habe ihn von hier:IPython Notebook-Zellen mit Auflistungen
Das Ergebnis sieht wie folgt aus:
(Quelle aus dem verlinkten Beitrag)
Gibt es eine Möglichkeit, die Möglichkeit hinzuzufügen,Beschriftungen und Labelszu den Kisten?
Minimales Arbeitsbeispiel:
\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}
Antwort1
Sie können die blend into
Option (in diesem Fall listings
bedeutet dies auch, dass Sie die Definition in einfügen müssen \AtBeginDocument{...}
, tcolorbox
die Gründe finden Sie im Handbuch) zusammen mit verwenden title
. blend into
holt den entsprechenden Float-Zähler und setzt den Titel entsprechend.
Mit diesen Änderungen an Ihrem Code
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
enlarge left by=\inwd,
...
}
}
und Schreiben
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}
wir bekommen
Wenn Sie möchten, dass die Beschriftung außerhalb des Titels steht, können Sie beispielsweise verwendencomment above* listing
stattdessenvon listing only
zusammen mit einem separaten Titel:
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][\theipythcntr]{
title=#2,
detach title,
coltitle=black,
comment above* listing,
comment=\centering\tcbtitle,
enlarge left by=\inwd,
...
}
}
Wenn wir den Code ändern in
\AtBeginDocument{
\newtcblisting[blend into=listings]{ipythonnb}[2][lst:\thelstlisting]{
label=#1,
title=#2,
...
{\stepcounter{ipythcntr}In [\theipythcntr]:};
...
}
}
und Typ
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}
wir können auch Beschriftungen verwenden:
Der vollständige Code für das letzte Beispiel:
\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}