
Betrachten Sie das folgende MWE:
\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\lstset{%
basicstyle =\ttfamily,
language = Python,
keywordstyle = \bfseries,
commentstyle = \itshape,
numbers = left,
numberstyle = \tiny\sffamily,
escapechar = |,
gobble = 2,
}
\begin{document}
\begin{lstlisting}
import numpy as np
from matplotlib import pyplot as plt
t = np.linspace(0, 1, 100) |\label{that-line}|
plt.plot(t, t**2)
plt.show() |\label{that-other-line}|
\end{lstlisting}
Please see line~\ref{that-line} and line~\ref{that-other-line}.
\end{document}
Die aktuelle Ausgabe ist:
Ich möchte, dass die Zahlen, die den Zeilen entsprechen, die \ref
'ed sind (also Zeilen 4 und 6 in meinem MWE), einen bestimmten Stil haben (z. B. in einem Quadrat oder Kreis gesetzt, idealerweise beliebiger tikz
Code). Beispiel:
Antwort1
Eine Anpassung meiner Antwort unterWie füge ich Codelisten referenzierbare nummerierte Kreissymbole hinzu?:
\documentclass{article}
\usepackage{tikz}
\usepackage{listings}
\usepackage{circledsteps}
\pgfkeys{/csteps/outer color=orange}
\lstset{%
basicstyle =\ttfamily,
language = Python,
keywordstyle = \bfseries,
commentstyle = \itshape,
numbers = left,
numberstyle = \tiny\sffamily,
escapechar = |,
gobble = 2,
}
\makeatletter
\newcommand*\CircleNext{%
\lst@AddToHook{OnNewLine}{%
\def\thelstnumber{\Circled{\arabic{lstnumber}}\hskip-2.1pt}}%
}
\def\circlabel#1{
\lst@AddToHook{OnNewLine}{%
\def\thelstnumber{\arabic{lstnumber}}}%
\label{#1}%
}
\makeatother
\begin{document}
\begin{lstlisting}
import numpy as np
from matplotlib import pyplot as plt
|\CircleNext|
t = np.linspace(0, 1, 100) |\circlabel{that-line}|
plt.plot(t, t**2)|\CircleNext|
plt.show() |\circlabel{that-other-line}|
\end{lstlisting}
Please see line~\ref{that-line} and line~\ref{that-other-line}.
\end{document}
Unten eine Variante, die die Befehle nicht benötigt \CircleNext
. Sie funktioniert, indem ein zusätzliches Label geschrieben wird, das aus dem Format besteht listing number-line number
. Anstelle eines OnNewLine
Hooks für Listings \thelstnumber
wird jetzt der Befehl (der die Zeilennummer für jede Zeile ausgibt) so geändert, dass geprüft wird, ob das Label für das aktuelle Listing und die aktuelle Zeile vorhanden ist oder nicht. Wenn das Label vorhanden ist, wird die Nummer eingekreist (beim nächsten Durchlauf).
Leider \thelstnumber
wird auch in die .aux-Datei als Beschriftungstext geschrieben, der von gelesen wird \ref
. Um eingekreiste Zahlen im Haupttext zu vermeiden, besteht eine Lösung darin, \thelstnumber
beim Schreiben des regulären vorübergehend neu zu definieren, sodass nur die Zahl verwendet wird \label
.
Der restliche Code dient der Buchhaltung zum Erstellen und Erhöhen des Listing-Zählers, der im neuen Label verwendet wird.
Code:
\documentclass{article}
\usepackage{tikz}
\newcounter{lstprefix}
\setcounter{lstprefix}{0}
\usepackage{listings}
\AddToHook{env/lstlisting/before}{\stepcounter{lstprefix}}
\usepackage{circledsteps}
\pgfkeys{/csteps/outer color=orange}
\lstset{%
basicstyle =\ttfamily,
language = Python,
keywordstyle = \bfseries,
commentstyle = \itshape,
numbers = left,
numberstyle = \tiny\sffamily,
escapechar = |,
gobble = 2,
}
\makeatletter
\def\thelstnumber{%
\ifcsname r@lst\thelstprefix-\arabic{lstnumber}\endcsname%
\Circled{\arabic{lstnumber}}\hskip-2.1pt%
\else%
\arabic{lstnumber}%
\fi%
}
\def\circlabel#1{
{\def\thelstnumber{\arabic{lstnumber}}\label{#1}}%
\label{lst\thelstprefix-\arabic{lstnumber}}%
}
\makeatother
\begin{document}
\begin{lstlisting}
import numpy as np
from matplotlib import pyplot as plt
t = np.linspace(0, 1, 100) |\circlabel{that-line}|
plt.plot(t, t**2)
plt.show() |\circlabel{that-other-line}|
\end{lstlisting}
Please see line~\ref{that-line} and line~\ref{that-other-line}.
\begin{lstlisting}
import numpy as np
from matplotlib import pyplot as plt |\circlabel{import-line}|
t = np.linspace(0, 1, 100)
plt.plot(t, t**2)
plt.show()
\end{lstlisting}
See also line \ref{import-line}.
\end{document}
Dadurch wird die folgende .aux-Datei erstellt:
\relax
\newlabel{that-line}{{4}{1}}
\newlabel{lst1-4}{{\Circled {4}\hskip -2.1pt}{1}}
\newlabel{that-other-line}{{6}{1}}
\newlabel{lst1-6}{{\Circled {6}\hskip -2.1pt}{1}}
\newlabel{import-line}{{2}{1}}
\newlabel{lst2-2}{{\Circled {2}\hskip -2.1pt}{1}}
\gdef \@abspage@last{1}