考虑以下 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}
当前输出为:
我希望与被\ref
'ed 的行(即我的 MWE 中的第 4 行和第 6 行)相对应的数字具有特定的样式(例如,排版在正方形或圆形内,理想情况下是任意tikz
代码)。例如:
答案1
改编自我的回答如何将可引用的编号圆圈符号添加到代码列表中?:
\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}
下面是一个不需要\CircleNext
命令的变体。它通过编写由格式组成的额外标签来工作listing number-line number
。OnNewLine
现在,命令\thelstnumber
(打印每行的行号)被修改为检查当前列表和当前行的标签是否存在,而不是列表的钩子。如果标签存在,则数字会被圈出(在下一次运行时)。
不幸的\thelstnumber
是 也被写入 .aux 文件作为标签文本,由 读取\ref
。为了防止主文本中出现带圈的数字,一种解决方案是\thelstnumber
在写入常规 时临时重新定义为数字\label
。
其余代码用于记账,创建并增加新标签中使用的列表计数器。
代码:
\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}
这将创建以下 .aux 文件:
\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}