texlive[56888] Master/texmf-dist: subfiles (8nov20)

commits+karl at tug.org commits+karl at tug.org
Sun Nov 8 22:43:17 CET 2020


Revision: 56888
          http://tug.org/svn/texlive?view=revision&revision=56888
Author:   karl
Date:     2020-11-08 22:43:17 +0100 (Sun, 08 Nov 2020)
Log Message:
-----------
subfiles (8nov20)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/latex/subfiles/subfiles.pdf
    trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
    trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls
    trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty

Modified: trunk/Master/texmf-dist/doc/latex/subfiles/subfiles.pdf
===================================================================
(Binary files differ)

Modified: trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
===================================================================
--- trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx	2020-11-08 21:43:02 UTC (rev 56887)
+++ trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx	2020-11-08 21:43:17 UTC (rev 56888)
@@ -23,12 +23,12 @@
 %
 %<*driver>
 % \fi
-\ProvidesFile{subfiles.dtx}[2020/09/07 v2.0 Multi-file projects]
+\ProvidesFile{subfiles.dtx}[2020/10/29 v2.1 Multi-file projects]
 % \iffalse
 \documentclass{ltxdoc}
 \GetFileInfo{subfiles.dtx}
 \title{A Document Class and a Package\\for Handling Multi-File Projects}
-\date{2020/09/07 v2.0}
+\date{2020/10/29 v2.1}
 \author{Federico Garcia, Gernot Salzer}
 \usepackage{hyperref}
 \begin{document}
@@ -200,8 +200,8 @@
 % The preamble of subfiles is skipped when loaded with |\subfile|, and everything after |\end{document}| is ignored.
 % In most cases this is what you want. 
 %
-% \DescribeMacro{v1}
-% For reasons of compatibility, the option |v1| restores the behaviour of older versons:
+% \DescribeMacro{[v1]}
+% For reasons of compatibility, the option |v1| restores the behaviour of previous versions.
 % \begin{center}
 %   |\usepackage[v1]{subfiles}|
 % \end{center}
@@ -208,7 +208,7 @@
 % This will have three effects.
 %
 % \emph{Code after the end of the main document} is added to the preamble of the subfiles, but is ignored when typesetting the main file.
-% Here one can add commands that are to be processed as part of the preamble when the subfiles are typeset on their one.
+% Here, one can add commands that are to be processed as part of the preamble when the subfiles are typeset on their one.
 % But this also means that any syntax error after |\end{document}| will ruin the \LaTeX ing of the subfile(s).
 %
 % \emph{Code in the preamble of a subfile} is processed as part of the text when typesetting the main file, but as part of the preamble when typesetting the subfile.
@@ -279,7 +279,26 @@
 % \end{center}
 % Then each of the three files can be typeset individually in its respective directory, where \LaTeX\ is able to locate all included text files and images.
 %
+% \subsection{Cross-referencing between subfiles}
+% When working with multiple subfiles under a main file, say |main.tex|, one may want to refer in subfile |A.tex| to labels in subfile |B.tex|. To make this work, load the package |xr| in the preamble of the main file and add an |\externaldocument| command after loading the |subfiles| package:
+% \begin{center}
+%   \begin{tabular}{l}
+%     |\usepackage{xr}| \\
+%     |\usepackage{subfiles}| \\
+%     |\externaldocument[M-]{\subfix{main}}|  
+%   \end{tabular}
+% \end{center}
+% In the |\externaldocument| command, |main| is the name of the main document.
+% Moreover, |M-| is an arbitrary sequence of characters that is added as prefix to the labels.
+% The |\subfix| command is only needed if the subfiles are not in the same directory as the main file, but it doesn't hurt if you add it in any case.
 %
+% To cross-reference between documents, add labels as usual.
+% Suppose you have |\label{mylabel}| in any of the files.
+% Then you can use |\ref{M-mylabel}| and  |\pageref{M-mylabel}| to obtain the (page) number that the label refers to in the main document.
+%
+% Note that you first have to compile |main.tex|.
+% This generates |main.aux|, which then can be loaded by the subfiles to provide the information for the labels prefixed with |M-|.
+%
 % \subsection{Avoiding extra spaces}
 %
 % Sometimes you may want to load the contents of a subfile without white space separating it from the contents of the main file.
@@ -323,39 +342,40 @@
 % \item
 %   Make sure that |\usepackage{subfiles}| appears near the end of the main preamble.
 % \item
-%   Make sure that the strings |\begin{document}| and |\end{document}| appear on lines of their own and that there are no additional characters preceding or trailing them.
-% \item
 %   If some external program that cooperates with \TeX, like |bibtex| or |biber|, complains about not being able to find a file, locate the name of the file in the \LaTeX\ source and replace \meta{filename} by |\subfix{|\meta{filename}|}|.
 % \item
-%   If nothing of the above helps, ask the nice people on tex.stackexchange\footnote{\url{https://tex.stackexchange.com/}}.
+%   If nothing of the above helps, ask the nice people on tex.stackexchange\footnote{\url{https://tex.stackexchange.com/}} or file an issue in the bug tracker of the Github repository\footnote{\url{https://github.com/gsalzer/subfiles/issues}}.
 % \end{enumerate}
+%
 % 
 % \section{Dependencies}
 %
-% The |subfiles| package uses the |import| package by Donald Arsenau to load subfiles from different directories.
-% |import.sty| is part of the standard \TeX\ distribution.
+% The |import| package by Donald Arsenau is needed to load subfiles from different directories.
+% When the |standalone| package is used, the |subfiles| package requires the |xpatch| package by Enrico Gregorio to patch the command |\includestandalone|. 
+% Both packages are part of the standard \TeX\ distributions.
 %
+%
 % \section{Version history}
 %
-% \begin{description}
+% \begin{enumerate}
 % \item[v1.1:]
-%   Initial version by Federico Garcia. Subsequent versions by Gernot Salzer.
-% \item[v1.2:] \mbox{}
+%   Initial version by Federico Garcia. (Subsequent versions by Gernot Salzer.)
+% \item[v1.2:] 
 %   \begin{itemize}
 %   \item Incompatibility with classes and packages removed that modify the |\document| command, like the class |revtex4|.
 %   \end{itemize}
-% \item[v1.3:] \mbox{}
+% \item[v1.3:]
 %   \begin{itemize}
 %   \item Use of |import| package to handle directory hierarchies.
 %   \item |\ignorespaces| added to avoid spurious spaces.
 %   \item Incompatibility with commands removed that expect |\document| to be equal to |\@onlypreamble| after the preamble. Thanks to Eric Domenjoud for analysing the problem.
 %   \end{itemize}
-% \item[v1.4:] \mbox{}
+% \item[v1.4:]
 %   \begin{itemize}
 %   \item Incompatibility with |memoir| class and |comment| package removed.
 %   \item Bug `|\unskip| cannot be used in vertical mode` fixed.
 %   \end{itemize}
-% \item[v1.5:] \mbox{}
+% \item[v1.5:]
 %   \begin{itemize}
 %   \item Command |\subfileinclude| added.
 %   \item Basic support for |bibtex| related bibliographies in subfiles added.
@@ -362,7 +382,7 @@
 %      Seems to suffice also for sub-bibliographies with the package |chapterbib|.
 %   \item Support for sub-bibliographies with package |bibunits| added.
 %   \end{itemize}
-% \item[v1.6:] \mbox{}
+% \item[v1.6:]
 %   \begin{itemize}
 %   \item Support for sub-bibliographies with package |bibunits| dropped, in favor of |\subfix|.
 %   \item Command |\subfix| added.
@@ -369,10 +389,10 @@
 %   \item Incompatibility with |standalone| class removed.
 %   \item The options of the main class are now also processed when typesetting a subfile; before they were ignored. Thanks to J\'an Kl'uka for analysing the problem.
 %   \end{itemize}
-% \item[v2.0:] \mbox{}
+% \item[v2.0:]
 %   \begin{itemize}
 %   \item Incompatibility with \LaTeX\ Oct.~2020 removed.
-%     Thanks to Ulrike \mbox{Fischer} from the \LaTeX~3 team for the warning in time.
+%     Thanks to Ulrike \mbox{Fischer} from the \LaTeX3 team for the timely warning.
 %   \item By default, text after |\end{document}| as well as the preamble of subfiles, when loaded with |\subfile|, are ignored now.
 %     The old behaviour is available via the new package option |v1|.
 %   \item Command |\ifSubfilesClassLoaded| added and documentation regarding the use of the |\bibliography| command corrected.
@@ -382,14 +402,26 @@
 %   \item Problem with the search path for images resolved.
 %     Thanks to Github user |maxnick| for reporting the issue.
 %   \end{itemize}
-% \end{description}
+% \item[v2.1]
+%   \begin{itemize}
+%   \item Bugfix: In some situations, the hooks of |\begin{document}| and |\end{document}| were triggered when loading a subfile.
+%     This occurred in particular with packages for handling CJK languages.
+%     Thanks to Github user |yuishin-kikuchi| for reporting the issue.
+%   \item Section about cross-referencing added.
+%     Thanks to Github user |ndvanforeest| for the input.
+%   \end{itemize}
+% \end{enumerate}
 %
 %\section{The Implementation}
 %\subsection{The class}
+%
+% \iffalse
+%<*class>
+% \fi
+%
 %    \begin{macrocode}
-%<*class>
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesClass{subfiles}[2020/09/07 v2.0 Multi-file projects (class)]
+\ProvidesClass{subfiles}[2020/10/29 v2.1 Multi-file projects (class)]
 \DeclareOption*{%
   \typeout{Preamble taken from file `\CurrentOption'}%
   \let\preamble at file\CurrentOption
@@ -422,6 +454,7 @@
 % In earlier versions, we used |\subimport| to load the preamble of the main file, which has the unwanted effect of undoing changes to the graphics path.
 % Therefore we use |\input| and initialize |\import at path| and |\input at path| to the path of the main file.
 % We use the internal \LaTeX\ macro |\filename at parse| to obtain this path.
+%
 %    \begin{macrocode}
 \filename at parse{\preamble at file}
 \edef\import at path{\filename at area}
@@ -429,7 +462,7 @@
 \input{\preamble at file}
 %    \end{macrocode}
 %
-% After loading the preamble of the main file, we reset |\import at path{}|.
+% After loading the preamble of the main file, we reset |\import at path|.
 % Since the preamble may have changed the catcode of the |@| sign, we make it (again) a letter. Better safe than sorry.
 %
 %    \begin{macrocode}
@@ -436,106 +469,189 @@
 {\makeatletter
   \gdef\import at path{}
 }
-%</class>      
 %    \end{macrocode}
 %
+% \iffalse
+%</class>      
+% \fi
 %
+%
 % \subsection{The package}
+%
+% \iffalse
+%<*package>
+% \fi
+%
 %    \begin{macrocode}
-%<*package>
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{subfiles}[2020/09/07 v2.0 Multi-file projects (package)]
+\ProvidesPackage{subfiles}[2020/10/29 v2.1 Multi-file projects (package)]
 %    \end{macrocode}
+% \subsubsection*{Auxiliary commands}
+% \begin{flushleft}
+%   |\ifDOCUMENT{|\meta{string}|}{|\meta{then code}|}{|\meta{else code}|}|\\
+%   If \meta{string} equals |document|, then execute \meta{then code}, else \meta{else code}.
+% \end{flushleft}
+%    \begin{macrocode}
+\def\subfiles at DOCUMENT{document}
+\def\ifDOCUMENT#1#2#3{%
+  \def\subfiles at tmp{#1}%
+  \ifx\subfiles at tmp\subfiles at DOCUMENT
+    \def\subfiles at tmp{#2}%
+  \else
+    \def\subfiles at tmp{#3}%
+  \fi
+  \subfiles at tmp
+}
+%    \end{macrocode}
 %
-% The package has one option, |v1|, which affects the way how the text after |\end{document}| and in the preamble of subfiles is handled.
-% With this option, |subfiles| behaves like in version 1.x.
-% To implement the retro behaviour, we need a few definitions.
+% \begin{flushleft}
+%   |\subfiles at renewDocument{begin}{|\meta{begin-document-code}|}|\\ 
+%   |\subfiles at renewDocument{end}{|\meta{end-document-code}|}|\\
+% Redefines |\begin|/|\end| to execute the given code in place of the next |\begin{document}|/|\end{document}|.
+% \end{flushleft}
+%    \begin{macrocode}
+\def\subfiles at renewDocument#1#2{%
+  \expandafter\def\csname#1\endcsname##1{%
+    \ifDOCUMENT{##1}{%
+      \expandafter\let\csname#1\expandafter\endcsname\csname subfiles@#1\endcsname
+      #2%
+    }{%
+      \csname subfiles@#1\endcsname{##1}%
+    }%
+  }%
+}
+%    \end{macrocode}
+% |\subfiles at renewDocument{begin}{<code>}| is actually the same as
+% \begin{verbatim}
+%\def\begin#1{%
+%    \ifDOCUMENT{#1}{%
+%        \let\begin\subfiles at begin
+%        <code>
+%    }{%
+%        \subfiles at begin{#1}%
+%    }%
+%}%\end{verbatim}
+% \begin{flushleft}
+% |\subfiles at newSkipToDocument\begin|\meta{cmd}|{|\meta{continuation}|}|\\
+% |\subfiles at newSkipToDocument\end|\meta{cmd}|{|\meta{continuation}|}|\\
+% Defines \meta{cmd} to skip to the next |\begin{document}| or |\end{document}| and to execute \meta{continuation}.
+% \end{flushleft}
 %
-% First, we define three macros containing the strings |\begin{document}|, |\end{document}| and |document|.
-% We need them later to detect these strings in the input.
 %    \begin{macrocode}
-\def\subfiles at DOCUMENT{document}
-{\escapechar=-1\relax
- \xdef\subfiles at BEGINDOCUMENT{\string\\begin\string\{document\string\}}%
- \xdef\subfiles at ENDDOCUMENT{\string\\end\string\{document\string\}}%
+\def\subfiles at newSkipToDocument#1#2#3{%
+  \long\def#2##1#1##2{\ifDOCUMENT{##2}{#3}{#2}}%
 }
 %    \end{macrocode}
 %
-% The macro |\subfiles at skipDocument| skips everything until it encounters the string |\end{document}|.
-% In earlier versions of the package, this was accomplished by redefining the |document| environment to the |comment| environment of the |verbatim| package.
-% This clashes with the \LaTeX\ format published in October 2020, so we copy and adapt the code from the |verbatim| package (see the definition of |\subfiles at skiplines| below).
+% \subsubsection*{Handling the main document}
 %
+% When the main document is loaded from a subfile, the preamble is read, but the document itself is skipped.
+% The lines after |\end{document}| are treated again as part of the preamble in old versions (1.x), but are ignored in new versions (2.x).
+% 
 %    \begin{macrocode}
-\def\subfiles at skipDocument{%  
-  \let\subfiles at skiplinescont\ignorespaces
-  \let\subfiles at skiplinesend\subfiles at ENDDOCUMENT
-  \subfiles at skiplines
+\def\subfiles at handleMainDocumentVi{%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \subfiles at newSkipToDocument\end\subfiles at skipToEndDocument\ignorespaces
+    \subfiles at skipToEndDocument
+  }%
 }
+\def\subfiles at handleMainDocumentVii{%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \endinput
+    \ignorespaces
+  }%
+}
 %    \end{macrocode}
 %
-% To skip |\documentclass| the old way, we redefine it to do nothing except restoring its original definition.
+% \subsubsection*{Handling subfiles}
 %
+% When a subfile is loaded, the preamble and |\end{document}| have to be ignored.
+% More precisely, in older versions (v1.x), the following happens:
+% \begin{itemize}
+% \item |\documentclass[...]{subfiles}| is ignored.
+% \item The contents of the preamble becomes part of the text.
+% \item |\begin{document}| is ignored.
+% \item |\end{document}| is ignored.
+% \item Any lines after |\end{document}| are also part of the text.
+% \end{itemize}
+%
 %    \begin{macrocode}
-\def\subfiles at skipDocumentclass{%
-  \renewcommand\documentclass[2][]{%
+\def\subfiles at handleSubDocumentVi{%
+  \let\subfiles at documentclass\documentclass
+  \def\documentclass{%
+    \@ifnextchar[\subfiles at documentclass@{\subfiles at documentclass@[]}%
+  }%
+  \def\subfiles at documentclass@[##1]##2{%
     \let\documentclass\subfiles at documentclass
     \ignorespaces
   }%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \subfiles at saveEnd
+    \subfiles at renewDocument{end}\ignorespaces
+    \ignorespaces
+  }%
 }
 %    \end{macrocode}
+% In newer versions (v2.x), the following happens:
+% \begin{itemize}
+% \item The preamble, up to |\begin{document}|, is ignored.
+% \item |\end{document}| as well as any lines after it are ignored.
+% \end{itemize}
 %
-% To ignore the preamble of a subfile, we redefine |\documentclass| to skip everything until the string |\begin{document}| is encountered.
 %    \begin{macrocode}
-\def\subfiles at skipPreamble{%
-  \def\documentclass{%
+\def\subfiles at handleSubDocumentVii{%
+  \let\subfiles at documentclass\documentclass
+  \subfiles at newSkipToDocument\begin\documentclass{%
     \let\documentclass\subfiles at documentclass
-    \def\subfiles at skiplinescont{\begin{document}}%
-    \let\subfiles at skiplinesend\subfiles at BEGINDOCUMENT
-    \subfiles at skiplines
+    \subfiles at saveEnd
+    \subfiles at renewDocument{end}{%
+      \endinput
+      \ignorespaces
+    }%
+    \ignorespaces
   }%
 }
 %    \end{macrocode}
 %
-% Now we set the default behaviour of the subfiles package:
-% When loading the main preamble in a subfile, everything after the preamble is ignored.
-% Moreover, when reading a subfile, its preamble as well as everything after |\end{document}| is ignored.
+% Before redefining |\begin| and |\end|, we remember their original definitions in |\subfiles at begin| and |\subfiles at end|.
+% We don't do this once and for all in the preamble, because there might be other packages also fiddling with these commands.
+% To reset |\begin| to the definition it had at the point where we modified it, we do |\let\subfiles at begin\begin| immediately before redefining it.
+% For |\end|, the situation is different.
+% For nested subfiles, |\end| does not have its original definition but ours.
+% Therefore we save |\end| only if |\subfiles at end| is still undefined (meaning that we are outside of subfiles).
+%
 %    \begin{macrocode}
-\let\subfiles at handleMain\endinput
-\let\subfiles at handleSubpreamble\subfiles at skipPreamble
-\let\subfiles at handleTextAfterSubdocument\endinput
+\def\subfiles at saveEnd{%
+  \ifcsname subfiles at end\endcsname
+  \else
+    \let\subfiles at end\end
+  \fi
+}
 %    \end{macrocode}
 %
-% The option |v1| restores the behaviour of the old |subfiles| package: When loading the main preamble in a subfile, only the contents of the |document| environent is ignored, but not the stuff following it. Moreover, when reading a subfile, only the |\documentclass| command and the lines |\begin{document}| and |\end{document}| are ignored, but the subfile preamble as well as everything after |\end{document}| is retained.
+% \subsubsection*{Processing the package options}
+%
+% The package has currently only one option, |v1|, which affects the way how the text after |\end{document}| and in the preamble of subfiles is handled.
+% We initialize the macros for handling main and sub documents with the behavior of version 2.x.
 %    \begin{macrocode}
+\let\subfiles at handleMainDocument\subfiles at handleMainDocumentVii
+\let\subfiles at handleSubDocument\subfiles at handleSubDocumentVii
+%    \end{macrocode}
+% When option |v1| is present, the macros are set to the behavior of version 1.x.
+%    \begin{macrocode}
 \DeclareOption{v1}{%
-  \let\subfiles at handleMain\subfiles at skipDocument
-  \let\subfiles at handleSubpreamble\subfiles at skipDocumentclass
-  \let\subfiles at handleTextAfterSubdocument\relax
+  \let\subfiles at handleMainDocument\subfiles at handleMainDocumentVi
+  \let\subfiles at handleSubDocument\subfiles at handleSubDocumentVi
 }
 \DeclareOption*{\PackageWarning{subfiles}{Option '\CurrentOption' ignored}}
 \ProcessOptions\relax
 %    \end{macrocode}
-% 
-% To skip everything until a specific string is read, we adapt code from the |verbatim| package.
-% The skipping of lines is controlled by two macros that have to be set before calling |\subfiles at skiplines|.
-% |\subfiles at skiplinesend| is the string that marks the end of the skipped area; it has to appear on a line of its own, as the only content of this line.
-% |\subfiles at skiplinescont| contains the code to be executed after skipping has ended.
-%  
-%    \begin{macrocode}
-\def\subfiles at skiplines{%
-  \begingroup
-    \let\do\@makeother\dospecials
-    \@makeother\^^L%
-    \endlinechar`\^^M\relax \catcode`\^^M=12\relax \subfiles at skipline}
-{\catcode`\^^M=12 \endlinechar=-1 %
- \gdef\subfiles at skipline#1^^M{\def\subfiles at tmp{#1}%
-      \ifx\subfiles at tmp\subfiles at skiplinesend
-          \def\subfiles at tmp{\endgroup\subfiles at skiplinescont}%
-      \else\let\subfiles at tmp\subfiles at skipline
-      \fi \subfiles at tmp}
-}
-%    \end{macrocode}
 %
+% \subsubsection*{Loading subfiles}
+%
 % To handle subfiles in separate directories, we use the |import| package.
 % If it has already been loaded, e.g.\ by the |subfiles| class, this line does nothing.
 %    \begin{macrocode}
@@ -542,7 +658,7 @@
 \RequirePackage{import}
 %    \end{macrocode}
 % 
-% The |\subimport| command requires path and filename as separate arguments, so we have to split file locations into these two components.
+% The |\subimport| command requires path and filename as separate arguments, so we have to split qualified filenames into these two components.
 % The internal \LaTeX\ command |\filename at parse| almost fits the bill, except that it additionally splits the filename into basename and extension.
 % Unfortunately, concatenating basename and extension to recover the filename is not clean:
 % Under Unix/Linux, the filenames |base| and |base.| denote different entities, but after |\filename at parse| both have the same basename and an empty extension.
@@ -579,30 +695,13 @@
 }
 %    \end{macrocode}
 %
-% The main functionality of the two commands is implemented in |\subfiles at subfile|.
-% It redefines |\documentclass| and the |document| environment to do nothing but reverting these command to their original meaning and avoiding spurious spaces.
-% Reverting |\documentclass| and |\document| to their original definition is important for being compatible with classes like |standalone| or packages like |bibentry|, which rely on this definition.
+% The main functionality is implemented in |\subfiles at subfile|.
+% It sets up the handling of the sub-preamble, splits the filename and loads the subfile.
 %
 %    \begin{macrocode}
-\newcommand\subfiles at subfile[1]{%
+\def\subfiles at subfile#1{%
   \begingroup
-  \let\subfiles at documentclass\documentclass
-  \let\subfiles at document\document
-  \let\subfiles at enddocument\enddocument
-  \subfiles at handleSubpreamble
-  \renewenvironment{document}{%
-    \let\document\subfiles at document
-    \ignorespaces
-  }{%
-    \let\enddocument\subfiles at enddocument
-    \@ignoretrue
-    \subfiles at handleTextAfterSubdocument
-  }%
-%    \end{macrocode}
-%
-% Now we split the file name into path and base name and load the file.
-%
-%    \begin{macrocode}
+  \subfiles at handleSubDocument
   \subfiles at split{#1}%
   \subfiles at loadfile{\filename at area}{\filename at base}%
   \endgroup
@@ -609,6 +708,8 @@
 }
 %    \end{macrocode}
 %
+% \subsubsection*{Fixing incompatibilities}
+%
 % \DescribeMacro{\subfix}
 % If some package provides a command that takes a filename as argument, then it has to be prefixed with the current |\import at path|.
 % This is what the |\subfix| command tries to do.
@@ -643,13 +744,13 @@
 %
 %    \begin{macrocode}
 \let\subfiles at bibliography\bibliography
-\renewcommand\bibliography[1]{%
+\def\bibliography#1{%
   \subfiles at fixfilelist{#1}%
   \expandafter\subfiles at bibliography\expandafter{\subfiles at list}%
 }
 \@ifpackageloaded{graphics}{%
   \let\subfiles at graphicspath\graphicspath
-  \renewcommand\graphicspath[1]{%
+  \def\graphicspath#1{%
     \subfiles at fixpathlist{#1}%
     \edef\subfiles at list{{\subfix{}}\subfiles at list}%
     \expandafter\subfiles at graphicspath\expandafter{\subfiles at list}%
@@ -657,6 +758,25 @@
 }{}
 %    \end{macrocode}
 %
+% \DescribeMacro{\includestandalone}
+% The command |\includestandalone| handles subfiles in its own way.
+% Therefore we modify it to use the original definition of |\end|.
+%
+%    \begin{macrocode}
+\@ifpackageloaded{standalone}{
+  \RequirePackage{xpatch}
+  \xpretocmd\includestandalone{%
+    \let\subfiles at end@\end
+    \ifcsname subfiles at end\endcsname
+      \let\end\subfiles at end
+    \fi
+  }{}{}
+  \xapptocmd\includestandalone{\let\end\subfiles at end@}{}{}
+}{}
+%    \end{macrocode}
+%
+% \subsubsection*{Do we typeset the main file or a subfile?}
+%
 % \DescribeMacro{\ifSubfilesClassLoaded}
 % To add code or text conditionally, depending on whether the main document or a subfile is typeset, we provide the command |\ifSubfilesClassLoaded|.
 %
@@ -670,26 +790,17 @@
 }
 %    \end{macrocode}
 %
+%
+% \subsubsection*{The end is near}
+%
 % The |subfiles| package is loaded near the end of the main preamble.
-% If it is loaded from a subfile, i.e., if |subfiles.cls| has been loaded, then we have to prepare for skipping the main document.
-% We do this be redefining the |\begin| command.
-% Normally, the first |\begin| after the |subfiles| package starts the main document.
-% To allow for the case that some other environment occurs before (does it really happen?) we test whether we are dealing with |\begin{document}|.
-% If not, we execute the original definition of |\begin|; otherwise we skip the main document as specified by |\subfiles at handleMain|.
-%
+% If it is loaded from a subfile, i.e., if |subfiles.cls| has been loaded, then we prepare for skipping the main document.
 %    \begin{macrocode}
 \ifSubfilesClassLoaded{%
-  \let\subfiles at begin\begin
-  \def\begin#1{%
-    \def\subfiles at tmp{#1}%
-    \ifx\subfiles at tmp\subfiles at DOCUMENT
-      \let\begin\subfiles at begin
-      \let\subfiles at tmp\subfiles at handleMain
-    \else 
-      \def\subfiles at tmp{\subfiles at begin{#1}}%
-    \fi
-    \subfiles at tmp
-  }%
+  \subfiles at handleMainDocument
 }{}
+%    \end{macrocode}
+%
+% \iffalse
 %</package>
-%    \end{macrocode}
+% \fi

Modified: trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls
===================================================================
--- trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls	2020-11-08 21:43:02 UTC (rev 56887)
+++ trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls	2020-11-08 21:43:17 UTC (rev 56888)
@@ -26,7 +26,7 @@
 %% and the derived files subfiles.sty and subfiles.pdf
 %% 
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesClass{subfiles}[2020/09/07 v2.0 Multi-file projects (class)]
+\ProvidesClass{subfiles}[2020/10/29 v2.1 Multi-file projects (class)]
 \DeclareOption*{%
   \typeout{Preamble taken from file `\CurrentOption'}%
   \let\preamble at file\CurrentOption

Modified: trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty	2020-11-08 21:43:02 UTC (rev 56887)
+++ trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty	2020-11-08 21:43:17 UTC (rev 56888)
@@ -26,53 +26,86 @@
 %% and the derived files subfiles.sty and subfiles.pdf
 %% 
 \NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{subfiles}[2020/09/07 v2.0 Multi-file projects (package)]
+\ProvidesPackage{subfiles}[2020/10/29 v2.1 Multi-file projects (package)]
 \def\subfiles at DOCUMENT{document}
-{\escapechar=-1\relax
- \xdef\subfiles at BEGINDOCUMENT{\string\\begin\string\{document\string\}}%
- \xdef\subfiles at ENDDOCUMENT{\string\\end\string\{document\string\}}%
+\def\ifDOCUMENT#1#2#3{%
+  \def\subfiles at tmp{#1}%
+  \ifx\subfiles at tmp\subfiles at DOCUMENT
+    \def\subfiles at tmp{#2}%
+  \else
+    \def\subfiles at tmp{#3}%
+  \fi
+  \subfiles at tmp
 }
-\def\subfiles at skipDocument{%
-  \let\subfiles at skiplinescont\ignorespaces
-  \let\subfiles at skiplinesend\subfiles at ENDDOCUMENT
-  \subfiles at skiplines
+\def\subfiles at renewDocument#1#2{%
+  \expandafter\def\csname#1\endcsname##1{%
+    \ifDOCUMENT{##1}{%
+      \expandafter\let\csname#1\expandafter\endcsname\csname subfiles@#1\endcsname
+      #2%
+    }{%
+      \csname subfiles@#1\endcsname{##1}%
+    }%
+  }%
 }
-\def\subfiles at skipDocumentclass{%
-  \renewcommand\documentclass[2][]{%
-    \let\documentclass\subfiles at documentclass
+\def\subfiles at newSkipToDocument#1#2#3{%
+  \long\def#2##1#1##2{\ifDOCUMENT{##2}{#3}{#2}}%
+}
+\def\subfiles at handleMainDocumentVi{%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \subfiles at newSkipToDocument\end\subfiles at skipToEndDocument\ignorespaces
+    \subfiles at skipToEndDocument
+  }%
+}
+\def\subfiles at handleMainDocumentVii{%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \endinput
     \ignorespaces
   }%
 }
-\def\subfiles at skipPreamble{%
+\def\subfiles at handleSubDocumentVi{%
+  \let\subfiles at documentclass\documentclass
   \def\documentclass{%
+    \@ifnextchar[\subfiles at documentclass@{\subfiles at documentclass@[]}%
+  }%
+  \def\subfiles at documentclass@[##1]##2{%
     \let\documentclass\subfiles at documentclass
-    \def\subfiles at skiplinescont{\begin{document}}%
-    \let\subfiles at skiplinesend\subfiles at BEGINDOCUMENT
-    \subfiles at skiplines
+    \ignorespaces
   }%
+  \let\subfiles at begin\begin
+  \subfiles at renewDocument{begin}{%
+    \subfiles at saveEnd
+    \subfiles at renewDocument{end}\ignorespaces
+    \ignorespaces
+  }%
 }
-\let\subfiles at handleMain\endinput
-\let\subfiles at handleSubpreamble\subfiles at skipPreamble
-\let\subfiles at handleTextAfterSubdocument\endinput
+\def\subfiles at handleSubDocumentVii{%
+  \let\subfiles at documentclass\documentclass
+  \subfiles at newSkipToDocument\begin\documentclass{%
+    \let\documentclass\subfiles at documentclass
+    \subfiles at saveEnd
+    \subfiles at renewDocument{end}{%
+      \endinput
+      \ignorespaces
+    }%
+    \ignorespaces
+  }%
+}
+\def\subfiles at saveEnd{%
+  \ifcsname subfiles at end\endcsname
+  \else
+    \let\subfiles at end\end
+  \fi
+}
+\let\subfiles at handleMainDocument\subfiles at handleMainDocumentVii
+\let\subfiles at handleSubDocument\subfiles at handleSubDocumentVii
 \DeclareOption{v1}{%
-  \let\subfiles at handleMain\subfiles at skipDocument
-  \let\subfiles at handleSubpreamble\subfiles at skipDocumentclass
-  \let\subfiles at handleTextAfterSubdocument\relax
+  \let\subfiles at handleMainDocument\subfiles at handleMainDocumentVi
+  \let\subfiles at handleSubDocument\subfiles at handleSubDocumentVi
 }
 \DeclareOption*{\PackageWarning{subfiles}{Option '\CurrentOption' ignored}}
 \ProcessOptions\relax
-\def\subfiles at skiplines{%
-  \begingroup
-    \let\do\@makeother\dospecials
-    \@makeother\^^L%
-    \endlinechar`\^^M\relax \catcode`\^^M=12\relax \subfiles at skipline}
-{\catcode`\^^M=12 \endlinechar=-1 %
- \gdef\subfiles at skipline#1^^M{\def\subfiles at tmp{#1}%
-      \ifx\subfiles at tmp\subfiles at skiplinesend
-          \def\subfiles at tmp{\endgroup\subfiles at skiplinescont}%
-      \else\let\subfiles at tmp\subfiles at skipline
-      \fi \subfiles at tmp}
-}
 \RequirePackage{import}
 \def\subfiles at split#1{%
   \let\subfiles at filename@simple\filename at simple
@@ -88,20 +121,9 @@
   \let\subfiles at loadfile\subincludefrom
   \subfiles at subfile
 }
-\newcommand\subfiles at subfile[1]{%
+\def\subfiles at subfile#1{%
   \begingroup
-  \let\subfiles at documentclass\documentclass
-  \let\subfiles at document\document
-  \let\subfiles at enddocument\enddocument
-  \subfiles at handleSubpreamble
-  \renewenvironment{document}{%
-    \let\document\subfiles at document
-    \ignorespaces
-  }{%
-    \let\enddocument\subfiles at enddocument
-    \@ignoretrue
-    \subfiles at handleTextAfterSubdocument
-  }%
+  \subfiles at handleSubDocument
   \subfiles at split{#1}%
   \subfiles at loadfile{\filename at area}{\filename at base}%
   \endgroup
@@ -122,18 +144,28 @@
   }%
 }
 \let\subfiles at bibliography\bibliography
-\renewcommand\bibliography[1]{%
+\def\bibliography#1{%
   \subfiles at fixfilelist{#1}%
   \expandafter\subfiles at bibliography\expandafter{\subfiles at list}%
 }
 \@ifpackageloaded{graphics}{%
   \let\subfiles at graphicspath\graphicspath
-  \renewcommand\graphicspath[1]{%
+  \def\graphicspath#1{%
     \subfiles at fixpathlist{#1}%
     \edef\subfiles at list{{\subfix{}}\subfiles at list}%
     \expandafter\subfiles at graphicspath\expandafter{\subfiles at list}%
   }%
 }{}
+\@ifpackageloaded{standalone}{
+  \RequirePackage{xpatch}
+  \xpretocmd\includestandalone{%
+    \let\subfiles at end@\end
+    \ifcsname subfiles at end\endcsname
+      \let\end\subfiles at end
+    \fi
+  }{}{}
+  \xapptocmd\includestandalone{\let\end\subfiles at end@}{}{}
+}{}
 \newcommand\ifSubfilesClassLoaded{%
   \expandafter\ifx\csname ver at subfiles.cls\endcsname\relax
     \expandafter\@secondoftwo
@@ -142,17 +174,7 @@
   \fi
 }
 \ifSubfilesClassLoaded{%
-  \let\subfiles at begin\begin
-  \def\begin#1{%
-    \def\subfiles at tmp{#1}%
-    \ifx\subfiles at tmp\subfiles at DOCUMENT
-      \let\begin\subfiles at begin
-      \let\subfiles at tmp\subfiles at handleMain
-    \else
-      \def\subfiles at tmp{\subfiles at begin{#1}}%
-    \fi
-    \subfiles at tmp
-  }%
+  \subfiles at handleMainDocument
 }{}
 \endinput
 %%



More information about the tex-live-commits mailing list.