在 Lineno 包中生成自动标签

在 Lineno 包中生成自动标签

我正在尝试生成自动标签以与包一起使用lineno。为此,我定义了一个新的计数器来与命令中的lineref命令一起使用。问题是该命令不起作用,出于一个我不知道的原因,引用仅适用于最后生成的标签。这是我的 MWE:\linelabel\linelabel{l\thelineref}

\documentclass{scrbook}
\usepackage{lineno}
\usepackage{refcount}

\newcounter{lineref}
\setcounter{lineref}{0}
\newcommand{\linemark}{\refstepcounter{lineref}\linelabel{l\thelineref}}

\begin{document}
    
\begin{linenumbers}
Lorem ipsum dolor sit\linemark amet, consectetur adipiscing elit. Duis mollis magna in erat porta, nec posuere nisi gravida. Quisque tristique dui non ante rhoncus, eu cursus augue\linemark iaculis. Vivamus ut efficitur mauris, sed condimentum metus. Nullam in mattis tortor, nec\linemark blandit mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;\linemark Curabitur elementum sem nulla, nec sagittis enim mattis ac. Maecenas iaculis odio a erat dapibus,\linemark sed sagittis urna aliquam. Pellentesque et risus eu sem\linemark varius imperdiet ut non est. Ut facilisis quam et nibh convallis, ac rutrum magna faucibus.\linemark Pellentesque molestie tempus urna, in\linemark ultricies neque varius eget. Quisque id velit mattis,\linemark tincidunt turpis\linemark id, tempus sapien.
\end{linenumbers}

\par\getrefnumber{l1}
\par\getrefnumber{l2}
\par\getrefnumber{l3}
\par\getrefnumber{l4}
\par\getrefnumber{l5}
\par\getrefnumber{l6}
\par\getrefnumber{l7}
\par\getrefnumber{l8}
\par\getrefnumber{l9}
\par\getrefnumber{l10}
\end{document}

这是我的输出:

在此处输入图片描述

我怎样才能获得正确的数字参考?

答案1

我找到了一个解决方案这个问题

这个技巧只是为了防止命令的扩展:

\documentclass{scrbook}
\usepackage{lineno}
\usepackage{refcount}

\newcounter{lineref}
\setcounter{lineref}{0}
%\newcommand{\linemark}{\refstepcounter{lineref}\linelabel{l\thelineref}}

\newcommand{\linemark}{%
  \stepcounter{lineref}%
  \begingroup\edef\x{\endgroup\noexpand\linelabel{l\thelineref}}\x%
}

\begin{document}
    
\begin{linenumbers}
Lorem ipsum dolor sit\linemark amet, consectetur adipiscing elit. Duis mollis magna in erat porta, nec posuere nisi gravida. Quisque tristique dui non ante rhoncus, eu cursus augue\linemark iaculis. Vivamus ut efficitur mauris, sed condimentum metus. Nullam in mattis tortor, nec\linemark blandit mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae;\linemark Curabitur elementum sem nulla, nec sagittis enim mattis ac. Maecenas iaculis odio a erat dapibus,\linemark sed sagittis urna aliquam. Pellentesque et risus eu sem\linemark varius imperdiet ut non est. Ut facilisis quam et nibh convallis, ac rutrum magna faucibus.\linemark Pellentesque molestie tempus urna, in\linemark ultricies neque varius eget. Quisque id velit mattis,\linemark tincidunt turpis\linemark id, tempus sapien.
\end{linenumbers}

\par\getrefnumber{l1}
\par\getrefnumber{l2}
\par\getrefnumber{l3}
\par\getrefnumber{l4}
\par\getrefnumber{l5}
\par\getrefnumber{l6}
\par\getrefnumber{l7}
\par\getrefnumber{l8}
\par\getrefnumber{l9}
\par\getrefnumber{l10}
\end{document}

相关内容