我有许多用计数器设置的数字注释的代码示例,lstNoteCounter
并在代码下方的文本中引用:我的标记有效,但对于大型文档来说并不方便:
\documentclass{article}
\usepackage{listings}
\lstset{
basicstyle=\footnotesize\ttfamily, % Standardschrift
escapechar=¢,
language=python,
}
\usepackage{pifont} % dings for margin numbers
\usepackage{ifthen}
\newcounter{lstNoteCounter}
\newcommand{\lnnum}[1]{% Print pifont circled number for line label
\noindent
\ifcase#1%
% nothing for 0
\or\normalsize\ding{202}%
\or\normalsize\ding{203}%
\or\normalsize\ding{204}%
\or\normalsize\ding{205}% % in practice, we continue to 9
\else{NUM TOO HIGH}%
\fi%
}
\newcommand*{\lnote}{%
\stepcounter{lstNoteCounter}\vbox{\llap{{\lnnum{\thelstNoteCounter}}\hskip 1em}}%
}
\newcommand*{\lnoteref} {%
\stepcounter{lstNoteCounter}{\lnnum{\thelstNoteCounter}\hskip 0.5em}%
}
\begin{document}
\begin{lstlisting}[frame=lines]
¢\lnote¢def cosec(x):
return 1/math.sin(x)
¢\lnote¢csc = cosec
print(csc(math.pi/3))
\end{lstlisting}
% I have to reset manually HERE:
\setcounter{lstNoteCounter}{0}
\lnoteref There is no \texttt{cosec} function in Python's \texttt{math} module, so we define one.
\lnoteref Python functions are objects so we can assign them to identifiers.
% I have to reset manually HERE:
\setcounter{lstNoteCounter}{0}
\begin{lstlisting}[frame=lines]
¢\lnote¢def sec(x):
return 1/math.cos(x)
¢\lnote¢print(sec(math.pi/3))
\end{lstlisting}
% I have to reset manually HERE:
\setcounter{lstNoteCounter}{0}
\lnoteref There is no \texttt{sec} function either, so define one.
\lnoteref Will print \texttt{2}.
\end{document}
我怎样才能在或环境lstNoteCounter
之前和之后自动重置,这样我就不必每次都手动发出命令?lstlisting
lstinputlisting
\setcounter{lstNoteCounter}{0}
答案1
我认为最好使用\label
-\ref
机制。如果您改变了对编号的想法,只需更改 的定义\lnnumding
。将计数器链接到 ,mocklisting
每次调用 时python
都会将其重置为零。
通过设置标签,您可以获得更大的灵活性(但必须提供标签)。
\documentclass{article}
\usepackage{listings}
\usepackage{pifont} % dings for margin numbers
\lstset{
basicstyle=\footnotesize\ttfamily, % Standardschrift
escapechar=¢,
language=python,
}
\lstnewenvironment{python}[1][]
{\stepcounter{mocklisting}\lstset{#1}}
{}
\newcounter{mocklisting}
\newcounter{lstNoteCounter}[mocklisting]
\renewcommand{\thelstNoteCounter}{\lnnumding{\arabic{lstNoteCounter}}}
\DeclareRobustCommand{\lnnumding}[1]{\mbox{\normalsize\ding{\number\numexpr201+#1}}}
\newcommand*{\lnote}[1]{%
\refstepcounter{lstNoteCounter}\label{#1}%
\makebox[0pt][r]{\thelstNoteCounter\hspace{1em}}%
}
\newcommand*{\lnoteref}[1]{\ref{#1}\hspace{.5em}\ignorespaces}
\begin{document}
\begin{python}[frame=lines]
¢\lnote{A}¢def cosec(x):
return 1/math.sin(x)
¢\lnote{B}¢csc = cosec
print(csc(math.pi/3))
\end{python}
\lnoteref{A} There is no \texttt{cosec} function in Python's \texttt{math} module,
so we define one.
\lnoteref{B} Python functions are objects so we can assign them to identifiers.
\begin{python}[frame=lines]
¢\lnote{C}¢def sec(x):
return 1/math.cos(x)
¢\lnote{D}¢print(sec(math.pi/3))
\end{python}
\lnoteref{C} There is no \texttt{sec} function either, so define one.
\lnoteref{D} Will print \texttt{2}.
\end{document}
答案2
我认为最好的方法是定义一个新的列表环境,在其开始和结束时执行此操作。
例如,我们称之为mylisting
:
\lstnewenvironment{mylisting}[1][]
{\lstset{%
basicstyle=\footnotesize\ttfamily, % Standardschrift
escapechar=¢,
language=python,
#1
}%
\setcounter{lstNoteCounter}{0}}
{\setcounter{lstNoteCounter}{0}}
并在您的文档中使用mylisting
而不是lstlisting
。
梅威瑟:
\documentclass{article}
\usepackage{listings}
\lstnewenvironment{mylisting}[1][]
{\lstset{%
basicstyle=\footnotesize\ttfamily, % Standardschrift
escapechar=¢,
language=python,
#1
}%
\setcounter{lstNoteCounter}{0}}
{\setcounter{lstNoteCounter}{0}}
\usepackage{pifont} % dings for margin numbers
\usepackage{ifthen}
\newcounter{lstNoteCounter}
\newcommand{\lnnum}[1]{% Print pifont circled number for line label
\noindent
\ifcase#1%
% nothing for 0
\or\normalsize\ding{202}%
\or\normalsize\ding{203}%
\or\normalsize\ding{204}%
\or\normalsize\ding{205}% % in practice, we continue to 9
\else{NUM TOO HIGH}%
\fi%
}
\newcommand*{\lnote}{%
\stepcounter{lstNoteCounter}\vbox{\llap{{\lnnum{\thelstNoteCounter}}\hskip 1em}}%
}
\newcommand*{\lnoteref} {%
\stepcounter{lstNoteCounter}{\lnnum{\thelstNoteCounter}\hskip 0.5em}%
}
\begin{document}
\begin{mylisting}[frame=lines]
¢\lnote¢def cosec(x):
return 1/math.sin(x)
¢\lnote¢csc = cosec
print(csc(math.pi/3))
\end{mylisting}
\lnoteref There is no \texttt{cosec} function in Python's \texttt{math} module, so we define one.
\lnoteref Python functions are objects so we can assign them to identifiers.
\begin{mylisting}[frame=lines]
¢\lnote¢def sec(x):
return 1/math.cos(x)
¢\lnote¢print(sec(math.pi/3))
\end{mylisting}
\lnoteref There is no \texttt{sec} function either, so define one.
\lnoteref Will print \texttt{2}.
\end{document}
输出: