# Patch the standalone redefinition of document environment in order to automatically add some extra code?

Edit. I rewrite my question as I simplified too much my real problem and the example didn't reflect it.

In the following MWE,

• a custom class myclass.cls:

1. defines a custom \maketitle macro (\mymaketitle) that starts a multicols environment after the usual \maketitle
2. automatically ends the multicols environment at the end of the document,
• a (complete) subfile (sub\jobname.tex) makes use of the custom class and the custom \mymaketitle,

• a 1st main file has the same content as the subfile.

• by contrast to the 1st main file, a 2nd main file inputs the subfile instead of having the same content: though the subfile is complete, it shouldn't be a problem as the custom class loads the standalone package.

Both the 1st main and the sub file compile like a charm but the compilation of the 2nd main file fails with the error:

! LaTeX Error: \begin{multicols} on input line 5 ended by \end{document}.


except if what makes the subfile compilable on its own:

  \documentclass{myclass}
\begin{document}
...
\end{document}


is removed.

%%%%%%%%%%%%%%%%%%%%%
% Class file
%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents*}{myclass.cls}
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{standalone}
\RequirePackage{multicol}
%
%
\newcommand{\mymaketitle}{%
\maketitle
\begin{multicols}{2}
}
%
\AtEndDocument{\end{multicols}}
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%
% Sub file
%%%%%%%%%%%%%%%%%%%%%
\begin{filecontents*}{sub\jobname.tex}
\documentclass{myclass}
\begin{document}
\title{Foo Bar}
\author{Bar Foo}
\mymaketitle
Foo bar bar foo.
\end{document}
\end{filecontents*}
%%%%%%%%%%%%%%%%%%%%%
% Main file 1
%%%%%%%%%%%%%%%%%%%%%
\documentclass{myclass}
%
\begin{document}
\title{Foo Bar}
\author{Bar Foo}
\mymaketitle
Foo bar bar foo.
\end{document}
%%%%%%%%%%%%%%%%%%%%%
% Main file 2
%%%%%%%%%%%%%%%%%%%%%
\documentclass{myclass}
%
\begin{document}
\input{sub\jobname}
\end{document}


I guess the failure of the compilation of the 2nd main file comes from the fact the document environment is redefined by the standalone package to be a simple TeX-group, hence the:

\AtEndDocument{\end{multicols}}


from the custom class is now ignored.

Hence my question: is it possible to patch the standalone redefinition of document environment in order to automatically add some extra code (here \end{multicols})?

• I don't understand the problem. When I add \AtEndDocument{\bigskip Good bye everybody!} to the sub-file and compile the sub-file the text is there. – Ulrike Fischer Mar 9 '15 at 14:41
• Oooops, sorry for the noise: I simplified too much my real problem and the example didn't reflect it. I edited the question with the hope it is clearer now. – Denis Bitouzé Mar 9 '15 at 21:17
• It has nothing to do with the redefinitions of standalone. You would run into the same error if you would put an \end{foo} in your subfile. – Ulrike Fischer Mar 9 '15 at 22:25
• Actually it looks as if standalone contains suitable code. Try \RequirePackage[group=false]{standalone}. – Ulrike Fischer Mar 9 '15 at 22:36
• There is no point at all in \RequirePackage{mwe}. – cfr Sep 16 '15 at 1:09

It looks as if standalone contains suitable code. Try \RequirePackage[group=false]{standalone}.