对于老年人xparse

对于老年人xparse

是否有一个包\somecommand可以逐步显示文本。我正在讨论像文章这样的常规文档类。

\documentclass{article}

\begin{document}

This is some text. % I would like all text before this point appear at the first page.
\somecommand This is some text. % I would like all text before this point appear at the second page.
\somecommand This is some text. % I would like all text before this point appear at the third page.
\somecommand This is some text. % etc

\end{document}

答案1

您可以使用类似这样的方法:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\tl_new:N \g__yourpkg_all_text_tl

\msg_new:nnn { yourpkg } { invalid-use-of-marker }
  {
    \token_to_str:c {#1}~should~not~be~expanded~when~used~correctly.
    There~is~probably~an~error~before~its~use.
  }

% Try to be helpful when \endPage is not found in the expected place.
\cs_new_protected:Npn \endPage
  { \msg_error:nnn { yourpkg } { invalid-use-of-marker } { endPage } }

\cs_new_protected:Npn \__yourpkg_process_one:w #1\endPage
  {
    \tl_gput_right:Nn \g__yourpkg_all_text_tl { #1 \par }
    \tl_use:N \g__yourpkg_all_text_tl
    \newpage
    \peek_meaning:NTF \q_stop
      { \use_none:n }
      { \__yourpkg_process_one:w }
  }

\NewDocumentEnvironment { progressive } { +b }
  {
    \tl_gclear:N \g__yourpkg_all_text_tl
    \__yourpkg_process_one:w #1 \endPage \q_stop
  }
  { }
\ExplSyntaxOff

\begin{document}

\begin{progressive}
  First piece of text.
  \endPage Second piece of text.
  \endPage Third piece of text.
  \endPage Fourth piece of text.
\end{progressive}

\end{document}

在此处输入图片描述

根据您想要用它做什么,您可能需要添加\unskip在某些地方添加;但是,这在段落末尾不是必需的,并且\endPage确实用上面的代码结束了一个段落。

对于老年人xparse

如果您的xparse软件包早于 2019-03-05,它将无法识别b-type 参数。如果发生这种情况,请升级xparse或添加\usepackage{environ}并替换:

\NewDocumentEnvironment { progressive } { +b }
  {
    \tl_gclear:N \g__yourpkg_all_text_tl
    \__yourpkg_process_one:w #1 \endPage \q_stop
  }
  { }

\NewEnviron { progressive }
  {
    \tl_gclear:N \g__yourpkg_all_text_tl
    \exp_after:wN \__yourpkg_process_one:w \BODY \endPage \q_stop
  } []

其他可能的语法

以下是答案的第一个版本,其中有两处更改:

  • vartl现在是全局的;

  • \startProgressive清除它(因此,它“重新开始”)。

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\tl_new:N \g__yourpkg_all_text_tl

\msg_new:nnn { yourpkg } { invalid-use-of-marker }
  {
    \token_to_str:c {#1}~should~not~be~expanded~when~used~correctly.
    There~is~probably~an~error~before~its~use.
  }

% Try to be helpful when \endPage or \endProgressive is not found in the
% expected place.
\cs_new_protected:Npn \endPage
  { \msg_error:nnn { yourpkg } { invalid-use-of-marker } { endPage } }

\cs_new_protected:Npn \endProgressive
  { \msg_error:nnn { yourpkg } { invalid-use-of-marker } { endProgressive } }

\cs_new_protected:Npn \__yourpkg_process_one:w #1\endPage
  {
    \tl_gput_right:Nn \g__yourpkg_all_text_tl { #1 \par }
    \tl_use:N \g__yourpkg_all_text_tl
    \newpage
    \peek_meaning_ignore_spaces:NTF \endProgressive
      { \use_none:n }
      { \__yourpkg_process_one:w }
  }

\NewDocumentCommand \startProgressive { }
  {
    \tl_gclear:N \g__yourpkg_all_text_tl
    \__yourpkg_process_one:w
  }
\ExplSyntaxOff

\begin{document}

\startProgressive
First piece of text.
\endPage Second piece of text.
\endPage Third piece of text.
\endPage Fourth piece of text.
\endPage
\endProgressive

\end{document}

(与上面相同的输出)。

相关内容