2

I need to declare an environment with \NewDocumentEnvironment such that every \par provided by the user inside that environment would automatically set an integer variable (inside environment-declaration code) to \prevgraf value. But for some reason \prevgraf is always zero when it's inside \everypar (in environment-declaration code). Of course I can do \par\int_set:Nn\myNum{\prevgraf} (in user-text provided to the environment) but I cannot have anything else there following \par (user is not allowed to directly use code in the text to set internal \myNum value).

\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}

Both \prevgraf values after \par are actually 4, not 0. Why are they zeros, and how to capture the real values without adding any code after \par?

1 Answer 1

7

There are two problems: First, \everypar is executed at the beginning of every paragraph, after the paragraph already started. Because the paragraph already started, \prevgraf is no longer set. Instead you can redefine \par to save the value. Then you have to save the value first before adding any output, otherwise your \textcolor starts a new paragraph and resets \prevgraf.

Independent from the actual problem you shouldn't use \bf with LaTeX. Use \textbf{...} or \bfseries instead.

For example:

\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}

enter image description here

In case you are wondering why there is a 5 instead of a 4 for the second paragraph: The line containing the 4 counts as an additional line in the second paragraph.

3
  • Expl3 interfaces manual shows \cs_set_eq:NN <cs1> <cs2>, so I would never have guessed there must be colon (:) between control sequences. I wonder why this important part is ignored in the manual.
    – bp2017
    Jun 7, 2019 at 5:46
  • 2
    There is no colon inbetween, the colon belongs to the one command. Jun 7, 2019 at 5:59
  • 1
    Commands ending with a colon are functions without arguments. There are a number of such commands in exp3, e.g \group_begin:. Marcel quite correctly added the colon, but as the command is internal it should start with two underscores. Jun 7, 2019 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .