在清單環境中引用的行的自訂數字樣式

在清單環境中引用的行的自訂數字樣式

考慮以下 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}

當前輸出為:

列表輸出

我希望與被編輯的行(即我的 MWE 中的第 4 行和第 6 行)相對應的數字\ref具有特定的樣式(例如,在正方形或圓形內排版,最好是任意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 numberOnNewLine現在,命令(列印每行的行號)不再是清單的掛鉤,\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}

相關內容