summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/subfiles
diff options
context:
space:
mode:
authorKarl Berry <karl@freefriends.org>2019-10-24 21:48:25 +0000
committerKarl Berry <karl@freefriends.org>2019-10-24 21:48:25 +0000
commitdbe0de34966f1a14cf3c9221ce6ec20ccd4febb1 (patch)
treedcec5e19e5442892a674e78ec01ac2e2af4075b6 /Master/texmf-dist/source/latex/subfiles
parented9573425b154ad38d9236a8eaccfe237bb415d8 (diff)
subfiles (24oct19)
git-svn-id: svn://tug.org/texlive/trunk@52526 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/source/latex/subfiles')
-rw-r--r--Master/texmf-dist/source/latex/subfiles/subfiles.dtx384
-rw-r--r--Master/texmf-dist/source/latex/subfiles/subfiles.ins4
2 files changed, 312 insertions, 76 deletions
diff --git a/Master/texmf-dist/source/latex/subfiles/subfiles.dtx b/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
index 1033a03e98c..dacece04617 100644
--- a/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
+++ b/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
@@ -2,7 +2,7 @@
%
% subfiles - class and package for multi-file projects in LaTeX
% Copyright 2002, 2012 Federico Garcia (feg8@pitt.edu, fedegarcia@hotmail.com)
-% Copyright 2018 Gernot Salzer (salzer@logic.at)
+% Copyright 2018, 2019 Gernot Salzer (salzer@logic.at)
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
@@ -23,13 +23,13 @@
%
%<*driver>
% \fi
-\ProvidesFile{subfiles.dtx}[2018/07/31 v1.2 Multi-file projects]
+\ProvidesFile{subfiles.dtx}[2019/09/28 v1.3 Multi-file projects]
% \iffalse
\documentclass{ltxdoc}
\GetFileInfo{subfiles.dtx}
-\title{A Document Class and a Package for handling multi-files projects}
+\title{A Document Class and a Package\\for Handling Multi-File Projects}
\date{\filedate{}}
- \author{Federico Garcia\\{\small (v1.2 update by Gernot Salzer)}}
+ \author{Federico Garcia, Gernot Salzer}
\begin{document}
\maketitle
@@ -38,68 +38,205 @@
%</driver>
% \fi
% \begin{abstract}
-% With the |subfiles| set, the typesetting of a multi-file project consisting of one main file and one or more subsidiary files (subfiles) is more comfortable, since the user can \LaTeX\ either the main file, which will |\input| the subfiles normally, or the subfiles by themselves, which take the preamble of the main file and become self-sufficient \LaTeX\ documents.
+% The |subfiles| package allows authors to split a document into one main file and one and more subsidiary files (subfiles) akin to the |\input| command, with the added benefit of making the subfiles compilable by themselves.
+% This is achieved by reusing the preamble of the main file also for the subfiles.
% \end{abstract}
% \section{Introduction}
-% \LaTeX\ commands |\include| and |\input| allow for the creation of different input files to be typeset jointly into a single output. The advantages of this are evident in the creation of large documents with many chapters, but there are also other circumstances in which the author might want to use this feature. I have used it particularly for long-coded examples, tables, figures, etc\@.\footnote{In my case most times it has been musical examples, whose code in MusiX\TeX\ is long, intrincate, and barely readable.}, which require a considerable amount of trial-and-error.
+% The \LaTeX\ commands |\include| and |\input| allow the user to split the \TeX\ source of a document into several input files.
+% This is useful when creating documents with many chapters, but also for handling large tables, figures, and code samples, which require a considerable amount of trial-and-errors.%
+% \footnote{Frederico had to typset numerous musical examples, whose code in MusiX\TeX\ is long, intrincate, and barely readable.}
%
-% In this process the rest of the document is of little use, and it can even disturb.\footnote{For example, the error messages indicate not only a wrong line number, but even the wrong file.} Frequently, one ends up wanting to work \emph{only} on the new file, which means following three steps:
+% In this process the rest of the document is of little use, and can even interfere.
+% For example, error messages may indicate not only the wrong line number, but may point to the wrong file.
+% Frequently, one ends up wanting to work only on the new file:
%
% \begin{itemize}
-% \item Create a new file, and copy-paste in it the preamble of the main file;
-% \item Work in the example, typeset it \emph{alone} as many times as necessary;
-% \item When the result is satisfactory, delete the preamble from the new file (and the |\end{document}|!), and |\include| or (more frequently) |\input| it from the main file.
+% \item Create a new file, and copy-paste the preamble of the main file into it.
+% \item Work on this file, typeset it \emph{alone} as many times as necessary.
+% \item Finally, when the result is satisfactory, delete the preamble from the file (alongside with |\end{document}|!), and |\include| or |\input| it from the main file.
% \end{itemize}
%
-% It is desirable to reduce these three steps to the only interesting one, the middle one. This would mean that each new, subordinated file (henceforth, `subfile') should be \emph{both} part of a project and a self-sufficient \LaTeX\ document, depending on whether it is \LaTeX ed or |\included|/|\input|. This is what the set of class and package under the name |subfiles| is intended for.
-%
-% The main idea behind it is the redefinition of |\documentclass| and the |document| environment; while these two features of \LaTeX\ are important to keep unchanged, |subfiles| changes them, as far as I know, harmlessly, and I have took care of undoing the changes when finished. This is the first version of |subfiles|, and although I have tried it a few times, it is still susceptible to conflicts with other packages and/or classes. (In fact, a conflict with the |revtex4| class later gave rise to version 1.2 of the package.)
-%
-% \section{Usage}
-%
-% \subsection{Setting up}
-%
-% The files involved have the following basic structures:
+% It is desirable to reduce these three steps to the interesting, middle one.
+% Each new, subordinate file (henceforth `subfile') should behave both as a self-sufficient \LaTeX\ document and as part of the whole project, depending on whether it is \LaTeX ed individually or |\included|/|\input| from the main document.
+% This is what the class |subfiles.cls| and the package |subfiles.sty| are intended for.
%
+% \section{Basic usage}
+%
+% The main file, i.e., the file with the preamble to be shared with the subfiles, has to load the package |subfiles| \emph{at the very end of the preamble}:
+% \begin{center}
+% \begin{tabular}{l}
+% |\usepackage{subfiles}|\\
+% |\begin{document}|
+% \end{tabular}
+% \end{center}
+% Subordinate files (subfiles) are loaded from the main file or from other subfiles with the command
+% \begin{center}
+% |\subfile{|\meta{subfile\_name}|}|
+% \end{center}
+% The subfiles have to start with the line
+% \begin{center}
+% |\documentclass[|\meta{main\_file\_name}|]{subfiles}|
+% \end{center}
+% which loads the class |subfiles|.
+% Its only `option', which is actually mandatory, gives the name of the main file.
+% This name follows \TeX\ conventions: |.tex| is the default extension, the path has to be provided if the main file is in a different directory, and directories in the path have to be separated by |/| (not |\|).
+% Thus, we have the following structure:
% \begin{center}\small
-% \begin{tabular}{ll}
-% \multicolumn{1}{c}{\emph{MAIN FILE}} & \multicolumn{1}{c}{\emph{SUBFILE}}\\
-% \qquad\meta{some preamble} & |\documentclass[|\meta{main\_file\_name}|]{subfiles}|\\
-% |\usepackage{subfiles}| & |\begin{document}|\\
-% |\begin{document}| & \qquad\meta{text, graphics, etc.}\\
-% \qquad\meta{text}&|\end{document}|\\
-% |\subfile{|\meta{subfile\_name}|}|\\
-% \qquad\meta{more text}\\
-% |\end{document}|
-% \end{tabular}
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{main file} \\
+% \hline
+% |\documentclass[...]{...}| \\
+% \meta{shared preamble} \\
+% |\usepackage{subfiles}| \\
+% |\begin{document}| \\
+% \dots \\
+% |\subfile{|\meta{subfile\_name}|}| \\
+% \dots \\
+% |\end{document}| \\
+% \hline
+% \end{tabular}
+% \hfill
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{subfile} \\
+% \hline
+% |\documentclass[|\meta{main\_file\_name}|]{subfiles}| \\
+% |\begin{document}| \\
+% \dots \\
+% |\end{document}| \\
+% \hline
+% \end{tabular}
% \end{center}
-%
-% The |subfiles| package is to be loaded in the main file of a \LaTeX\ project at the end of the preamble, and the |subfiles| class is to be loaded by each subordinate file. Note that the |subfiles| class handles only \emph{one} `option' (whose presence is actually mandatory), the name of the main file. The name should be given according to \TeX\ conventions: |.tex| is the default extension; the path has to be indicated (|/|, not |\|) if the main file is in a different directory from the subfile; spaces are gobbled (at least under Windows).
-%
-% \subsection{Results}
-% This done, \LaTeX ing either the main or the subordinate file produces the following results:
+% Now there are two possibilities.
% \begin{itemize}
-% \item If the subfile is typeset by itself, it takes as preamble the one of the main file (including its |\documentclass|). The rest is typeset normally.
-% \item If the subordinated file was |\subfile|'d, it ignores everything before and including |\begin{document}|, and then ignores |\end{document}| too. (The body of the file, nothing else, is effectively |\input|.)
+% \item If \LaTeX\ is run on the subfile, the line |\documentclass[..]{subfiles}| is replaced by the preamble of the main file (including its |\documentclass| command).
+% The rest of the subfile is processed normally.
+% \item If \LaTeX\ is run on the main file, the subfile is loaded like with an |\input| command, except that the three lines |\documentclass[..]{subfiles}|, |\begin{document}|, and |\end{document}| are ignored.
% \end{itemize}
+%
+% \section{Advanced usage}
+%
+% \subsection{Hierarchy of directories}
+%
+% Sometimes it is desirable to put a subfile together with its images and further files into its own directory.
+% The difficulty now is that these additional files have to be addressed by different pathes depending on whether the main files or the subfile is typeset.
+% As of version 1.3, the |subfiles| package handles this problem by using the |import| package.
+%
+% As an example, consider the following hierarchy of files:
+% \begin{center}\ttfamily
+% \begin{tabular}{l}
+% main.tex\\
+% mypreamble.tex\\
+% dir1/subfile1.tex\\
+% dir1/image1.jpg\\
+% dir1/text1.tex\\
+% dir1/dir2/subfile2.tex\\
+% dir1/dir2/image2.jpg\\
+% dir1/dir2/text2.tex
+% \end{tabular}
+% \end{center}
+% where |main|, |subfile1|, and |subfile2| have the following contents:
+% \begin{center}
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{|main.tex|} \\
+% \hline
+% |\documentclass{article}| \\
+% |\input{mypreamble}| \\
+% |\usepackage{graphicx}| \\
+% |\usepackage{subfiles}| \\
+% |\begin{document}| \\
+% |\subfile{dir1/subfile1}|\\
+% |\end{document}|\\
+% \hline
+% \end{tabular}%
+% \hfill
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{|subfile1.tex|} \\
+% \hline
+% |\documentclass[../main]{subfiles}| \\
+% |\begin{document}| \\
+% |\input{text1}|\\
+% |\includegraphics{image1.jpg}|\\
+% |\subfile{dir2/subfile2}| \\
+% |\end{document}| \\
+% \hline
+% \end{tabular}\\[2ex]
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{|subfile2.tex|} \\
+% \hline
+% |\documentclass[../../main]{subfiles}| \\
+% |\begin{document}| \\
+% |\input{text2}|\\
+% |\includegraphics{image2.jpg}|\\
+% |\end{document}| \\
+% \hline
+% \end{tabular}
+% \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.
%
-% The |\subfile| command is more like |\input| than |\include| in the sense that it does not start a new page. It allows nesting, but there is no exclusion mechanism analogous to |\includeonly|.
+% \subsection{Additional definitions for subfiles}
%
-% \subsection{Further details and warnings}
+% Usually all definitions and packages required by the subfiles should go into the preamble of the main file.
+% There are some places, though, where one might consider adding definitions for the subfiles.
%
-% To be precise, a subfile typeset by itself does not exactly take the preamble of the main file, but \emph{everything outside} \verb|\begin{document}| and \verb|\end{document}|. This has two consequences: $a)$~the user can add some commands to be read only when the subfiles are typeset by themselves---which in any case are processed as part of the preamble; but also $b)$~the user has to be careful even \emph{after} |\end{document}| (in the main file), for any syntax error there will ruin the \LaTeX ing of the subfile(s).
+% \paragraph{Code after the end of the main document} is added to the preamble of the subfiles, but is ignored when typesetting the main file.
+% This happens because a subfile typeset by itself does not really take the preamble of the main file, but \emph{everything outside} of \verb|\begin{document}| and \verb|\end{document}|.
+% This has two consequences: \emph{a)}~the user can add some commands to be processed as part of the preamble only when the subfiles are typeset by themselves; but also \emph{b)}~the user has to be careful even \emph{after} |\end{document}| in the main file, for any syntax error there will ruin the \LaTeX ing of the subfile(s).
%
-% The preamble of the main file can |\input| (not |\include| nor |\subfile|) other files (e.g.\ files with definitions and shorthand-commands), and the subfiles will too. But it has to be kept in mind that each subfile is |\input| within a group, so definitions made within them might not work outside. A good practice when using |subfiles| (and also when not using it) is to make any definitions in the preamble of the main file, avoiding confusion and allowing to find them easily.
+% \paragraph{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.
+% This means that the preamble of a subfile can only contain stuff that is acceptable for both, the preamble and the text area.
+% One should also keep in mind that each subfile is |\input| within a group, so definitions made within may not work outside.
+% A good practice when using |subfiles| (and also when not using it) is to make any definitions in the preamble of the main file, avoiding confusion and allowing the reader to find them easily.
+%
+% \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.
+% In this respect |\subfile| behaves like |\input|.
+% Any space or newline before and after the |\subfile| command will appear in the typeset document, as will any white space between the last character of the subfile and |\end{document}|.
+% Therefore, to load the contents of a subfile without intervening spaces, you have either to add comment signs:
+% \begin{center}
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{|main.tex|}\\
+% \hline
+% \dots\\
+% |text before%|\\
+% |\subfile{sub.tex}%|\\
+% |text after|\\
+% \hline
+% \end{tabular}
+% \qquad
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{|sub.tex|}\\
+% \hline
+% |\documentclass[main.tex]{subfiles}|\\
+% |\begin{document}|\\
+% |contents of subfile%|\\
+% |\end{document}|\\
+% \hline
+% \end{tabular}
+% \end{center}
+% or to put everything on the same line:
+% \begin{center}
+% |text before\subfile{sub.tex}text after|\\
+% |contents of subfile\end{document}|
+% \end{center}
%
-% In principle, nesting files with |\subfile| should work and has worked in my tries, as far as every subfile loads the main file as its option to the |subfiles| class. However, who knows, the behavior can be unpredictable in weird situations. In any case, |subfiles| does \emph{not} disable |\include| nor |\input|, which remain available for free use.
+% \section{Dependencies}
%
-% The |subfiles| package requires the |verbatim| package (whose |comment| environment is used to ignore the different parts of different files); this should not be a problem since it makes part of the standard distribution of \LaTeX $2_\varepsilon$.
+% The |subfiles| package requires the |verbatim| package, whose |comment| environment is used to ignore the text area of the main file when typesetting subfiles separately.
+% Moreover, the |import| package is needed to load subfiles and their auxiliary files from different directories.
+% Both packages are part of the standard \TeX\ distributions.
%
-% \subsection{Version history}
+% \section{Version history}
%
% \begin{description}
-% \item[v1.1:] Start of version history. Written by Federico Garcia.
-% \item[v1.2:] The |subfiles| package is now compatible with classes and packages that modify the |\document| command, like the class |revtex4|. Modification by Gernot Salzer.
+% \item[v1.1 (FG):]
+% Start of version history.
+% \item[v1.2 (GS):]
+% The |subfiles| package becomes compatible with classes and packages that modify the |\document| command, like the class |revtex4|.
+% \item[v1.3 (GS):]
+% Use of |import| package to handle directory hierarchies.
+% |\ignorespaces| added to avoid spurious spaces.
+% Incompatibility with commands removed that expect |\document| to be equal to |\@onlypreamble| after the preamble (thanks to Eric Domenjoud for analysing the problem).
% \end{description}
%
%\section{The Implementation}
@@ -107,70 +244,169 @@
% \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
-\ProvidesClass{subfiles}[2018/07/31 Federico Garcia, Gernot Salzer]
+\ProvidesClass{subfiles}[2019/09/28 v1.3 Federico Garcia, Gernot Salzer]
\DeclareOption*{\typeout{Preamble taken from file `\CurrentOption'}%
\let\preamble@file\CurrentOption}
-\ProcessOptions
+\ProcessOptions
% \end{macrocode}
%
-% The first thing to do is to save the regular \LaTeX\ definition of |\documentclass|:
+% We start by saving the regular \LaTeX\ definition of |\documentclass|:
+%
% \begin{macrocode}
-\let\old@documentclass@subfiles\documentclass
+\let\subfiles@documentclass\documentclass
% \end{macrocode}
%
-% Now |\documentclass|, after having already loaded |subfiles.cls|, is set equal to |\LoadClass| such that the class and the options of the main file will be loaded identically.
+% Now |\documentclass| is set equal to |\LoadClass| such that the class and the options of the main file will be loaded as usual.
+%
% \begin{macrocode}
\let\documentclass\LoadClass\relax
% \end{macrocode}
%
-% Now it is possible to |\input| the main file. The main file loads the package |subfiles| as part of the preamble, which saves the contents of |\document| and |\enddocument| as |\old@document@subfiles| and |\old@enddocument@subfiles|, respectively. After having loaded the main file, we can restore the original values of |\document|, |\enddocument| and |\documentclass|. The backup commands are |\undefined| to save memory. That's it.
+% To handle subfiles in separate directories, we load the |import| package.
+%
+% \begin{macrocode}
+\RequirePackage{import}
+% \end{macrocode}
+%
+% The |\subimport| command requires the path and the basename of the file to be loaded in separate arguments.
+% Therefore we have to decompose file names into these two components.
+%
+% \begin{macrocode}
+\def\subfiles@split#1{%
+ \edef\subfiles@filename{#1}%
+ \def\subfiles@dir{}%
+ \def\subfiles@base{}%
+ \def\subfiles@sep{}%
+ \expandafter\subfiles@split@\subfiles@filename/\@nil/%
+}
+\def\subfiles@split@#1/{%
+ \def\tmp{#1}%
+ \ifx\tmp\@nnil
+ \let\subfiles@next\relax
+ \else
+ \edef\subfiles@dir{\subfiles@dir\subfiles@base\subfiles@sep}%
+ \def\subfiles@base{#1}%
+ \def\subfiles@sep{/}%
+ \let\subfiles@next\subfiles@split@
+ \fi
+ \subfiles@next
+}
+% \end{macrocode}
+%
+% After executing e.g.\ |\subfiles@split{../dir1/dir2/file.tex}|, the commands |\subfiles@dir| and |\subfiles@base| expand to |../dir1/dir2/| and |file.tex|, respectively.
+%
+% Now we split the name of the main file that has been provided as optional argument of the document class, and |\subimport| the main file.
+%
+% \begin{macrocode}
+\subfiles@split{\preamble@file}
+\subimport{\subfiles@dir}{\subfiles@base}
+% \end{macrocode}
+%
+% The main file loads the package |subfiles| as part of the preamble, which saves the contents of |\document| and |\enddocument| as |\subfiles@document| and |\subfiles@enddocument|, respectively.
+% Then we restore the original values of |\document|, |\enddocument|, and |\documentclass|. The backup commands are |\undefined| to save memory. That's it.
+%
% \begin{macrocode}
-\input{\preamble@file}
{\catcode`\@=11
-\global\let\document\old@document@subfiles
-\global\let\enddocument\old@enddocument@subfiles
-\global\let\documentclass\old@documentclass@subfiles
-\global\let\old@document@subfiles\undefined
-\global\let\old@enddocument@subfiles\undefined
-\global\let\old@documentclass@subfiles\undefined}
-%</class>
+\global\let\document\subfiles@document
+\global\let\enddocument\subfiles@enddocument
+\global\let\documentclass\subfiles@documentclass
+\global\let\subfiles@document\undefined
+\global\let\subfiles@enddocument\undefined
+\global\let\subfiles@documentclass\undefined
+}
+%</class>
% \end{macrocode}
%
-% It may not be obvious why |@| has to be catcoded to a letter, since we are in a style file anyway. However, the |\preamble@file| occasionally contains |\usepackage| commands that make |@| a non-letter. This is why the part after loading the main preamble needs a |\catcode| command, grouping, and |\global|'s.
+% It may not be obvious why |@| has to be catcoded to a letter, since we are in a style file anyway.
+% However, the |\preamble@file| occasionally contains |\usepackage| commands that make |@| a non-letter.
+% This is why the part after loading the main preamble needs a |\catcode| command, grouping, and |\global|'s.
+%
%
% \subsection{The package}
-% Any option will be ignored.
+%
+% Any option will be ignored.
+%
% \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{subfiles}[2018/07/31 Federico Garcia, Gernot Salzer]
+\ProvidesPackage{subfiles}[2019/09/28 v1.3 Federico Garcia, Gernot Salzer]
\DeclareOption*{\PackageWarning{\CurrentOption ignored}}
\ProcessOptions
\RequirePackage{verbatim}
% \end{macrocode}
%
-% \DescribeMacro{\skip@preamble}
-% The core of the package. It works by redefining the |document| environment, thus making |\begin{document}| and |\end{document}| of the subfile transparent to the inclusion. The redefinition of |\documentclass| is analogous, just having a required and an optional arguments which mean nothing to |\subfile|.
+% To handle subfiles in separate directories, we load the |import| package.
+%
% \begin{macrocode}
-\newcommand{\skip@preamble}{%
- \let\document\relax\let\enddocument\relax%
- \newenvironment{document}{}{}%
- \renewcommand{\documentclass}[2][subfiles]{}}
+\RequirePackage{import}
% \end{macrocode}
+%
+% The |\import| command requires the path and the basename of files in separate arguments.
+% Therefore we have to decompose file names into these two components.
%
+% \begin{macrocode}
+\def\subfiles@split#1{%
+ \edef\subfiles@filename{#1}%
+ \def\subfiles@dir{}%
+ \def\subfiles@base{}%
+ \def\subfiles@sep{}%
+ \expandafter\subfiles@split@\subfiles@filename/\@nil/%
+}
+\def\subfiles@split@#1/{%
+ \def\tmp{#1}%
+ \ifx\tmp\@nnil
+ \let\subfiles@next\relax
+ \else
+ \edef\subfiles@dir{\subfiles@dir\subfiles@base\subfiles@sep}%
+ \def\subfiles@base{#1}%
+ \def\subfiles@sep{/}%
+ \let\subfiles@next\subfiles@split@
+ \fi
+ \subfiles@next
+}
+% \end{macrocode}
+%
+% After executing e.g.\ |\subfiles@split{../dir1/dir2/file.tex}|, the commands |\subfiles@dir| and |\subfiles@base| expand to |../dir1/dir2/| and |file.tex|, respectively.
+%
% \DescribeMacro{\subfile}
-% Note that the new command |\subfile| calls for |\skip@preamble| \emph{within a group}. The changes to |document| and |\documentclass| are undone after the inclusion of the subfile.
+% The command |\subfile| first redefines |\documentclass| and the |document| environment to do nothing.
+% To avoid spurious spaces we |\ignorespaces|.
+% Moreover, we have to set |\document| to the value it usually has after the end of the preamble, since some commands check this value and raise an error saying that the command cannot be used in the preamble.
+%
% \begin{macrocode}
-\newcommand\subfile[1]{\begingroup\skip@preamble\input{#1}\endgroup}
+\newcommand\subfile[1]{%
+ \begingroup
+ \renewcommand\documentclass[2][subfiles]{\ignorespaces}%
+ \renewenvironment{document}{\let\document\@onlypreamble\ignorespaces}{}%
% \end{macrocode}
-% If the package is being processed as part of the main file, then we are done. However, if the initial document class was |subfiles|, then the main file is loaded as part of a subfile. In this case the subsequent text between |\begin{document}| and |\end{document}| has to be skipped, but the contents of the commands |\document| and |\enddocument| has to be retained for using it with the contents of the subfile. Therefore we save the contents of the two commands as |\old@document@subfiles| and |\old@enddocument@subfiles|, respectively. Now the |document| environment is redefined to become the |comment| environment from the |verbatim| package. As a consequence, the body of the main file is ignored by \LaTeX, and only the preamble is read (and anything that comes after |\end{document}|!).
+%
+% Now we split the file name into path and base name and |\subimport| it.
+%
+% \begin{macrocode}
+ \subfiles@split{#1}%
+ \subimport{\subfiles@dir}{\subfiles@base}%
+ \unskip
+ \endgroup
+}
+% \end{macrocode}
+%
+% Note that the changes to |\documentclass| and the |document| environment happen \emph{within a group}, so they are undone after inclusion of the subfile.
+%
+% If the package is being processed as part of the main file, then we are done.
+% However, if the initial document class was |subfiles|, then the main file is loaded as part of a subfile.
+% In this case anything between |\begin{document}| and |\end{document}| has to be skipped, while the contents of the commands |\document| and |\enddocument| has to be retained for later use in the subfile.
+% Therefore we save the contents of the two commands as |\subfiles@document| and |\subfiles@enddocument|, respectively.
+% Now the |document| environment is redefined to become the |comment| environment from the |verbatim| package.
+% Consequently, the body of the main file is ignored by \LaTeX, and only the preamble is read (as well as anything that comes after |\end{document}|!).
% \begin{macrocode}
\@ifclassloaded{subfiles}{%
- \let\old@document@subfiles\document
- \let\old@enddocument@subfiles\enddocument
+ \let\subfiles@document\document
+ \let\subfiles@enddocument\enddocument
\let\document\comment
\let\enddocument\endcomment
}{}
%</package>
% \end{macrocode}
-% By loading the |subfiles| package immediately before |\begin{document}| we ensure that |\old@document@subfiles| and |\old@enddocument@subfiles| contain all modifications that the class and the preamble of the main file may have applied to the |document| environment. E.g., the class |revtex4| prepends some commands to |\document|. \ No newline at end of file
+%
+% By loading the |subfiles| package immediately before |\begin{document}| we ensure that |\subfiles@document| and |\subfiles@enddocument| contain all modifications that the class and the preamble of the main file may have applied to the |document| environment.
+% This happens e.g.\ with the class |revtex4| and the package |pythontex|. \ No newline at end of file
diff --git a/Master/texmf-dist/source/latex/subfiles/subfiles.ins b/Master/texmf-dist/source/latex/subfiles/subfiles.ins
index f188c09f376..95380c1670b 100644
--- a/Master/texmf-dist/source/latex/subfiles/subfiles.ins
+++ b/Master/texmf-dist/source/latex/subfiles/subfiles.ins
@@ -1,6 +1,6 @@
% subfiles - class and package for multi-file projects in LaTeX
% Copyright 2002, 2012 Federico Garcia (feg8@pitt.edu, fedegarcia@hotmail.com)
-% Copyright 2018 Gernot Salzer (salzer@logic.at)
+% Copyright 2018, 2019 Gernot Salzer (salzer@logic.at)
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
@@ -23,7 +23,7 @@
This is a generated file.
-Copyright 2002, 2012 Federico Garcia; 2018 Gernot Salzer
+Copyright 2002, 2012 Federico Garcia; 2018, 2019 Gernot Salzer
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3