简单的“lineno”宏不起作用

简单的“lineno”宏不起作用

\linelabel我正在尝试以一种可以创建自己的标签名称的方式自动化命令。以下是 MWE:

\documentclass{article}
\usepackage[modulo]{lineno}
\newcounter{countone}
\newcounter{counttwo}
\newcommand{\markline}{\stepcounter{countone}\linelabel{liner\thecountone}}
\newcommand{\refline}{\stepcounter{counttwo}\ref{liner\thecounttwo}}

\begin{document}

\begin{linenumbers}
``Hello, here is some\markline text without a meaning.\\ 
This text\markline should show, how a printed text will\\ 
look like at this place. If you read\markline this text, you will get\\ 
no information. Really\markline? Is there no information?\\ 
Is there a difference\markline between this text and some\\ 
nonsense like ``Huardest gefburn''? Kjift – Never mind\markline!'' 
\end{linenumbers}

Here, I have the marked lines: \refline, \refline, \refline, \refline and \refline.
\end{document}

这给了我: 在此处输入图片描述

在我看来,分散\markline在文本中的将是\linelabel{liner1}、、等。然后,当我调用它时\linelabel{liner2},会相应地调用标签。我做错了什么?有时,根据我将命令放在哪里,它会起作用。\linelabel{liner3}\refline\markline

答案1

lineno.sty您可以修复不将参数扩展为的代码\linelabel;因为参数最终会在下次调用输出例程时扩展,所以使用的计数器值是页面中的最后一个值,在您的情况下为 6。

\documentclass{article}
\usepackage[modulo]{lineno}
\newcounter{countone}
\newcounter{counttwo}
\newcommand{\markline}{\stepcounter{countone}\linelabel{liner\thecountone}}
\newcommand{\refline}{\stepcounter{counttwo}\ref{liner\thecounttwo}}

\makeatletter
\def\@LN@postlabel#1{%
  \begingroup\edef\x{\endgroup
    % make sure to expand the argument to \linelabel before adding it
    % to \@LN@labellist
    \noexpand\g@addto@macro\noexpand\@LN@labellist{#1\noexpand\@lt}%
  }\x
  \vadjust{\penalty-\@Mllbcodepen}%
}
\makeatother


\begin{document}

\begin{linenumbers}
``Hello, here is some\markline text without a meaning.\\ 
This text\markline should show, how a printed text will\\ 
look like at this place. If you read\markline this text, you will get\\ 
no information. Really\markline? Is there no information?\\ 
Is there a difference\markline between this text and some\\ 
nonsense like ``Huardest gefburn''? Kjift – Never mind\markline!'' 
\end{linenumbers}

Here, I have the marked lines: \refline, \refline, \refline, \refline\ and \refline.
\end{document}

在此处输入图片描述

答案2

我不确定\linelabel具体是如何工作的,但问题似乎是扩展发生得太晚了。当您调用时\linelabel{liner\thecountone},该liner\thecountone部分似乎不会立即扩展。相反,它似乎发生在环境结束时的某个时候linenumbers。那时\thecountone6,所以所有标签都得到相同的数字。

\edef但是,你可以使用定义中的TeX 原语立即进行扩展\markline

\newcommand{\markline}{%
    \stepcounter{countone}%
    \edef\temp{liner\thecountone}%
    \expandafter\linelabel\expandafter{\temp}%
}

现在标签定义正确了,您就可以得到所需的结果:

在此处输入图片描述

还要注意... \refline{} and ...文本最后一行的需求,否则下面的空间将被吞噬。

相关内容