Repeating code between two commands on a new page

loops

I have a quite complex code where I need to repeat a few lines (consisting of commands, for simplification here in MWE only a few words). In the MWE for printing the same as above in italic. The number of lines to be repeated changes from case to case, so the command should use two other commands (here \begin{document} and \newpage) as parameters for guidance. Can someone help me with this? Thanks a lot already!

\documentclass{article}
\begin{document}

\LaTeX

Hi

Ciao

Bonjour

Hallo

Servus

\LaTeX

\newpage

\textit{
%Here should be a command to reapeat everything between \begin{document} and \newpage to be printed in italic
}
\end{document}

Best Answer

You can use NewEnviron to capture the content with the \BODY macro and reuse it. The two pages appear as:

enter image description here

There is also \NewDocumentEnvironment from xparse, but that is currently marked as experimental:

\NewDocumentEnvironment{MyRepeatingEnvironment}{+b}{%
    #1
    \newpage
    \textit{#1}%
}{}%

This is yielding "Argument of \environment MyRepeatingEnvironment has an extra }", but don't exactly know why.

Code:

\documentclass{article}
\usepackage{environ}

\NewEnviron{MyRepeatingEnvironment}{%
    \BODY
    \newpage
    \textit{\BODY}%
}%

\begin{document}
\begin{MyRepeatingEnvironment}
\LaTeX

Hi

Ciao

Bonjour

Hallo

Servus

\LaTeX
\end{MyRepeatingEnvironment}
\end{document}
Related Question