編輯 0

編輯 0

我一直在考虑一个更彻底的解决方案问题kthagen 不久前提出了这个问题。是否有可能(也许通过 catcode 劫持)将钩子放在单个线源代码?(更具体地说,在document环境中)

以下是 MWE:

\documentclass{memoir}
\usepackage{libertine}      % want this font
\usepackage{xcolor}         % for color
\usepackage{xspace}

\newcounter{srcline}
\newcommand{\linemark}{%
    \stepcounter{srcline}%
    \textcolor{red}{\textsc{Line}~\arabic{srcline}}%
    \xspace}

\begin{document}
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque porttitor ullamcorper velit, eu
    viverra augue varius in. Integer vehicula lacus
    vitae nunc blandit, sit amet auctor ex volutpat.
    Fusce ultrices elementum est, vel consectetur
    nulla elementum vel. Praesent in elit eu odio
    elementum luctus vel vitae leo. Sed vitae gravida
    nunc. Sed id posuere ligula. Donec a odio
    tincidunt, placerat nisl eget, egestas enim.
    Vivamus eget massa in quam fermentum cursus. In
    dictum, odio in laoreet fringilla, diam purus
    suscipit lorem, sit amet tempus ex lectus eget
    enim. Suspendisse potenti.
\end{document}

我希望创造这样的东西:

理想的

每行源代码调用\linemark一次。有没有办法只使用 LaTeX2e 来实现这一点?


編輯 0

尽量不要吞噬段落,MWE:

\documentclass{memoir}
\usepackage{libertine}      % want this font
\usepackage{xcolor}         % for color
\usepackage{xspace}

\newcounter{srcline}
\newcommand{\linemark}{%
    \stepcounter{srcline}%
    \textcolor{red}{\textsc{Line}~\arabic{srcline}}%
    \xspace}

\begin{document}
    \catcode`\^^M\active%
    \let\oldLF\^^M%
    \def
    {\oldLF\linemark}
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque porttitor ullamcorper velit, eu
    viverra augue varius in. Integer vehicula lacus
    vitae nunc blandit, sit amet auctor ex volutpat.
    Fusce ultrices elementum est, vel consectetur
    nulla elementum vel. Praesent in elit eu odio
    elementum luctus vel vitae leo. Sed vitae gravida
    nunc. Sed id posuere ligula. Donec a odio
    tincidunt, placerat nisl eget, egestas enim.
    Vivamus eget massa in quam fermentum cursus. In
    dictum, odio in laoreet fringilla, diam purus
    suscipit lorem, sit amet tempus ex lectus eget
    enim. Suspendisse potenti.

    % my solution does gobble the paragraph break above.
    % add \par after this comment to see how I want it
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque porttitor ullamcorper velit, eu
    viverra augue varius in. Integer vehicula lacus
    vitae nunc blandit, sit amet auctor ex volutpat.
    Fusce ultrices elementum est, vel consectetur
    nulla elementum vel. Praesent in elit eu odio
    elementum luctus vel vitae leo. Sed vitae gravida
    nunc. Sed id posuere ligula. Donec a odio
    tincidunt, placerat nisl eget, egestas enim.
    Vivamus eget massa in quam fermentum cursus. In
    dictum, odio in laoreet fringilla, diam purus
    suscipit lorem, sit amet tempus ex lectus eget
    enim. Suspendisse potenti.
\end{document}

答案1

现在您添加了一个包含所有要求的新答案,我创建了一个 expl3 解决方案。它附带一个额外的功能,这很重要全部行(所以如果你留下两行空白,那么6号线 7号线 8号线已添加)。

\documentclass{scrartcl}
\usepackage{xparse,xcolor}

\newcounter{srcline}

\ExplSyntaxOn
\NewDocumentEnvironment{scrlines} { }
 { \endgraf \char_set_active_eq:NN \^^M \kuang_active_lf: }
 { \endgraf }
\tl_clear_new:N \l_kuang_cumulativelines_tl
\tl_new:N \l_kuang_linemark_tl
\tl_set:Nn \l_kuang_linemark_tl
 {
  \stepcounter{srcline}
  \textcolor{red}{\textsc{Line\nobreakspace\thesrcline}}
 }
\cs_new_protected_nopar:Npn \kuang_active_lf:
 {
  \kuang_peek_active_lf:TF
   {
    \par
    \tl_put_right:Nn \l_kuang_cumulativelines_tl { ~ \l_kuang_linemark_tl }
    \kuang_active_lf:
   }
   {
    \tl_use:N \l_kuang_cumulativelines_tl
    \tl_clear:N \l_kuang_cumulativelines_tl
    \c_space_tl
    \tl_use:N \l_kuang_linemark_tl
    \c_space_tl
   }
 }
\group_begin:
 \char_set_catcode_active:N \^^M %
 \cs_new_protected_nopar:Npn \kuang_peek_active_lf:TF %
  { \peek_meaning_remove:NTF ^^M } %
\group_end:

\ExplSyntaxOff

\begin{document}
\begin{scrlines}
  Lorem ipsum dolor sit amet, consectetur adipiscing
  elit. Quisque porttitor ullamcorper velit, eu
  viverra augue varius in. Integer vehicula lacus
  vitae nunc blandit, sit amet auctor ex volutpat.
  Fusce ultrices elementum est, vel consectetur
  nulla elementum vel. Praesent in elit eu odio


  elementum luctus vel vitae leo. Sed vitae gravida
  nunc. Sed id posuere ligula. Donec a odio
  tincidunt, placerat nisl eget, egestas enim.
  Vivamus eget massa in quam fermentum cursus. In
  dictum, odio in laoreet fringilla, diam purus
  suscipit lorem, sit amet tempus ex lectus eget
  enim. Suspendisse potenti.%
\end{scrlines}
\end{document}

在此处输入图片描述


原始解决方案

既然你有,\obeylines你就可以利用它。

\documentclass{memoir}
\usepackage{xcolor}

\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
    \textcolor{red}{\textsc{Line~\thesrcline}}}

% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\scrlinepar{\@ifnextchar{^^M}{\endgraf\expandafter\scrlinepar\@gobble}{ \linemark\ }}}
\makeatother

\newenvironment{scrlines}
  {\endgraf\let\par\scrlinepar\obeylines}
  {\endgraf}

\begin{document}
  \begin{scrlines}
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque porttitor ullamcorper velit, eu
    viverra augue varius in. Integer vehicula lacus
    vitae nunc blandit, sit amet auctor ex volutpat.
    Fusce ultrices elementum est, vel consectetur
    nulla elementum vel. Praesent in elit eu odio

    elementum luctus vel vitae leo. Sed vitae gravida
    nunc. Sed id posuere ligula. Donec a odio
    tincidunt, placerat nisl eget, egestas enim.
    Vivamus eget massa in quam fermentum cursus. In
    dictum, odio in laoreet fringilla, diam purus
    suscipit lorem, sit amet tempus ex lectus eget
    enim. Suspendisse potenti.%
  \end{scrlines}
\end{document}

在此处输入图片描述

答案2

我不确定这个解决方案有多么强大,但我只是重新定义了catcode换行符以使其处于活动状态,然后将新命令附加到它。

\documentclass{memoir}
\usepackage{libertine}      % want this font
\usepackage{xcolor}         % for color
\usepackage{xspace}

\newcounter{srcline}
\newcommand{\linemark}{%
    \stepcounter{srcline}%
    \textcolor{red}{\textsc{Line}~\arabic{srcline}}%
    \xspace}

\begin{document}
    \catcode`\^^M\active%
    \let\oldLF\^^M%
    \def
    {\oldLF\linemark}
    Lorem ipsum dolor sit amet, consectetur adipiscing
    elit. Quisque porttitor ullamcorper velit, eu
    viverra augue varius in. Integer vehicula lacus
    vitae nunc blandit, sit amet auctor ex volutpat.
    Fusce ultrices elementum est, vel consectetur
    nulla elementum vel. Praesent in elit eu odio
    elementum luctus vel vitae leo. Sed vitae gravida
    nunc. Sed id posuere ligula. Donec a odio
    tincidunt, placerat nisl eget, egestas enim.
    Vivamus eget massa in quam fermentum cursus. In
    dictum, odio in laoreet fringilla, diam purus
    suscipit lorem, sit amet tempus ex lectus eget
    enim. Suspendisse potenti.
\end{document}

结果

答案3

对 Manuel 的答案进行了轻微的修改,以便跨段落跟踪行。

\documentclass{memoir}
\usepackage{xcolor}

\newcounter{srcline}
\newcommand{\linemark}{\stepcounter{srcline}%
    \textcolor{red}{\textsc{Line~\thesrcline}}}

% Playing with the gobbling of \par
\makeatletter
{\obeylines\gdef\srclinepar{\@ifnextchar{^^M}{\endgraf\linemark\expandafter\srclinepar\@gobble}{ \linemark\ }}}
\makeatother

\newenvironment{srclines}
{\endgraf\let\par\srclinepar\obeylines}
{\endgraf}

\begin{document}
    \begin{srclines}
        Lorem ipsum dolor sit amet, consectetur adipiscing
        elit. Quisque porttitor ullamcorper velit, eu
        viverra augue varius in. Integer vehicula lacus
        vitae nunc blandit, sit amet auctor ex volutpat.
        Fusce ultrices elementum est, vel consectetur
        nulla elementum vel. Praesent in elit eu odio

        elementum luctus vel vitae leo. Sed vitae gravida
        nunc. Sed id posuere ligula. Donec a odio
        tincidunt, placerat nisl eget, egestas enim.
        Vivamus eget massa in quam fermentum cursus. In
        dictum, odio in laoreet fringilla, diam purus
        suscipit lorem, sit amet tempus ex lectus eget
        enim. Suspendisse potenti.%
    \end{srclines}
\end{document}

相关内容