2

if i used \afterpage outside the my custom environment, it works fine:

\documentclass{article}
\usepackage{lipsum, afterpage}
\newenvironment{myenv}{

  some macros that manipulate page numbering

} {

  another macros here

}

\begin{document}
\lipsum[1-10]

\afterpage{
  \begin{myenv}
    \lipsum[3]
  \end{myenv}
}

\lipsum[2]
\end{document}

i want to put the macro \afterpage inside the definition of the environment. i understand that this is necessary to use the macro \NewEnviron from the package environ:

\documentclass{article}
\usepackage{lipsum, afterpage, environ}
\NewEnviron{myenv}{
  \afterpage{
  some macros that manipulate page numbering

\BODY

  another macros here
  }
}

\begin{document}
\lipsum[1-10]

\begin{myenv}
  \lipsum[3]
\end{myenv}

\lipsum[2]
\end{document}

but i get the error:

$ pdflatex my.latex
...
! Undefined control sequence.
\AP@ ...that manipulate page numbering \par \BODY 
                                                  \par another macros here \...
l.20 \lipsum[2]
  • 1
    ! Undefined control sequence. \arterpage are you sure this is not a typo? – clemens Oct 20 '16 at 12:12
  • @clemens, you are right, a typo there, but after its correction, only changed an error message. – aleksandr barakin Oct 20 '16 at 12:24
  • @Werner, 1. file /usr/share/texlive/texmf-dist/tex/latex/environ/environ.sty is from debian package texlive-latex-extra-2012.20120611-2, and contains text: 2008/06/18 v0.2. 2. the same error when using file from texlive-latex-extra-2014.20141024-1, that contains text: 2014/05/04 v0.3 – aleksandr barakin Oct 20 '16 at 13:12
2

You don't want to put the token \BODY into afterpage you want to put all the tokens from the current environment so

\def\afterpagebody#1{%
\afterpage{%%
  some macros that manipulate page numbering%%
   #1%
  another macros here%
  }}

\NewEnviron{myenv}{%%
  \expandafter\afterpagebody\expandafter{\BODY}%%
}

I hope you read the warnings in the afterpage documentation before using it in production:-)

1

\BODY is not visible on the page that follows your myenv environment, hence the "Undefined control sequence" error. It is only available within myenv. To avoid complications with \BODY's accessibility, you can store it in another variable:

\documentclass{article}

\usepackage{lipsum}
\usepackage{afterpage,environ}

\newcounter{myenvcntr}
\NewEnviron{myenv}{%
  \stepcounter{myenvcntr}%
  \expandafter\global\expandafter\let\csname BODY@\themyenvcntr\endcsname\BODY
  \begingroup\edef\x{\endgroup\noexpand\afterpage{\noexpand\setcounter{myenvcntr}{\themyenvcntr}}}\x
  \afterpage{%
  some macros that manipulate page numbering

  \csname BODY@\themyenvcntr\endcsname

  another macros here
  }%
}

\begin{document}

\lipsum[1-10]

\begin{myenv}
This is some text in \texttt{myenv}. \lipsum[1]
\end{myenv}

\lipsum[2]

\end{document}

The above method stores \BODY inside \BODY@<num>, where <num> is incremented in case you do multiple myenvs on the same page.

Be careful about spurious spaces in your definitions.

  • What happens if myenvcntr get incremented twice on the same page? – John Kormylo Apr 12 '18 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.