2

I'm trying to automate the command \linelabel in a way that it could create its own label name. Here is a 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}

This gives me: enter image description here

In my thinking, those scattered \markline over the text would be \linelabel{liner1}, \linelabel{liner2}, \linelabel{liner3}, etc. And then, when I call \refline it would call the label accordingly. What am I doing wrong? Sometimes, depending on where I put the command \markline, it works.

2 Answers 2

3

You can fix the code in lineno.sty which does no expansion of the argument to \linelabel; since the arguments are eventually expanded during the next call of the output routine, the counter value used is the last in the page, in your case, 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}

enter image description here

1

I'm not sure how \linelabel works exactly, but the problem seems to be that expansion happens too late. When you call \linelabel{liner\thecountone}, the liner\thecountone part seems to not be expanded immediately. Instead it seems to happen somewhen at the end of the linenumbers environment. At that point \thecountone is 6, so all labels get the same number.

You can make expansion happen immediately, though, using TeX's \edef primitive in the definition of \markline:

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

With the labels defined correctly now, you get the desired result:

enter image description here

Also note that your need ... \refline{} and ... in the last line of the text, otherwise the following space will be gobbled.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .