每次文本以 \par 结尾时,如何自动捕获 \prevgraf 值(在 xparse 环境内)?

每次文本以 \par 结尾时,如何自动捕获 \prevgraf 值(在 xparse 环境内)?

我需要声明一个环境,\NewDocumentEnvironment以便\par该环境中用户提供的每个内容都会自动将一个整数变量(在环境声明代码中)设置为\prevgraf值。但由于某种原因,\prevgraf当它在内部时(在环境声明代码中)始终为零\everypar。当然我可以这样做\par\int_set:Nn\myNum{\prevgraf}(在提供给环境的用户文本中),但我不能在下面做任何其他事情\par(用户不能直接使用文本中的代码来设置内部\myNum值)。

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{xcolor}
\begin{document}
\ExplSyntaxOn

\int_new:N\myNum % THIS SHOULD HOLD LAST \prevgraf
\NewDocumentEnvironment{myEnv}{+b}
  { \parbox{\hsize}
      { \everypar
          { % simply display \prevgraf value (in red)
            \textcolor{red}
              { \bf\the\prevgraf\newline
              }
             % capturing prevgraf here doesn't work because prevgraf is always zero
             \int_set:Nn\myNum{\prevgraf}
          }
        #1 % DISPLAY ENVIRONMENT CONTENTS
      }
  } {}

\ExplSyntaxOff

\begin{myEnv}
  When PNG development started in early 1995, developers decided
   not to incorporate support for animation, because the majority
    of the PNG developers felt that overloading a single file type
     with both still and animation features is a bad design.
      \par % WE CAN CAPTURE HERE MANUALLY BUT WE WON'T,
       %  BECAUSE WE NEED TO CAPTURE AUTOMATICALLY WITH \par ALONE
  PNG supports palette-based images (with palettes of 24-bit RGB or
   32-bit RGBA colors), grayscale images (with or without alpha channel
    for transparency), and full-color non-palette-based RGB/RGBA images
     (with or without alpha channel).
      \par % WE CAN CAPTURE HERE MANUALLY BUT WE WON'T,
       %  BECAUSE WE NEED TO CAPTURE AUTOMATICALLY WITH \par ALONE
\end{myEnv}

\end{document}

后面的两个\prevgraf\par实际上都是 4,而不是 0。为什么它们是零,以及如何在不添加任何代码的情况下捕获真实值\par

答案1

有两个问题:首先,\everypar在每个段落的开头执行,在段落已经开始之后。因为段落已经开始,\prevgraf所以不再设置。相反,您可以重新定义\par以保存该值。然后,您必须先保存该值,然后再添加任何输出,否则您\textcolor将开始一个新段落并重置\prevgraf

独立于您不应该使用 LaTeX 的实际问题\bf。请改用\textbf{...}\bfseries

例如:

\documentclass[varwidth]{standalone}
\usepackage{expl3}
\usepackage{xparse}
\usepackage{xcolor}
\begin{document}
\ExplSyntaxOn

\int_new:N\l__my_int % THIS SHOULD HOLD LAST \prevgraf
\NewDocumentEnvironment{myEnv}{+b}
  { \parbox{\hsize}
      { \cs_set_eq:NN \__bp_preserved_par: \par
        \cs_set:Npn \par {
          \__bp_preserved_par:
          \int_set:Nn \l__my_int { \prevgraf }
          \textcolor{red}
            { \textbf
              { \int_use:N \l__my_int \newline
              }
            }
        }
        #1 % DISPLAY ENVIRONMENT CONTENTS
      }
  } {}

\ExplSyntaxOff

\begin{myEnv}
  When PNG development started in early 1995, developers decided
   not to incorporate support for animation, because the majority
    of the PNG developers felt that overloading a single file type
     with both still and animation features is a bad design.
      \par % WE CAN CAPTURE HERE MANUALLY BUT WE WON'T,
       %  BECAUSE WE NEED TO CAPTURE AUTOMATICALLY WITH \par ALONE
  PNG supports palette-based images (with palettes of 24-bit RGB or
   32-bit RGBA colors), grayscale images (with or without alpha channel
    for transparency), and full-color non-palette-based RGB/RGBA images
     (with or without alpha channel).
      \par % WE CAN CAPTURE HERE MANUALLY BUT WE WON'T,
       %  BECAUSE WE NEED TO CAPTURE AUTOMATICALLY WITH \par ALONE
\end{myEnv}

\end{document}

在此处输入图片描述

如果你想知道为什么第二段是 5 而不是 4:包含 4 的行算作第二段中的附加行。

相关内容