목록 환경에서 \ref'ed 행에 대한 사용자 정의 번호 스타일

목록 환경에서 \ref'ed 행에 대한 사용자 정의 번호 스타일

다음 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}

현재 출력은 다음과 같습니다:

목록 출력

나는 'ed' 행 \ref(따라서 내 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 파일에서 읽히는 레이블 텍스트로 .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}

관련 정보