我需要声明一个环境,\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 的行算作第二段中的附加行。