1

I have issue with this MWE

\documentclass{article}
\usepackage{lineno}

\parindent=0pt
\renewcommand{\thelinenumber}{-\arabic{linenumber}-}

\begin{document}
\linenumbers
Text \thelinenumber\ text \\
Text \thelinenumber\ text \\
Text \thelinenumber\ text \par
Text \thelinenumber\ text 

\end{document}

enter image description here

Counter linenumber not increased within lines except after \par, contrary to the line number shown on the left side of the page.

How I can display or use line number (for example in conditional) inside lines?

2
  • This is similar to other counters like \thepage, which is not fully known at the time of setting it. From the lineno user manual: "Whenever you want to attach something to your text, after a paragraph was broken into lines, across pagebreaks, lineno may do the job." Since you're using \\ to denote a line-break, the paragraph has not been broken into lines. This only happens with the occurrence of \par or a blank line. What about a \label-\ref to extract the correct number?
    – Werner
    Commented Sep 30, 2016 at 17:02
  • I want to use line number in conditional \ifnum like this \ifnum\value{linenumber}=2 we are in the second line \fi
    – Salim Bou
    Commented Sep 30, 2016 at 17:10

1 Answer 1

4

For extracting the ordinal representation of a line number, you can set a \linelabel and retrieve it using a combination of regular \getrefnumber (from refcount; to avoid extracting it to a counter) and \ordinalnum (from fmtcount).

enter image description here

\documentclass{article}

\usepackage{lineno,refcount,fmtcount,xcolor}

\setlength{\parindent}{0pt}
\renewcommand{\thelinenumber}{-\arabic{linenumber}-}
\renewcommand{\theLineNumber}{\arabic{linenumber}}

\newcounter{linecntr}
\newcommand{\ordinalline}{%
  \stepcounter{linecntr}%
  \begingroup\edef\x{\endgroup\noexpand\linelabel{linecntr-\thelinecntr}}\x%
  \textcolor{red}{\ordinalstringnum{\getrefnumber{linecntr-\thelinecntr}}}}

\begin{document}
\linenumbers

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed dapibus 
facilisis magna, id sodales ante convallis vitae. In a porttitor nibh, 
We are in the \ordinalline{} line.
eget vehicula dui. Integer feugiat dui nec neque porttitor, non eleifend 
erat lobortis. Sed varius malesuada nisi, id vulputate ante commodo vitae. 
Nam non felis at ante mollis commodo eget porttitor ex. Donec in quam diam. 
We are in the \ordinalline{} line.
Maecenas fringilla, turpis a egestas pulvinar, massa lacus molestie enim, 
vel vulputate lorem ante ac velit. Nam ex nulla, venenatis et tortor at, 
We are in the \ordinalline{} line.
efficitur fringilla ante.

\end{document}
0

You must log in to answer this question.

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