1

I am trying to duplicate a document using hooks at the beginning and the end. Here is a minimal example of my problem :

\documentclass{minimal}

\usepackage{environ}
\usepackage{forloop}

%Repeat text
\newcounter{loopcntr}
\newcommand{\rpt}[1]{
\forloop{loopcntr}{0}{\value{loopcntr}<2}{#1}
}

%Create an environment to repeat text
\NewEnviron{rptenv}{
    \rpt{\BODY}
}

\AtBeginDocument{
    \begin{rptenv}
}

\AtEndDocument{
    \end{rptenv}
}

\begin{document}
Hello.
\end{document}

Why doesn't it work ? If I replace "rptenv" in the hooks by "center", it works like a charm. Is there another way to achieve an automated repetition without changing anything in the body of the file ?

  • It won't work. Your environment needs to find the string \end{rptenv} to stop the parsing. You can't hide it in \end{document}. tex.stackexchange.com/questions/218589/…. I would put the content of the document in some external file and do the loop over \input{content}. – Ulrike Fischer Aug 27 '15 at 15:13
  • @touhami I forgot to change that to do my minimal example. I made the change. – Florian Aug 27 '15 at 15:56
1

The following seems to work with some tricks:

\documentclass{minimal}

\usepackage{environ}
\usepackage{forloop}

%Repeat text
\newcounter{loopcntr}
\newcommand{\rpt}[1]{
  \forloop{loopcntr}{0}{\value{loopcntr}<3}{#1}
}

%Create an environment to repeat text
\NewEnviron{rptenv}{%
    \rpt{\BODY}%
}

\makeatletter
\g@addto@macro\document{%
    \rptenv
}
\makeatother

\begin{document}
Hello.
\end{document}

\begin{rptenv} does not work inside another environment, because the environment name is used to find the \end{...} part.

The preamble is not yet finished, when the \AtBeginDocument hook is called, therefore the part after the hook of \begin{document} must not be called twice. This is fixed in the example by appending \rptenv to the command form \document.

I have dropped, \endrptenv, because it is not needed here (it would expand to \ignorespacesafterend, but the job is finished by \end{document}, thus spaces afterwards do not matter).

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.