行号在“lineno”包内

行号在“lineno”包内

我对这个 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}

在此处输入图片描述

除 之后的行外,其他行内的计数器linenumber均未增加\par,与页面左侧显示的行号相反。

如何在行内显示或使用行号(例如在条件中)?

答案1

为了提取序数行号的表示,你可以设置一个\linelabel,并使用常规的组合来检索它\getrefnumber(从refcount;避免将其提取到计数器)和\ordinalnum(从fmtcount)。

在此处输入图片描述

\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}

相关内容