texlive[56289] Master/texmf-dist: subfiles (7sep20)
commits+karl at tug.org
commits+karl at tug.org
Mon Sep 7 23:23:38 CEST 2020
Revision: 56289
http://tug.org/svn/texlive?view=revision&revision=56289
Author: karl
Date: 2020-09-07 23:23:38 +0200 (Mon, 07 Sep 2020)
Log Message:
-----------
subfiles (7sep20)
Modified Paths:
--------------
trunk/Master/texmf-dist/doc/latex/subfiles/subfiles.pdf
trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
trunk/Master/texmf-dist/source/latex/subfiles/subfiles.ins
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-09-07 21:23:16 UTC (rev 56288)
+++ trunk/Master/texmf-dist/source/latex/subfiles/subfiles.dtx 2020-09-07 21:23:38 UTC (rev 56289)
@@ -23,12 +23,12 @@
%
%<*driver>
% \fi
-\ProvidesFile{subfiles.dtx}[2020/02/14 v1.6 Multi-file projects]
+\ProvidesFile{subfiles.dtx}[2020/09/07 v2.0 Multi-file projects]
% \iffalse
\documentclass{ltxdoc}
\GetFileInfo{subfiles.dtx}
\title{A Document Class and a Package\\for Handling Multi-File Projects}
-\date{2020/02/14 v1.6}
+\date{2020/09/07 v2.0}
\author{Federico Garcia, Gernot Salzer}
\usepackage{hyperref}
\begin{document}
@@ -38,13 +38,13 @@
%</driver>
% \fi
% \begin{abstract}
-% 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.
+% The |subfiles| package allows authors to split a document into one main file and several subsidiary files (subfiles) akin to the |\input| command, with the added benefit of making the subfiles compilable on their own.
% This is achieved by reusing the preamble of the main file also for the subfiles.
% \end{abstract}
% \tableofcontents
% \section{Introduction}
% 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.
+% 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.
%
% 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.
@@ -63,9 +63,10 @@
% \section{Basic usage}
%
% \DescribeMacro{subfiles.sty}
-% 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 end of the preamble}:
+% The main file, i.e., the file with the preamble to be shared with the subfiles, has to load the package |subfiles|:
% \begin{center}
% \begin{tabular}{l}
+% |\documentclass[...]{...}|\\
% |\usepackage{subfiles}|\\
% |\begin{document}|
% \end{tabular}
@@ -83,7 +84,7 @@
% 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:
+% Thus, we have the following structure.
% \begin{center}\small
% \begin{tabular}[t]{l}
% \multicolumn{1}{c}{main file} \\
@@ -113,15 +114,117 @@
% \begin{itemize}
% \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.
+% \item If \LaTeX\ is run on the main file, the subfile is loaded like with an |\input| command, except that the preamble of the subfile up to |\begin{document}| as well as |\end{document}| and the lines following it are ignored.
% \end{itemize}
%
% \section{Advanced usage}
%
+% \subsection{Including files instead of inputting them}
+%
+% \DescribeMacro{\subfileinclude}
+% In plain \LaTeX, you can use either |\input| or |\include| to load a file.
+% In most cases the first is appropriate, but sometimes there are reasons to prefer the latter.
+% Internally, the |\subfile| command uses |\input|.
+% For those cases where you need |\include|, the package provides the command
+% \begin{center}
+% |\subfileinclude{|\meta{subfile\_name}|}|
+% \end{center}
+%
+% \subsection{Fixing pathes}
+%
+% \DescribeMacro{\subfix}
+% Whenever an error message of \LaTeX\ or an external program indicates that a file cannot be found, the reason may be that the missing file has to be addressed by varying pathes, depending on which file is typeset.
+% In such a case, it may help to apply the command |\subfix| to the file or path names.
+% Examples:
+% \begin{center}
+% \begin{tabular}{ll}
+% package & command when used with |subfiles| \\
+% \hline
+% |biblatex| & |\addbibresource{\subfix{|\meta{file}|}}| \\
+% |bibunits| & |\putbib[\subfix{|\meta{file1}|},\subfix{|\meta{file2}|},|\dots|]|\\
+% & |\defaultbibliography{\subfix{|\meta{file1}|},|\dots|}|
+% \end{tabular}
+% \end{center}
+% \DescribeMacro{\bibliography}
+% \DescribeMacro{\graphicspath}
+% Some commands already apply the fix on the fly.
+% At the moment these are the standard \LaTeX\ command |\bibliography| and |\graphicspath| from the |graphics|/|graphicx| package.
+%
+% \subsection{Conditional execution of commands}
+%
+% \DescribeMacro{\ifSubfilesClassLoaded}
+% The command |\ifSubfilesClassLoaded| is useful to execute commands conditionally, depending on whether the main file is typeset or a subfile.
+% \begin{center}
+% \begin{tabular}{l}
+% |\ifSubfilesClassLoaded{% then branch|\\
+% \quad \dots\ commands executed when the subfile is typeset \dots\\
+% |}{% else branch|\\
+% \quad \dots\ commands executed when the main file is typeset \dots\\
+% |}|
+% \end{tabular}
+% \end{center}
+% As an example, this can be used to add the bibliography to the main document or to the subdocument, whichever is typeset:
+% \begin{center}\small
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{main file} \\
+% \hline
+% |\documentclass[...]{...}| \\
+% |\usepackage{subfiles}|\\
+% |\bibliographystyle{alpha}|\\
+% |\begin{document}| \\
+% \dots \\
+% |\subfile{|\meta{subfile\_name}|}| \\
+% \dots \\
+% |\bibliography{bibfile}|\\
+% |\end{document}| \\
+% \hline
+% \end{tabular}
+% \hfill
+% \begin{tabular}[t]{l}
+% \multicolumn{1}{c}{subfile} \\
+% \hline
+% |\documentclass[|\meta{main\_file\_name}|]{subfiles}| \\
+% |\begin{document}| \\
+% \dots \\
+% |\ifSubfilesClassLoaded{%|\\
+% \quad|\bibliography{bibfile}%|\\
+% |}{}|\\
+% |\end{document}| \\
+% \hline
+% \end{tabular}
+% \end{center}
+%
+% \subsection{Unusual locations for placing definitions and text}
+%
+% Starting with version 2.0, the |subfiles| package treats sub-preambles and text after |\end{document}| as one would expect:
+% 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:
+% \begin{center}
+% |\usepackage[v1]{subfiles}|
+% \end{center}
+% 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.
+% 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.
+% This means that with the option |v1|, 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 here may not work outside.
+%
+% \emph{Code after \texttt{\textbackslash end\{document\}} in a subfile} is treated like the code preceding it when the subfile is loaded from the main file, but is ignored when typesetting the subfile.
+% The code after |\end{document}| behaves as if following the |\subfile| command in the main file, except that it is still part of the group enclosing the subfile.
+% As a consequence, empty lines at the end of the subfile lead to a new paragraph in the main document, even if the |\subfile| command is immediately followed by text.
+%
+% \section{Use cases}
+%
% \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.
+% Sometimes it is desirable to put a subfile together with its images and supplementary 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 file 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:
@@ -176,73 +279,12 @@
% \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{Including files instead of inputting them}
%
-% \DescribeMacro{\subfileinclude}
-% In plain \LaTeX, you can use either |\input| or |\include| to load a file.
-% In most cases the first is appropriate, but sometimes there are reasons to prefer the latter.
-% Internally, the |\subfile| command uses |\input|.
-% For those cases where you need |\include|, the package provides the command
-% \begin{center}
-% |\subfileinclude{|\meta{subfile\_name}|}|
-% \end{center}
-%
-% \subsection{Bibliographies}
-%
-% Manual bibliographies with the |thebibliography| environment work as usual.
-% Problems may arise if external programs like |bibtex| or |biber| are used to generate the bibliography.
-% Here are some hints on how to make it work.
-% \begin{itemize}
-% \item
-% \DescribeMacro{\bibliography}
-% Make sure the command |\bibliography| is executed after loading the |subfiles| package.
-% Put the command between |\usepackage{subfiles}| and |\begin{document}| or somewhere into the text part.
-% \item When you use the package |biblatex|, and programs like |biber| complain about not being able to find the bibliography files, use |\bibliography| instead of |\addbibresource| (see above), or the command |\subfix| (see below).
-% \item
-% \DescribeMacro{\subfix}
-% Whenever an external program complains that a file specified in the \LaTeX\ document cannot be found, wrap the command |\subfix| around the filename.
-% Here are some examples.
-% \begin{center}
-% \begin{tabular}{ll}
-% package & command when used with |subfiles| \\
-% \hline
-% |biblatex| & |\addbibresource{\subfix{|\meta{file}|}}| \\
-% |bibunits| & |\putbib[\subfix{|\meta{file1}|},\subfix{|\meta{file2}|},|\dots|]|\\
-% & |\defaultbibliography{\subfix{|\meta{file1}|},|\dots|}|
-% \end{tabular}
-% \end{center}
-%
-% \end{itemize}
-% The |subfiles| package has been tested with the packages |biblatex|, |bibunits|, and |chapterbib| as well as with the external programs |bibtex| and |biber|.
-%
-% \subsection{Unusual locations for placing definitions and text}
-%
-% Usually all definitions and packages required by the subfiles should go into the preamble of the main file.
-% There are some further locations, though, where one might consider adding definitions and text.
-% Put negatively, apparently irrelevant stuff in these locations may become unexpectedly visible in the document or cause errors.
-%
-% \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 |\begin{document}| and |\end{document}|.
-% This has two consequences: \emph{a)}~the user can add some commands that are 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).
-% \bigskip
-%
-% Similarly, when typesetting the main document, the |\subfile| command does not really load the stuff within the |document| environment, but \emph{everything except} the three commands |\documentclass[...]{...}|, |\begin{document}|, and |\end{document}|.
-% This has the following consequences.
-%
-% \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.
-%
-% \paragraph{Code after \texttt{\textbackslash end\{document\}} in a subfile} is treated like the code preceding it when the subfile is loaded from the main file, but is ignored when typesetting the subfile.
-% The code after |\end{document}| behaves as if following the |\subfile| command in the main file, except that it is still part of the group enclosing the subfile.
-% As a consequence, empty lines at the end of the subfile lead to a new paragraph in the main document, even if the |\subfile| command is immediately followed by text.
-%
% \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 similar to |\input|.
+% In this respect, |\subfile| behaves similar to |\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}|.
-% Moreover, any stuff after |\end{document}| will end up in the main document, including spurious empty lines, which may lead to a new paragraph.
% Therefore, to load the contents of a subfile without intervening spaces, you have either to add comment signs:
% \begin{center}
% \begin{tabular}[t]{l}
@@ -262,7 +304,6 @@
% |\begin{document}|\\
% |contents of subfile%|\\
% |\end{document}|\\
-% |% No empty lines after \end{document}!|\\
% \hline
% \end{tabular}
% \end{center}
@@ -280,29 +321,25 @@
% \item
% Make sure to use the most recent version of the |subfiles| package, available from CTAN\footnote{\url{https://ctan.org/pkg/subfiles}} and Github\footnote{\url{https://github.com/gsalzer/subfiles}}.
% \item
-% Make sure that |\usepackage{subfiles}| comes last in the preamble
-% of the main document.
+% 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
-% Make sure that there is no stuff after |\end{document}|, neither in the main file nor in the subfiles.
-% \item
-% If there is anything in the preambles of the subfiles, make sure that it is admissible for both, the preamble and the document area.
-% \item
-% If nothing of the above helps and you are stuck, ask the 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/}}.
% \end{enumerate}
%
% \section{Dependencies}
%
-% 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.
+% 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.
%
% \section{Version history}
%
% \begin{description}
% \item[v1.1:]
-% Initial version by Federico Garcia. Further versions by Gernot Salzer.
+% Initial version by Federico Garcia. Subsequent versions by Gernot Salzer.
% \item[v1.2:] \mbox{}
% \begin{itemize}
% \item Incompatibility with classes and packages removed that modify the |\document| command, like the class |revtex4|.
@@ -332,6 +369,19 @@
% \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{}
+% \begin{itemize}
+% \item Incompatibility with \LaTeX\ Oct.~2020 removed.
+% Thanks to Ulrike \mbox{Fischer} from the \LaTeX~3 team for the warning in time.
+% \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.
+% Thanks to Github user |alan-isaac| for reporting the issue.
+% \item Subfiles now can have the same name as the main file.
+% Thanks to Github user |June-6th| for reporting the issue.
+% \item Problem with the search path for images resolved.
+% Thanks to Github user |maxnick| for reporting the issue.
+% \end{itemize}
% \end{description}
%
%\section{The Implementation}
@@ -339,142 +389,172 @@
% \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
-\ProvidesClass{subfiles}[2020/02/14 v1.6 Multi-file projects (class)]
-\DeclareOption*{\typeout{Preamble taken from file `\CurrentOption'}%
- \let\preamble at file\CurrentOption}
+\ProvidesClass{subfiles}[2020/09/07 v2.0 Multi-file projects (class)]
+\DeclareOption*{%
+ \typeout{Preamble taken from file `\CurrentOption'}%
+ \let\preamble at file\CurrentOption
+}
\ProcessOptions
% \end{macrocode}
+%
% After processing the option of the |subfiles| class, we reset |\@classoptionslist| such that the options in the main file will be processed.
%
% \begin{macrocode}
\let\@classoptionslist\relax
% \end{macrocode}
-% Then we save the original definition of |\documentclass| to be able to reset it later to this definition:
+%
+% To handle subfiles in separate directories, we use the |import| package.
+% We load it now, since it resets the macro |\import at path|.
+% \begin{macrocode}
+\RequirePackage{import}
+% \end{macrocode}
%
+% We redefine |\documentclass| to load the class of the main document.
+%
% \begin{macrocode}
\let\subfiles at documentclass\documentclass
+\def\documentclass{%
+ \let\documentclass\subfiles at documentclass
+ \LoadClass
+}
% \end{macrocode}
%
-% Now |\documentclass| is set equal to |\LoadClass| such that the class and the options of the main file will be loaded as usual.
-%
+% 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}
-\let\documentclass\LoadClass\relax
+\filename at parse{\preamble at file}
+\edef\import at path{\filename at area}
+\edef\input at path{{\filename at area}}
+\input{\preamble at file}
% \end{macrocode}
%
-% When typesetting a subfile, we have to skip the |document| environment of the main file.
-% This is done with the commands |\comment| and |\endcomment| from the |verbatim| package.
-% Now there is a problem:
-% If we load |verbatim| here, the definition of the commands may be overwritten if the user loads e.g.\ the |comment| package.
-% Loading |verbatim| in |subfiles.sty| at the latest possible moment is not reliable, either.
-% On the one hand we may overwrite macros required later by the user, on the other hand the |memoir| class contains a copy of |verbatim|, so a later |\RequirePackage| refuses to reload the package.
-% Thus, in the case of a document loading the |memoir| class and the |comment| package, we end up with the wrong definition of |\comment| in any case.
+% 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.
%
-% Therefore we load the |verbatim| package here and save the contents of the crucial commands |\comment| and |\endcomment| under a different name.
% \begin{macrocode}
-\RequirePackage{verbatim}
-\let\subfiles at comment\comment
-\let\subfiles at endcomment\endcomment
+{\makeatletter
+ \gdef\import at path{}
+}
+%</class>
% \end{macrocode}
-%
-% To handle subfiles in separate directories, we load the |import| package.
-%
+%
+%
+% \subsection{The package}
% \begin{macrocode}
-\RequirePackage{import}
+%<*package>
+\NeedsTeXFormat{LaTeX2e}
+\ProvidesPackage{subfiles}[2020/09/07 v2.0 Multi-file projects (package)]
% \end{macrocode}
%
-% The |\subimport| command requires path and filename as separate arguments, so we have to split file locations 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.
-% Therefore we redefine the command |\filename at simple| temporarily; it is responsible for this unwanted split.
-%
+% 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.
+%
+% 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 split#1{%
- \let\subfiles at filename@simple\filename at simple
- \def\filename at simple##1.\\{\edef\filename at base{##1}}%
- \filename at parse{#1}%
- \let\filename at simple\subfiles at filename@simple
+\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\}}%
}
% \end{macrocode}
%
-% E.g., after executing |\subfiles at split{../dir1/dir2/file.tex}| the macros |\filename at area| and |\filename at base| expand to |../dir1/dir2/| and |file.tex|, respectively.
+% 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).
%
-% 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 at split{\preamble at file}
-\subimport{\filename at area}{\filename at base}
+\def\subfiles at skipDocument{%
+ \let\subfiles at skiplinescont\ignorespaces
+ \let\subfiles at skiplinesend\subfiles at ENDDOCUMENT
+ \subfiles at skiplines
+}
% \end{macrocode}
%
-% The main file loads the package |subfiles| as part of the preamble, which saves the contents of |\document| and |\enddocument| as |\subfiles at document| and |\subfiles at enddocument|, respectively.
-% We use these macors now to restore the original values of |\document|, |\enddocument|, and |\documentclass|.
-% The backup commands are |\undefined| to save memory.
-% That's it.
+% To skip |\documentclass| the old way, we redefine it to do nothing except restoring its original definition.
%
% \begin{macrocode}
-{\catcode`\@=11
-\global\let\document\subfiles at document
-\global\let\enddocument\subfiles at enddocument
-\global\let\documentclass\subfiles at documentclass
-\global\let\subfiles at document\undefined
-\global\let\subfiles at enddocument\undefined
-\global\let\subfiles at documentclass\undefined
+\def\subfiles at skipDocumentclass{%
+ \renewcommand\documentclass[2][]{%
+ \let\documentclass\subfiles at documentclass
+ \ignorespaces
+ }%
}
-%</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 at 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.
+% 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{%
+ \let\documentclass\subfiles at documentclass
+ \def\subfiles at skiplinescont{\begin{document}}%
+ \let\subfiles at skiplinesend\subfiles at BEGINDOCUMENT
+ \subfiles at skiplines
+ }%
+}
+% \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.
+% \begin{macrocode}
+\let\subfiles at handleMain\endinput
+\let\subfiles at handleSubpreamble\subfiles at skipPreamble
+\let\subfiles at handleTextAfterSubdocument\endinput
+% \end{macrocode}
%
-% \subsection{The package}
-%
-% Any option will be ignored.
-%
+% 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.
% \begin{macrocode}
-%<*package>
-\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{subfiles}[2020/02/14 v1.6 Multi-file projects (package)]
-\DeclareOption*{\PackageWarning{\CurrentOption ignored}}
-\ProcessOptions
+\DeclareOption{v1}{%
+ \let\subfiles at handleMain\subfiles at skipDocument
+ \let\subfiles at handleSubpreamble\subfiles at skipDocumentclass
+ \let\subfiles at handleTextAfterSubdocument\relax
+}
+\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}
%
-% 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 at document| and |\subfiles at enddocument|, respectively.
-% Now the |document| environment is redefined to become the saved |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}|!).
-%
+% 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}
-\@ifclassloaded{subfiles}{%
- \let\subfiles at document\document
- \let\subfiles at enddocument\enddocument
- \let\document\subfiles at comment
- \let\enddocument\subfiles at endcomment
+\RequirePackage{import}
% \end{macrocode}
-%
-% By loading the |subfiles| package immediately before |\begin{document}| we ensure that |\subfiles at document| and |\subfiles at 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|.
-%
-% We use the |import| package to handle subfiles in separate directories.
+%
% The |\subimport| command requires path and filename as separate arguments, so we have to split file locations 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.
% Therefore we redefine the command |\filename at simple| temporarily; it is responsible for this unwanted split.
-% Both things, loading the package and defining the command, are also done in |subfiles.cls|, so we have to execute this code only if we are typesetting the main file.
-%
+%
% \begin{macrocode}
-}{% subfiles class not loaded, we typeset the main document
- \RequirePackage{import}
- \def\subfiles at split#1{%
- \let\subfiles at filename@simple\filename at simple
- \def\filename at simple##1.\\{\edef\filename at base{##1}}%
- \filename at parse{#1}%
- \let\filename at simple\subfiles at filename@simple
- }
-}
+\def\subfiles at split#1{%
+ \let\subfiles at filename@simple\filename at simple
+ \def\filename at simple##1.\\{\edef\filename at base{##1}}%
+ \filename at parse{#1}%
+ \let\filename at simple\subfiles at filename@simple
+}%
% \end{macrocode}
%
% E.g., after executing |\subfiles at split{../dir1/dir2/file.tex}| the macros |\filename at area| and |\filename at base| expand to |../dir1/dir2/| and |file.tex|, respectively.
@@ -481,6 +561,7 @@
%
% \DescribeMacro{\subfile}
% The command |\subfile| specifies the command |\subimport| for |\input|ing the subfile, and then calls |\subfiles at subfile|.
+%
% \begin{macrocode}
\newcommand\subfile{%
\let\subfiles at loadfile\subimport
@@ -490,6 +571,7 @@
%
% \DescribeMacro{\subfileinclude}
% The command |\subfileinclude| specifies the command |\subincludefrom| for |\include|ing the subfile, and then calls |\subfiles at subfile|.
+%
% \begin{macrocode}
\newcommand\subfileinclude{%
\let\subfiles at loadfile\subincludefrom
@@ -497,7 +579,7 @@
}
% \end{macrocode}
%
-% The main functionality of the two |\subfile| commands is implemented in |\subfiles at subfile|.
+% 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.
%
@@ -507,10 +589,7 @@
\let\subfiles at documentclass\documentclass
\let\subfiles at document\document
\let\subfiles at enddocument\enddocument
- \renewcommand\documentclass[2][subfiles]{%
- \let\documentclass\subfiles at documentclass
- \ignorespaces
- }%
+ \subfiles at handleSubpreamble
\renewenvironment{document}{%
\let\document\subfiles at document
\ignorespaces
@@ -517,8 +596,10 @@
}{%
\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}
@@ -528,36 +609,87 @@
}
% \end{macrocode}
%
-% To let external programs find files, we have to add the |\import at path| to file names.
-% This is accomplished with the command |\subfiles at addimportpath|.
+% \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.
+% In order to succeed, the filename has to be expanded immediately, such that the current value of |\import at path| is used.
+%
% \begin{macrocode}
-\def\subfiles at addimportpath#1{%
- \def\subfiles at filelist{}%
+\def\subfix#1{\import at path#1}
+% \end{macrocode}
+%
+% For patching a list of file or path names, we define two auxiliary macros, one iterating over a comma-separated list of names and one processing a sequence of names enclosed in braces.
+%
+% \begin{macrocode}
+\def\subfiles at fixfilelist#1{%
+ \def\subfiles at list{}%
\def\subfiles at sep{}%
- \@for\subfiles at filename:=#1\do{%
- \edef\subfiles at filelist{%
- \subfiles at filelist
- \subfiles at sep
- \import at path
- \subfiles at filename
- }%
+ \@for\subfiles at tmp:=#1\do{%
+ \edef\subfiles at list{\subfiles at list\subfiles at sep\subfix{\subfiles at tmp}}%
\def\subfiles at sep{,}%
- }
+ }%
}
+\def\subfiles at fixpathlist#1{%
+ \def\subfiles at list{}%
+ \@tfor\subfiles at tmp:=#1\do{%
+ \edef\subfiles at list{\subfiles at list{\subfix\subfiles at tmp}}%
+ }%
+}
% \end{macrocode}
%
% \DescribeMacro{\bibliography}
-% We redefine the |\bibliography| command such that the import path is added to the file names before the original command is called.
+% \DescribeMacro{\graphicspath}
+% We patch |\bibliography| and |\graphicspath| (from the |graphics|/|graphicx| package) such that users don't have to worry about adding |\subfix|.
+%
% \begin{macrocode}
\let\subfiles at bibliography\bibliography
\renewcommand\bibliography[1]{%
- \subfiles at addimportpath{#1}%
- \expandafter\subfiles at bibliography\expandafter{\subfiles at filelist}%
+ \subfiles at fixfilelist{#1}%
+ \expandafter\subfiles at bibliography\expandafter{\subfiles at list}%
}
+\@ifpackageloaded{graphics}{%
+ \let\subfiles at graphicspath\graphicspath
+ \renewcommand\graphicspath[1]{%
+ \subfiles at fixpathlist{#1}%
+ \edef\subfiles at list{{\subfix{}}\subfiles at list}%
+ \expandafter\subfiles at graphicspath\expandafter{\subfiles at list}%
+ }%
+}{}
% \end{macrocode}
%
-% \DescribeMacro{\subfix}
-% Instead of adding further fixes for other packages that write filenames to external files (like |bibunits|), we provide a command for adding the |\import at path| to a filename.
+% \DescribeMacro{\ifSubfilesClassLoaded}
+% To add code or text conditionally, depending on whether the main document or a subfile is typeset, we provide the command |\ifSubfilesClassLoaded|.
+%
% \begin{macrocode}
-\def\subfix#1{\import at path#1}
+\newcommand\ifSubfilesClassLoaded{%
+ \expandafter\ifx\csname ver at subfiles.cls\endcsname\relax
+ \expandafter\@secondoftwo
+ \else
+ \expandafter\@firstoftwo
+ \fi
+}
% \end{macrocode}
+%
+% 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|.
+%
+% \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
+ }%
+}{}
+%</package>
+% \end{macrocode}
Modified: trunk/Master/texmf-dist/source/latex/subfiles/subfiles.ins
===================================================================
--- trunk/Master/texmf-dist/source/latex/subfiles/subfiles.ins 2020-09-07 21:23:16 UTC (rev 56288)
+++ trunk/Master/texmf-dist/source/latex/subfiles/subfiles.ins 2020-09-07 21:23:38 UTC (rev 56289)
@@ -1,6 +1,6 @@
% subfiles - class and package for multi-file projects in LaTeX
% Copyright 2002, 2012 Federico Garcia (feg8 at pitt.edu, fedegarcia at hotmail.com)
-% Copyright 2018, 2019 Gernot Salzer (salzer at logic.at)
+% Copyright 2018-2020 Gernot Salzer (salzer at 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, 2019 Gernot Salzer
+Copyright 2002, 2012 Federico Garcia; 2018-2020 Gernot Salzer
This work may be distributed and/or modified under the
conditions of the LaTeX Project Public License, either version 1.3
Modified: trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls
===================================================================
--- trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls 2020-09-07 21:23:16 UTC (rev 56288)
+++ trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.cls 2020-09-07 21:23:38 UTC (rev 56289)
@@ -8,7 +8,7 @@
%%
%% This is a generated file.
%%
-%% Copyright 2002, 2012 Federico Garcia; 2018, 2019 Gernot Salzer
+%% Copyright 2002, 2012 Federico Garcia; 2018-2020 Gernot Salzer
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
@@ -26,32 +26,25 @@
%% and the derived files subfiles.sty and subfiles.pdf
%%
\NeedsTeXFormat{LaTeX2e}
-\ProvidesClass{subfiles}[2020/02/14 v1.6 Multi-file projects (class)]
-\DeclareOption*{\typeout{Preamble taken from file `\CurrentOption'}%
- \let\preamble at file\CurrentOption}
+\ProvidesClass{subfiles}[2020/09/07 v2.0 Multi-file projects (class)]
+\DeclareOption*{%
+ \typeout{Preamble taken from file `\CurrentOption'}%
+ \let\preamble at file\CurrentOption
+}
\ProcessOptions
\let\@classoptionslist\relax
+\RequirePackage{import}
\let\subfiles at documentclass\documentclass
-\let\documentclass\LoadClass\relax
-\RequirePackage{verbatim}
-\let\subfiles at comment\comment
-\let\subfiles at endcomment\endcomment
-\RequirePackage{import}
-\def\subfiles at split#1{%
- \let\subfiles at filename@simple\filename at simple
- \def\filename at simple##1.\\{\edef\filename at base{##1}}%
- \filename at parse{#1}%
- \let\filename at simple\subfiles at filename@simple
+\def\documentclass{%
+ \let\documentclass\subfiles at documentclass
+ \LoadClass
}
-\subfiles at split{\preamble at file}
-\subimport{\filename at area}{\filename at base}
-{\catcode`\@=11
-\global\let\document\subfiles at document
-\global\let\enddocument\subfiles at enddocument
-\global\let\documentclass\subfiles at documentclass
-\global\let\subfiles at document\undefined
-\global\let\subfiles at enddocument\undefined
-\global\let\subfiles at documentclass\undefined
+\filename at parse{\preamble at file}
+\edef\import at path{\filename at area}
+\edef\input at path{{\filename at area}}
+\input{\preamble at file}
+{\makeatletter
+ \gdef\import at path{}
}
\endinput
%%
Modified: trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty
===================================================================
--- trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty 2020-09-07 21:23:16 UTC (rev 56288)
+++ trunk/Master/texmf-dist/tex/latex/subfiles/subfiles.sty 2020-09-07 21:23:38 UTC (rev 56289)
@@ -8,7 +8,7 @@
%%
%% This is a generated file.
%%
-%% Copyright 2002, 2012 Federico Garcia; 2018, 2019 Gernot Salzer
+%% Copyright 2002, 2012 Federico Garcia; 2018-2020 Gernot Salzer
%%
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
@@ -26,23 +26,60 @@
%% and the derived files subfiles.sty and subfiles.pdf
%%
\NeedsTeXFormat{LaTeX2e}
-\ProvidesPackage{subfiles}[2020/02/14 v1.6 Multi-file projects (package)]
-\DeclareOption*{\PackageWarning{\CurrentOption ignored}}
-\ProcessOptions
-\@ifclassloaded{subfiles}{%
- \let\subfiles at document\document
- \let\subfiles at enddocument\enddocument
- \let\document\subfiles at comment
- \let\enddocument\subfiles at endcomment
-}{% subfiles class not loaded, we typeset the main document
- \RequirePackage{import}
- \def\subfiles at split#1{%
- \let\subfiles at filename@simple\filename at simple
- \def\filename at simple##1.\\{\edef\filename at base{##1}}%
- \filename at parse{#1}%
- \let\filename at simple\subfiles at filename@simple
- }
+\ProvidesPackage{subfiles}[2020/09/07 v2.0 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\subfiles at skipDocument{%
+ \let\subfiles at skiplinescont\ignorespaces
+ \let\subfiles at skiplinesend\subfiles at ENDDOCUMENT
+ \subfiles at skiplines
+}
+\def\subfiles at skipDocumentclass{%
+ \renewcommand\documentclass[2][]{%
+ \let\documentclass\subfiles at documentclass
+ \ignorespaces
+ }%
+}
+\def\subfiles at skipPreamble{%
+ \def\documentclass{%
+ \let\documentclass\subfiles at documentclass
+ \def\subfiles at skiplinescont{\begin{document}}%
+ \let\subfiles at skiplinesend\subfiles at BEGINDOCUMENT
+ \subfiles at skiplines
+ }%
+}
+\let\subfiles at handleMain\endinput
+\let\subfiles at handleSubpreamble\subfiles at skipPreamble
+\let\subfiles at handleTextAfterSubdocument\endinput
+\DeclareOption{v1}{%
+ \let\subfiles at handleMain\subfiles at skipDocument
+ \let\subfiles at handleSubpreamble\subfiles at skipDocumentclass
+ \let\subfiles at handleTextAfterSubdocument\relax
+}
+\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
+ \def\filename at simple##1.\\{\edef\filename at base{##1}}%
+ \filename at parse{#1}%
+ \let\filename at simple\subfiles at filename@simple
+}%
\newcommand\subfile{%
\let\subfiles at loadfile\subimport
\subfiles at subfile
@@ -56,10 +93,7 @@
\let\subfiles at documentclass\documentclass
\let\subfiles at document\document
\let\subfiles at enddocument\enddocument
- \renewcommand\documentclass[2][subfiles]{%
- \let\documentclass\subfiles at documentclass
- \ignorespaces
- }%
+ \subfiles at handleSubpreamble
\renewenvironment{document}{%
\let\document\subfiles at document
\ignorespaces
@@ -66,30 +100,60 @@
}{%
\let\enddocument\subfiles at enddocument
\@ignoretrue
+ \subfiles at handleTextAfterSubdocument
}%
\subfiles at split{#1}%
\subfiles at loadfile{\filename at area}{\filename at base}%
\endgroup
}
-\def\subfiles at addimportpath#1{%
- \def\subfiles at filelist{}%
+\def\subfix#1{\import at path#1}
+\def\subfiles at fixfilelist#1{%
+ \def\subfiles at list{}%
\def\subfiles at sep{}%
- \@for\subfiles at filename:=#1\do{%
- \edef\subfiles at filelist{%
- \subfiles at filelist
- \subfiles at sep
- \import at path
- \subfiles at filename
- }%
+ \@for\subfiles at tmp:=#1\do{%
+ \edef\subfiles at list{\subfiles at list\subfiles at sep\subfix{\subfiles at tmp}}%
\def\subfiles at sep{,}%
- }
+ }%
}
+\def\subfiles at fixpathlist#1{%
+ \def\subfiles at list{}%
+ \@tfor\subfiles at tmp:=#1\do{%
+ \edef\subfiles at list{\subfiles at list{\subfix\subfiles at tmp}}%
+ }%
+}
\let\subfiles at bibliography\bibliography
\renewcommand\bibliography[1]{%
- \subfiles at addimportpath{#1}%
- \expandafter\subfiles at bibliography\expandafter{\subfiles at filelist}%
+ \subfiles at fixfilelist{#1}%
+ \expandafter\subfiles at bibliography\expandafter{\subfiles at list}%
}
-\def\subfix#1{\import at path#1}
+\@ifpackageloaded{graphics}{%
+ \let\subfiles at graphicspath\graphicspath
+ \renewcommand\graphicspath[1]{%
+ \subfiles at fixpathlist{#1}%
+ \edef\subfiles at list{{\subfix{}}\subfiles at list}%
+ \expandafter\subfiles at graphicspath\expandafter{\subfiles at list}%
+ }%
+}{}
+\newcommand\ifSubfilesClassLoaded{%
+ \expandafter\ifx\csname ver at subfiles.cls\endcsname\relax
+ \expandafter\@secondoftwo
+ \else
+ \expandafter\@firstoftwo
+ \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
+ }%
+}{}
\endinput
%%
%% End of file `subfiles.sty'.
More information about the tex-live-commits
mailing list.