我如何引用一系列行?

我如何引用一系列行?

我希望能够指定一小段文本,可以将其作为输出 PDF 中的一系列行引用,最好使用这种语法:

\documentclass{memoir}
\usepackage{lineno}

\begin{document}
    \runninglinenumbers*
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lacinia posuere
    magna ut imperdiet. \labellines{nunc-pellentesque}{Nunc pellentesque} velit quis
    leo interdum ullamcorper. Ut fringilla sapien ut sem viverra, et rhoncus enim
    lobortis. Morbi diam eros, tristique nec feugiat eu, consequat ac dui. Duis
    molestie, sem id efficitur dignissim, velit metus auctor orci, sit amet mattis
    metus purus in nibh. Etiam mattis, magna ut imperdiet molestie, ex leo viverra
    velit, ac posuere augue est maximus risus. Etiam ac pellentesque nulla. Morbi
    quis pharetra purus. Proin porta, turpis et scelerisque hendrerit, elit eros
    malesuada mi, a imperdiet massa est id nibh. Class aptent taciti sociosqu ad
    litora torquent per conubia nostra, per inceptos himenaeos.
    \labellines{nunc-pharetra}{Nunc pharetra vehicula metus, eu tincidunt arcu
        aliquet et. Aenean ac odio nunc. Etiam ultricies purus non dolor tincidunt, et
        ultricies turpis consequat. Integer vitae ullamcorper eros. Nullam pulvinar
        dictum nunc. Morbi ac justo mi.}

    % \reflines{nunc-pellentesque} should print "line 2"
    ``Nunc pellentesque\ldots'' was on \reflines{nunc-pellentesque}.

    % \reflines{nunc-pharetra} should print "lines 10--13"
    ``Nunc pharetra\ldots'' was on \reflines{nunc-pharetra}.
\end{document}

文本本身看起来是这样的:

没有这些命令的 pdf 输出

答案1

这是一个简单的解决方案,但可能并不安全:

  • 将 a 放在\linelabel{start:#1}封闭的 #2 的开头

  • 将 a 放在\linelabel{end:#1}封闭的 #2 末尾

使用\getrefnumberfromrefcount包并提取行号。原则上\lineref可以这样做,但这不会产生可扩展的数字,因此比较将失败。

我根本没有添加代码来检查标签是否存在!


\documentclass{memoir}


\usepackage{refcount}
\usepackage{lineno}

\newcommand{\labellines}[2]{%
  \linelabel{start:#1}%
  #2%
  \linelabel{end:#1}%
}

\makeatletter
\newcommand{\reflines}[1]{%
  \begingroup
  \edef\temp@a{\getrefnumber{start:#1}}%
  \edef\temp@b{\getrefnumber{end:#1}}%
  \ifnum\temp@a = \temp@b
  line \temp@a%
  \else
  lines \temp@a\ and \temp@b%
  \fi
  \endgroup
}
\makeatother

\begin{document}
    \runninglinenumbers*
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam lacinia posuere
    magna ut imperdiet. \labellines{nunc-pellentesque}{Nunc pellentesque} velit quis
    leo interdum ullamcorper. Ut fringilla sapien ut sem viverra, et rhoncus enim
    lobortis. Morbi diam eros, tristique nec feugiat eu, consequat ac dui. Duis
    molestie, sem id efficitur dignissim, velit metus auctor orci, sit amet mattis
    metus purus in nibh. Etiam mattis, magna ut imperdiet molestie, ex leo viverra
    velit, ac posuere augue est maximus risus. Etiam ac pellentesque nulla. Morbi
    quis pharetra purus. Proin porta, turpis et scelerisque hendrerit, elit eros
    malesuada mi, a imperdiet massa est id nibh. Class aptent taciti sociosqu ad
    litora torquent per conubia nostra, per inceptos himenaeos.
    \labellines{nunc-pharetra}{Nunc pharetra vehicula metus, eu tincidunt arcu
        aliquet et. Aenean ac odio nunc. Etiam ultricies purus non dolor tincidunt, et
        ultricies turpis consequat. Integer vitae ullamcorper eros. Nullam pulvinar
        dictum nunc. Morbi ac justo mi.}

    % \reflines{nunc-pellentesque} should print "line 2"
    ``Nunc pellentesque\ldots'' was on \reflines{nunc-pellentesque}.

    % \reflines{nunc-pharetra} should print "lines 10--13"
    ``Nunc pharetra\ldots'' was on \reflines{nunc-pharetra}.
\end{document}

在此处输入图片描述

相关内容