| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% subfiles - class and package for multi-file projects in LaTeX |
| 4 |
% Copyright 2002, 2012 Federico Garcia ([email protected], [email protected]) |
| 5 |
% Copyright 2018-2020 Gernot Salzer ([email protected]) |
| 6 |
% |
| 7 |
% This work may be distributed and/or modified under the |
| 8 |
% conditions of the LaTeX Project Public License, either version 1.3 |
| 9 |
% of this license or (at your option) any later version. |
| 10 |
% The latest version of this license is in |
| 11 |
% http://www.latex-project.org/lppl.txt |
| 12 |
% and version 1.3 or later is part of all distributions of LaTeX |
| 13 |
% version 2005/12/01 or later. |
| 14 |
% |
| 15 |
% This work has the LPPL maintenance status `maintained'. |
| 16 |
% |
| 17 |
% The Current Maintainer of this work is Gernot Salzer. |
| 18 |
% |
| 19 |
% This work consists of the files subfiles.dtx and subfiles.ins |
| 20 |
% and the derived files subfiles.cls, subfiles.sty and subfiles.pdf |
| 21 |
% |
| 22 |
% ------------------------------------------- |
| 23 |
% |
| 24 |
%<*driver> |
| 25 |
% \fi |
| 26 |
\ProvidesFile{subfiles.dtx}[2020/11/14 v2.2 Multi-file projects] |
| 27 |
% \iffalse |
| 28 |
\documentclass{ltxdoc} |
| 29 |
\GetFileInfo{subfiles.dtx} |
| 30 |
\title{A Document Class and a Package\\for Handling Multi-File Projects} |
| 31 |
\date{2020/11/14 v2.2} |
| 32 |
\author{Federico Garcia, Gernot Salzer} |
| 33 |
\usepackage{hyperref} |
| 34 |
\begin{document} |
| 35 |
\maketitle |
| 36 |
\DocInput{\filename} |
| 37 |
\end{document} |
| 38 |
%</driver> |
| 39 |
% \fi |
| 40 |
% \begin{abstract} |
| 41 |
% 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. |
| 42 |
% This is achieved by reusing the preamble of the main file also for the subfiles. |
| 43 |
% \end{abstract} |
| 44 |
% \tableofcontents |
| 45 |
% \section{Introduction} |
| 46 |
% The \LaTeX\ commands |\include| and |\input| allow the user to split the \TeX\ source of a document into several input files. |
| 47 |
% 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. |
| 48 |
% |
| 49 |
% In this process the rest of the document is of little use, and can even interfere. |
| 50 |
% For example, error messages may indicate not only the wrong line number, but may point to the wrong file. |
| 51 |
% Frequently, one ends up wanting to work only on the new file: |
| 52 |
% |
| 53 |
% \begin{itemize} |
| 54 |
% \item Create a new file, and copy-paste the preamble of the main file into it. |
| 55 |
% \item Work on this file, typeset it \emph{alone} as many times as necessary. |
| 56 |
% \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. |
| 57 |
% \end{itemize} |
| 58 |
% |
| 59 |
% It is desirable to reduce these three steps to the interesting, middle one. |
| 60 |
% 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. |
| 61 |
% This is what the class |subfiles.cls| and the package |subfiles.sty| are intended for. |
| 62 |
% |
| 63 |
% \section{Basic usage} |
| 64 |
% |
| 65 |
% \DescribeMacro{subfiles.sty} |
| 66 |
% The main file, i.e., the file with the preamble to be shared with the subfiles, has to load the package |subfiles|: |
| 67 |
% \begin{center} |
| 68 |
% \begin{tabular}{l} |
| 69 |
% |\documentclass[...]{...}|\\ |
| 70 |
% |\usepackage{subfiles}|\\ |
| 71 |
% |\begin{document}| |
| 72 |
% \end{tabular} |
| 73 |
% \end{center} |
| 74 |
% \DescribeMacro{\subfile} |
| 75 |
% Subordinate files (subfiles) are loaded from the main file or from other subfiles with the command |
| 76 |
% \begin{center} |
| 77 |
% |\subfile{|\meta{subfile\_name}|}| |
| 78 |
% \end{center} |
| 79 |
% \DescribeMacro{subfiles.cls} |
| 80 |
% The subfiles have to start with the line |
| 81 |
% \begin{center} |
| 82 |
% |\documentclass[|\meta{main\_file\_name}|]{subfiles}| |
| 83 |
% \end{center} |
| 84 |
% which loads the class |subfiles|. |
| 85 |
% Its only `option', which is actually mandatory, gives the name of the main file. |
| 86 |
% 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 |\|). |
| 87 |
% Thus, we have the following structure. |
| 88 |
% \begin{center}\small |
| 89 |
% \begin{tabular}[t]{l} |
| 90 |
% \multicolumn{1}{c}{main file} \\ |
| 91 |
% \hline |
| 92 |
% |\documentclass[...]{...}| \\ |
| 93 |
% \meta{shared preamble} \\ |
| 94 |
% |\usepackage{subfiles}| \\ |
| 95 |
% |\begin{document}| \\ |
| 96 |
% \dots \\ |
| 97 |
% |\subfile{|\meta{subfile\_name}|}| \\ |
| 98 |
% \dots \\ |
| 99 |
% |\end{document}| \\ |
| 100 |
% \hline |
| 101 |
% \end{tabular} |
| 102 |
% \hfill |
| 103 |
% \begin{tabular}[t]{l} |
| 104 |
% \multicolumn{1}{c}{subfile} \\ |
| 105 |
% \hline |
| 106 |
% |\documentclass[|\meta{main\_file\_name}|]{subfiles}| \\ |
| 107 |
% |\begin{document}| \\ |
| 108 |
% \dots \\ |
| 109 |
% |\end{document}| \\ |
| 110 |
% \hline |
| 111 |
% \end{tabular} |
| 112 |
% \end{center} |
| 113 |
% Now there are two possibilities. |
| 114 |
% \begin{itemize} |
| 115 |
% \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). |
| 116 |
% The rest of the subfile is processed normally. |
| 117 |
% \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. |
| 118 |
% \end{itemize} |
| 119 |
% |
| 120 |
% \section{Advanced usage} |
| 121 |
% |
| 122 |
% \subsection{Including files instead of inputting them} |
| 123 |
% |
| 124 |
% \DescribeMacro{\subfileinclude} |
| 125 |
% In plain \LaTeX, you can use either |\input| or |\include| to load a file. |
| 126 |
% In most cases the first is appropriate, but sometimes there are reasons to prefer the latter. |
| 127 |
% Internally, the |\subfile| command uses |\input|. |
| 128 |
% For those cases where you need |\include|, the package provides the command |
| 129 |
% \begin{center} |
| 130 |
% |\subfileinclude{|\meta{subfile\_name}|}| |
| 131 |
% \end{center} |
| 132 |
% |
| 133 |
% \subsection{Fixing pathes} |
| 134 |
% |
| 135 |
% \DescribeMacro{\subfix} |
| 136 |
% 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. |
| 137 |
% In such a case, it may help to apply the command |\subfix| to the file or path names. |
| 138 |
% Examples: |
| 139 |
% \begin{center} |
| 140 |
% \begin{tabular}{ll} |
| 141 |
% package & command when used with |subfiles| \\ |
| 142 |
% \hline |
| 143 |
% |biblatex| & |\addbibresource{\subfix{|\meta{file}|}}| \\ |
| 144 |
% |bibunits| & |\putbib[\subfix{|\meta{file1}|},\subfix{|\meta{file2}|},|\dots|]|\\ |
| 145 |
% & |\defaultbibliography{\subfix{|\meta{file1}|},|\dots|}| |
| 146 |
% \end{tabular} |
| 147 |
% \end{center} |
| 148 |
% \DescribeMacro{\bibliography} |
| 149 |
% \DescribeMacro{\graphicspath} |
| 150 |
% Some commands already apply the fix on the fly. |
| 151 |
% At the moment these are the standard \LaTeX\ command |\bibliography| and |\graphicspath| from the |graphics|/|graphicx| package. |
| 152 |
% |
| 153 |
% \subsection{Conditional execution of commands} |
| 154 |
% |
| 155 |
% \DescribeMacro{\ifSubfilesClassLoaded} |
| 156 |
% The command |\ifSubfilesClassLoaded| is useful to execute commands conditionally, depending on whether the main file is typeset or a subfile. |
| 157 |
% \begin{center} |
| 158 |
% \begin{tabular}{l} |
| 159 |
% |\ifSubfilesClassLoaded{% then branch|\\ |
| 160 |
% \quad \dots\ commands executed when the subfile is typeset \dots\\ |
| 161 |
% |}{% else branch|\\ |
| 162 |
% \quad \dots\ commands executed when the main file is typeset \dots\\ |
| 163 |
% |}| |
| 164 |
% \end{tabular} |
| 165 |
% \end{center} |
| 166 |
% As an example, this can be used to add the bibliography to the main document or to the subdocument, whichever is typeset: |
| 167 |
% \begin{center}\small |
| 168 |
% \begin{tabular}[t]{l} |
| 169 |
% \multicolumn{1}{c}{main file} \\ |
| 170 |
% \hline |
| 171 |
% |\documentclass[...]{...}| \\ |
| 172 |
% |\usepackage{subfiles}|\\ |
| 173 |
% |\bibliographystyle{alpha}|\\ |
| 174 |
% |\begin{document}| \\ |
| 175 |
% \dots \\ |
| 176 |
% |\subfile{|\meta{subfile\_name}|}| \\ |
| 177 |
% \dots \\ |
| 178 |
% |\bibliography{bibfile}|\\ |
| 179 |
% |\end{document}| \\ |
| 180 |
% \hline |
| 181 |
% \end{tabular} |
| 182 |
% \hfill |
| 183 |
% \begin{tabular}[t]{l} |
| 184 |
% \multicolumn{1}{c}{subfile} \\ |
| 185 |
% \hline |
| 186 |
% |\documentclass[|\meta{main\_file\_name}|]{subfiles}| \\ |
| 187 |
% |\begin{document}| \\ |
| 188 |
% \dots \\ |
| 189 |
% |\ifSubfilesClassLoaded{%|\\ |
| 190 |
% \quad|\bibliography{bibfile}%|\\ |
| 191 |
% |}{}|\\ |
| 192 |
% |\end{document}| \\ |
| 193 |
% \hline |
| 194 |
% \end{tabular} |
| 195 |
% \end{center} |
| 196 |
% |
| 197 |
% \subsection{Unusual locations for placing definitions and text} |
| 198 |
% |
| 199 |
% Starting with version 2.0, the |subfiles| package treats sub-preambles and text after |\end{document}| as one would expect: |
| 200 |
% The preamble of subfiles is skipped when loaded with |\subfile|, and everything after |\end{document}| is ignored. |
| 201 |
% In most cases this is what you want. |
| 202 |
% |
| 203 |
% \DescribeMacro{[v1]} |
| 204 |
% For reasons of compatibility, the option |v1| restores the behaviour of previous versions. |
| 205 |
% \begin{center} |
| 206 |
% |\usepackage[v1]{subfiles}| |
| 207 |
% \end{center} |
| 208 |
% This will have three effects. |
| 209 |
% |
| 210 |
% \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. |
| 211 |
% Here, one can add commands that are to be processed as part of the preamble when the subfiles are typeset on their one. |
| 212 |
% But this also means that any syntax error after |\end{document}| will ruin the \LaTeX ing of the subfile(s). |
| 213 |
% |
| 214 |
% \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. |
| 215 |
% 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. |
| 216 |
% One should also keep in mind that each subfile is input within a group, so definitions made here may not work outside. |
| 217 |
% |
| 218 |
% \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. |
| 219 |
% 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. |
| 220 |
% 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. |
| 221 |
% |
| 222 |
% \section{Use cases} |
| 223 |
% |
| 224 |
% \subsection{Hierarchy of directories} |
| 225 |
% |
| 226 |
% Sometimes it is desirable to put a subfile together with its images and supplementary files into its own directory. |
| 227 |
% 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. |
| 228 |
% As of version 1.3, the |subfiles| package handles this problem by using the |import| package. |
| 229 |
% |
| 230 |
% As an example, consider the following hierarchy of files: |
| 231 |
% \begin{center}\ttfamily |
| 232 |
% \begin{tabular}{l} |
| 233 |
% main.tex\\ |
| 234 |
% mypreamble.tex\\ |
| 235 |
% dir1/subfile1.tex\\ |
| 236 |
% dir1/image1.jpg\\ |
| 237 |
% dir1/text1.tex\\ |
| 238 |
% dir1/dir2/subfile2.tex\\ |
| 239 |
% dir1/dir2/image2.jpg\\ |
| 240 |
% dir1/dir2/text2.tex |
| 241 |
% \end{tabular} |
| 242 |
% \end{center} |
| 243 |
% where |main|, |subfile1|, and |subfile2| have the following contents: |
| 244 |
% \begin{center} |
| 245 |
% \begin{tabular}[t]{l} |
| 246 |
% \multicolumn{1}{c}{|main.tex|} \\ |
| 247 |
% \hline |
| 248 |
% |\documentclass{article}| \\ |
| 249 |
% |\input{mypreamble}| \\ |
| 250 |
% |\usepackage{graphicx}| \\ |
| 251 |
% |\usepackage{subfiles}| \\ |
| 252 |
% |\begin{document}| \\ |
| 253 |
% |\subfile{dir1/subfile1}|\\ |
| 254 |
% |\end{document}|\\ |
| 255 |
% \hline |
| 256 |
% \end{tabular}% |
| 257 |
% \hfill |
| 258 |
% \begin{tabular}[t]{l} |
| 259 |
% \multicolumn{1}{c}{|subfile1.tex|} \\ |
| 260 |
% \hline |
| 261 |
% |\documentclass[../main]{subfiles}| \\ |
| 262 |
% |\begin{document}| \\ |
| 263 |
% |\input{text1}|\\ |
| 264 |
% |\includegraphics{image1.jpg}|\\ |
| 265 |
% |\subfile{dir2/subfile2}| \\ |
| 266 |
% |\end{document}| \\ |
| 267 |
% \hline |
| 268 |
% \end{tabular}\\[2ex] |
| 269 |
% \begin{tabular}[t]{l} |
| 270 |
% \multicolumn{1}{c}{|subfile2.tex|} \\ |
| 271 |
% \hline |
| 272 |
% |\documentclass[../../main]{subfiles}| \\ |
| 273 |
% |\begin{document}| \\ |
| 274 |
% |\input{text2}|\\ |
| 275 |
% |\includegraphics{image2.jpg}|\\ |
| 276 |
% |\end{document}| \\ |
| 277 |
% \hline |
| 278 |
% \end{tabular} |
| 279 |
% \end{center} |
| 280 |
% 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. |
| 281 |
% |
| 282 |
% \subsection{Cross-referencing between subfiles} |
| 283 |
% 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: |
| 284 |
% \begin{center} |
| 285 |
% \begin{tabular}{l} |
| 286 |
% |\usepackage{xr}| \\ |
| 287 |
% |\usepackage{subfiles}| \\ |
| 288 |
% |\externaldocument[M-]{\subfix{main}}| |
| 289 |
% \end{tabular} |
| 290 |
% \end{center} |
| 291 |
% In the |\externaldocument| command, |main| is the name of the main document. |
| 292 |
% Moreover, |M-| is an arbitrary sequence of characters that is added as prefix to the labels. |
| 293 |
% 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. |
| 294 |
% |
| 295 |
% To cross-reference between documents, add labels as usual. |
| 296 |
% Suppose you have |\label{mylabel}| in any of the files. |
| 297 |
% 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. |
| 298 |
% |
| 299 |
% Note that you first have to compile |main.tex|. |
| 300 |
% This generates |main.aux|, which then can be loaded by the subfiles to provide the information for the labels prefixed with |M-|. |
| 301 |
% |
| 302 |
% \subsection{Avoiding extra spaces} |
| 303 |
% |
| 304 |
% Sometimes you may want to load the contents of a subfile without white space separating it from the contents of the main file. |
| 305 |
% In this respect, |\subfile| behaves similar to |\input|. |
| 306 |
% 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}|. |
| 307 |
% Therefore, to load the contents of a subfile without intervening spaces, you have either to add comment signs: |
| 308 |
% \begin{center} |
| 309 |
% \begin{tabular}[t]{l} |
| 310 |
% \multicolumn{1}{c}{|main.tex|}\\ |
| 311 |
% \hline |
| 312 |
% \dots\\ |
| 313 |
% |text before%|\\ |
| 314 |
% |\subfile{sub.tex}%|\\ |
| 315 |
% |text after|\\ |
| 316 |
% \hline |
| 317 |
% \end{tabular} |
| 318 |
% \qquad |
| 319 |
% \begin{tabular}[t]{l} |
| 320 |
% \multicolumn{1}{c}{|sub.tex|}\\ |
| 321 |
% \hline |
| 322 |
% |\documentclass[main.tex]{subfiles}|\\ |
| 323 |
% |\begin{document}|\\ |
| 324 |
% |contents of subfile%|\\ |
| 325 |
% |\end{document}|\\ |
| 326 |
% \hline |
| 327 |
% \end{tabular} |
| 328 |
% \end{center} |
| 329 |
% or to put everything on the same line: |
| 330 |
% \begin{center} |
| 331 |
% |text before\subfile{sub.tex}text after|\\ |
| 332 |
% |contents of subfile\end{document}| |
| 333 |
% \end{center} |
| 334 |
% |
| 335 |
% \section{Troubleshooting} |
| 336 |
% |
| 337 |
% Here are some hints that solve most problems. |
| 338 |
% |
| 339 |
% \begin{enumerate} |
| 340 |
% \item |
| 341 |
% 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}}. |
| 342 |
% \item |
| 343 |
% Make sure that |\usepackage{subfiles}| appears near the end of the main preamble. If you need the package |standalone|, then it has to be loaded before |subfiles|. |
| 344 |
% \item |
| 345 |
% 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}|}|. |
| 346 |
% \item |
| 347 |
% 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}}. |
| 348 |
% \end{enumerate} |
| 349 |
% |
| 350 |
% |
| 351 |
% \section{Dependencies} |
| 352 |
% |
| 353 |
% The |import| package by Donald Arsenau is needed to load subfiles from different directories. |
| 354 |
% When the |standalone| package is used, the |subfiles| package requires the |xpatch| package by Enrico Gregorio to patch the command |\includestandalone|. |
| 355 |
% Both packages are part of the standard \TeX\ distributions. |
| 356 |
% |
| 357 |
% |
| 358 |
% \section{Version history} |
| 359 |
% |
| 360 |
% \begin{enumerate} |
| 361 |
% \item[v1.1:] |
| 362 |
% Initial version by Federico Garcia. (Subsequent versions by Gernot Salzer.) |
| 363 |
% \item[v1.2:] |
| 364 |
% \begin{itemize} |
| 365 |
% \item Incompatibility with classes and packages removed that modify the |\document| command, like the class |revtex4|. |
| 366 |
% \end{itemize} |
| 367 |
% \item[v1.3:] |
| 368 |
% \begin{itemize} |
| 369 |
% \item Use of |import| package to handle directory hierarchies. |
| 370 |
% \item |\ignorespaces| added to avoid spurious spaces. |
| 371 |
% \item Incompatibility with commands removed that expect |\document| to be equal to |\@onlypreamble| after the preamble. Thanks to Eric Domenjoud for analysing the problem. |
| 372 |
% \end{itemize} |
| 373 |
% \item[v1.4:] |
| 374 |
% \begin{itemize} |
| 375 |
% \item Incompatibility with |memoir| class and |comment| package removed. |
| 376 |
% \item Bug `|\unskip| cannot be used in vertical mode` fixed. |
| 377 |
% \end{itemize} |
| 378 |
% \item[v1.5:] |
| 379 |
% \begin{itemize} |
| 380 |
% \item Command |\subfileinclude| added. |
| 381 |
% \item Basic support for |bibtex| related bibliographies in subfiles added. |
| 382 |
% Seems to suffice also for sub-bibliographies with the package |chapterbib|. |
| 383 |
% \item Support for sub-bibliographies with package |bibunits| added. |
| 384 |
% \end{itemize} |
| 385 |
% \item[v1.6:] |
| 386 |
% \begin{itemize} |
| 387 |
% \item Support for sub-bibliographies with package |bibunits| dropped, in favor of |\subfix|. |
| 388 |
% \item Command |\subfix| added. |
| 389 |
% \item Incompatibility with |standalone| class removed. |
| 390 |
% \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. |
| 391 |
% \end{itemize} |
| 392 |
% \item[v2.0:] |
| 393 |
% \begin{itemize} |
| 394 |
% \item Incompatibility with \LaTeX\ Oct.~2020 removed. |
| 395 |
% Thanks to Ulrike \mbox{Fischer} from the \LaTeX3 team for the timely warning. |
| 396 |
% \item By default, text after |\end{document}| as well as the preamble of subfiles, when loaded with |\subfile|, are ignored now. |
| 397 |
% The old behaviour is available via the new package option |v1|. |
| 398 |
% \item Command |\ifSubfilesClassLoaded| added and documentation regarding the use of the |\bibliography| command corrected. |
| 399 |
% Thanks to Github user |alan-isaac| for reporting the issue. |
| 400 |
% \item Subfiles now can have the same name as the main file. |
| 401 |
% Thanks to Github user |June-6th| for reporting the issue. |
| 402 |
% \item Problem with the search path for images resolved. |
| 403 |
% Thanks to Github user |maxnick| for reporting the issue. |
| 404 |
% \end{itemize} |
| 405 |
% \item[v2.1] |
| 406 |
% \begin{itemize} |
| 407 |
% \item Bugfix: In some situations, the hooks of |\begin{document}| and |\end{document}| were triggered when loading a subfile. |
| 408 |
% This occurred in particular with packages for handling CJK languages. |
| 409 |
% Thanks to Github user |yuishin-kikuchi| for reporting the issue. |
| 410 |
% \item Section about cross-referencing added. |
| 411 |
% Thanks to Github user |ndvanforeest| for the input. |
| 412 |
% \end{itemize} |
| 413 |
% \item[v2.2] |
| 414 |
% \begin{itemize} |
| 415 |
% \item Bugfix: The bugfix of v2.1 introduced an incompatibility with |tabular| environments in subfiles. Thanks to |yuishin-kikuchi| and |nvmnghia| on Github for reporting the issue. Kudos to |Phelype Oleinik| on tex.stackexchange.com for explaining the intricacies of the |tabular| environment and its interaction with the |\end| command. |
| 416 |
% \end{itemize} |
| 417 |
% \end{enumerate} |
| 418 |
% |
| 419 |
%\section{The Implementation} |
| 420 |
%\subsection{The class} |
| 421 |
% |
| 422 |
% \iffalse |
| 423 |
%<*class> |
| 424 |
% \fi |
| 425 |
% |
| 426 |
% \begin{macrocode} |
| 427 |
\NeedsTeXFormat{LaTeX2e} |
| 428 |
\ProvidesClass{subfiles}[2020/11/14 v2.2 Multi-file projects (class)] |
| 429 |
\DeclareOption*{% |
| 430 |
\typeout{Preamble taken from file `\CurrentOption'}% |
| 431 |
\let\preamble@file\CurrentOption |
| 432 |
} |
| 433 |
\ProcessOptions |
| 434 |
% \end{macrocode} |
| 435 |
% |
| 436 |
% After processing the option of the |subfiles| class, we reset |\@classoptionslist| such that the options in the main file will be processed. |
| 437 |
% |
| 438 |
% \begin{macrocode} |
| 439 |
\let\@classoptionslist\relax |
| 440 |
% \end{macrocode} |
| 441 |
% |
| 442 |
% To handle subfiles in separate directories, we use the |import| package. |
| 443 |
% We load it now, since it resets the macro |\import@path|. |
| 444 |
% \begin{macrocode} |
| 445 |
\RequirePackage{import} |
| 446 |
% \end{macrocode} |
| 447 |
% |
| 448 |
% We redefine |\documentclass| to load the class of the main document. |
| 449 |
% |
| 450 |
% \begin{macrocode} |
| 451 |
\let\subfiles@documentclass\documentclass |
| 452 |
\def\documentclass{% |
| 453 |
\let\documentclass\subfiles@documentclass |
| 454 |
\LoadClass |
| 455 |
} |
| 456 |
% \end{macrocode} |
| 457 |
% |
| 458 |
% 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. |
| 459 |
% Therefore we use |\input| and initialize |\import@path| and |\input@path| to the path of the main file. |
| 460 |
% We use the internal \LaTeX\ macro |\filename@parse| to obtain this path. |
| 461 |
% |
| 462 |
% \begin{macrocode} |
| 463 |
\filename@parse{\preamble@file} |
| 464 |
\edef\import@path{\filename@area} |
| 465 |
\edef\input@path{{\filename@area}} |
| 466 |
\input{\preamble@file} |
| 467 |
% \end{macrocode} |
| 468 |
% |
| 469 |
% After loading the preamble of the main file, we reset |\import@path|. |
| 470 |
% Since the preamble may have changed the catcode of the |@| sign, we make it (again) a letter. Better safe than sorry. |
| 471 |
% |
| 472 |
% \begin{macrocode} |
| 473 |
{\makeatletter |
| 474 |
\gdef\import@path{} |
| 475 |
} |
| 476 |
% \end{macrocode} |
| 477 |
% |
| 478 |
% \iffalse |
| 479 |
%</class> |
| 480 |
% \fi |
| 481 |
% |
| 482 |
% |
| 483 |
% \subsection{The package} |
| 484 |
% |
| 485 |
% \iffalse |
| 486 |
%<*package> |
| 487 |
% \fi |
| 488 |
% |
| 489 |
% \begin{macrocode} |
| 490 |
\NeedsTeXFormat{LaTeX2e} |
| 491 |
\ProvidesPackage{subfiles}[2020/11/14 v2.2 Multi-file projects (package)] |
| 492 |
% \end{macrocode} |
| 493 |
% \subsubsection*{Auxiliary commands} |
| 494 |
% |
| 495 |
% When redefining the |\end| command, we have to have to keep it expandable to a certain depth. |
| 496 |
% Therefore we need an expandable way to compare strings. |
| 497 |
% We borrow the function from LaTeX's |expl3| part. |
| 498 |
% |
| 499 |
% \begin{macrocode} |
| 500 |
\ExplSyntaxOn |
| 501 |
\cs_new_eq:NN \subfiles@StrIfEqTF \str_if_eq:nnTF |
| 502 |
\ExplSyntaxOff |
| 503 |
% \end{macrocode} |
| 504 |
% |
| 505 |
% The macro |\subfiles@renewBeginDocument{|\meta{code}|}| redefines |\begin| such that the next |\begin{document}| executes \meta{code} (and then restores the original definition of |\begin|). |
| 506 |
% |
| 507 |
% \begin{macrocode} |
| 508 |
\def\subfiles@renewBeginDocument#1{% |
| 509 |
\let\subfiles@begin\begin |
| 510 |
\def\begin##1{% |
| 511 |
\subfiles@StrIfEqTF{##1}{document}{% |
| 512 |
\let\begin\subfiles@begin |
| 513 |
#1% |
| 514 |
}{% |
| 515 |
\csname subfiles@begin\endcsname{##1}% |
| 516 |
}% |
| 517 |
}% |
| 518 |
} |
| 519 |
% \end{macrocode} |
| 520 |
% |
| 521 |
% The macro |\subfiles@renewEndDocument{|\meta{code}|}| redefines |\end| such that the next |\end{document}| executes \meta{code} (and then restores the original definition of |\end|). |
| 522 |
% This macro is more complex then the previous one, as we have to ensure that |
| 523 |
% |\expandafter\expandafter\expandafter\relax\end{env}| expands to |\relax\endenv|. |
| 524 |
% This is necessary for |tabular|s to work correctly. |
| 525 |
% |
| 526 |
% \begin{macrocode} |
| 527 |
\def\subfiles@saveEndTo#1{\expandafter\let\expandafter#1\csname end \endcsname} |
| 528 |
\def\subfiles@restoreEndFrom{\expandafter\let\csname end \endcsname} |
| 529 |
\def\subfiles@renewEndDocument#1{% |
| 530 |
\ifcsname subfiles@end\endcsname |
| 531 |
\else |
| 532 |
\subfiles@saveEndTo\subfiles@end |
| 533 |
\fi |
| 534 |
\expandafter\def\csname end \endcsname##1{% |
| 535 |
\romannumeral |
| 536 |
\subfiles@StrIfEqTF{##1}{document}{% |
| 537 |
\z@ |
| 538 |
\subfiles@restoreEndFrom\subfiles@end |
| 539 |
#1% |
| 540 |
}{% |
| 541 |
\expandafter\expandafter\expandafter\z@\subfiles@end{##1}% |
| 542 |
}% |
| 543 |
}% |
| 544 |
} |
| 545 |
% \end{macrocode} |
| 546 |
% |
| 547 |
% \subsubsection*{Handling the main document} |
| 548 |
% |
| 549 |
% When the main document is loaded from a subfile, the preamble is read, but the document itself is skipped. |
| 550 |
% The end of the preamble is marked by |\begin{document}|. |
| 551 |
% In version 1.x of the |subfiles| package, everything up to (and including) |\end{document}| is skipped, but the lines following it are treated again as part of the preamble. |
| 552 |
% The macro |\subfiles@handleMainDocumentVi| redefines |\begin{document}| accordingly. |
| 553 |
% |
| 554 |
% \begin{macrocode} |
| 555 |
\def\subfiles@handleMainDocumentVi{% |
| 556 |
\long\def\subfiles@skipToEndDocument##1\end##2{% |
| 557 |
\subfiles@StrIfEqTF{##2}{document}{\ignorespaces}{\subfiles@skipToEndDocument}% |
| 558 |
}% |
| 559 |
\subfiles@renewBeginDocument{% |
| 560 |
\subfiles@skipToEndDocument |
| 561 |
}% |
| 562 |
} |
| 563 |
% \end{macrocode} |
| 564 |
% |
| 565 |
% In version 2.x of the |subfiles| package, everything following |\begin{document}| is ignored. |
| 566 |
% The macro |\subfiles@handleMainDocumentVii| redefines this command accordingly. |
| 567 |
% |
| 568 |
% \begin{macrocode} |
| 569 |
\def\subfiles@handleMainDocumentVii{% |
| 570 |
\subfiles@renewBeginDocument{% |
| 571 |
\endinput |
| 572 |
\ignorespaces |
| 573 |
}% |
| 574 |
} |
| 575 |
% \end{macrocode} |
| 576 |
% |
| 577 |
% \subsubsection*{Handling subfiles} |
| 578 |
% |
| 579 |
% When a subfile is loaded, the preamble and |\end{document}| have to be ignored. |
| 580 |
% More precisely, in older versions (v1.x), the following happens: |
| 581 |
% \begin{itemize} |
| 582 |
% \item |\documentclass[...]{subfiles}| is ignored. |
| 583 |
% \item The contents of the preamble becomes part of the text. |
| 584 |
% \item |\begin{document}| is ignored. |
| 585 |
% \item |\end{document}| is ignored. |
| 586 |
% \item Any lines after |\end{document}| are also part of the text. |
| 587 |
% \end{itemize} |
| 588 |
% |
| 589 |
% \begin{macrocode} |
| 590 |
\def\subfiles@handleSubDocumentVi{% |
| 591 |
\let\subfiles@documentclass\documentclass |
| 592 |
\def\documentclass{% |
| 593 |
\@ifnextchar[\subfiles@documentclass@{\subfiles@documentclass@[]}% |
| 594 |
}% |
| 595 |
\def\subfiles@documentclass@[##1]##2{% |
| 596 |
\let\documentclass\subfiles@documentclass |
| 597 |
\ignorespaces |
| 598 |
}% |
| 599 |
\subfiles@renewBeginDocument{% |
| 600 |
\subfiles@renewEndDocument\ignorespaces |
| 601 |
\ignorespaces |
| 602 |
}% |
| 603 |
} |
| 604 |
% \end{macrocode} |
| 605 |
% In newer versions (v2.x), the following happens: |
| 606 |
% \begin{itemize} |
| 607 |
% \item The preamble, up to |\begin{document}|, is ignored. |
| 608 |
% \item |\end{document}| as well as any lines after it are ignored. |
| 609 |
% \end{itemize} |
| 610 |
% |
| 611 |
% \begin{macrocode} |
| 612 |
\def\subfiles@handleSubDocumentVii{% |
| 613 |
\let\subfiles@documentclass\documentclass |
| 614 |
\long\def\documentclass##1\begin##2{% |
| 615 |
\subfiles@StrIfEqTF{##2}{document}{% |
| 616 |
\let\documentclass\subfiles@documentclass |
| 617 |
\subfiles@renewEndDocument{% |
| 618 |
\endinput |
| 619 |
\ignorespaces |
| 620 |
}% |
| 621 |
\ignorespaces |
| 622 |
}{\documentclass} |
| 623 |
}% |
| 624 |
} |
| 625 |
% \end{macrocode} |
| 626 |
% |
| 627 |
% \subsubsection*{Processing the package options} |
| 628 |
% |
| 629 |
% 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. |
| 630 |
% We initialize the macros for handling main and sub documents with the behavior of version 2.x. |
| 631 |
% \begin{macrocode} |
| 632 |
\let\subfiles@handleMainDocument\subfiles@handleMainDocumentVii |
| 633 |
\let\subfiles@handleSubDocument\subfiles@handleSubDocumentVii |
| 634 |
% \end{macrocode} |
| 635 |
% When option |v1| is present, the macros are set to the behavior of version 1.x. |
| 636 |
% \begin{macrocode} |
| 637 |
\DeclareOption{v1}{% |
| 638 |
\let\subfiles@handleMainDocument\subfiles@handleMainDocumentVi |
| 639 |
\let\subfiles@handleSubDocument\subfiles@handleSubDocumentVi |
| 640 |
} |
| 641 |
\DeclareOption*{\PackageWarning{subfiles}{Option '\CurrentOption' ignored}} |
| 642 |
\ProcessOptions\relax |
| 643 |
% \end{macrocode} |
| 644 |
% |
| 645 |
% \subsubsection*{Loading subfiles} |
| 646 |
% |
| 647 |
% To handle subfiles in separate directories, we use the |import| package. |
| 648 |
% If it has already been loaded, e.g.\ by the |subfiles| class, this line does nothing. |
| 649 |
% \begin{macrocode} |
| 650 |
\RequirePackage{import} |
| 651 |
% \end{macrocode} |
| 652 |
% |
| 653 |
% The |\subimport| command requires path and filename as separate arguments, so we have to split qualified filenames into these two components. |
| 654 |
% The internal \LaTeX\ command |\filename@parse| almost fits the bill, except that it additionally splits the filename into basename and extension. |
| 655 |
% Unfortunately, concatenating basename and extension to recover the filename is not clean: |
| 656 |
% Under Unix/Linux, the filenames |base| and |base.| denote different entities, but after |\filename@parse| both have the same basename and an empty extension. |
| 657 |
% Therefore we redefine the command |\filename@simple| temporarily; it is responsible for this unwanted split. |
| 658 |
% |
| 659 |
% \begin{macrocode} |
| 660 |
\def\subfiles@split#1{% |
| 661 |
\let\subfiles@filename@simple\filename@simple |
| 662 |
\def\filename@simple##1.\\{\edef\filename@base{##1}}% |
| 663 |
\filename@parse{#1}% |
| 664 |
\let\filename@simple\subfiles@filename@simple |
| 665 |
}% |
| 666 |
% \end{macrocode} |
| 667 |
% |
| 668 |
% E.g., after executing |\subfiles@split{../dir1/dir2/file.tex}| the macros |\filename@area| and |\filename@base| expand to |../dir1/dir2/| and |file.tex|, respectively. |
| 669 |
% |
| 670 |
% \DescribeMacro{\subfile} |
| 671 |
% The command |\subfile| specifies the command |\subimport| for |\input|ing the subfile, and then calls |\subfiles@subfile|. |
| 672 |
% |
| 673 |
% \begin{macrocode} |
| 674 |
\newcommand\subfile{% |
| 675 |
\let\subfiles@loadfile\subimport |
| 676 |
\subfiles@subfile |
| 677 |
} |
| 678 |
% \end{macrocode} |
| 679 |
% |
| 680 |
% \DescribeMacro{\subfileinclude} |
| 681 |
% The command |\subfileinclude| specifies the command |\subincludefrom| for |\include|ing the subfile, and then calls |\subfiles@subfile|. |
| 682 |
% |
| 683 |
% \begin{macrocode} |
| 684 |
\newcommand\subfileinclude{% |
| 685 |
\let\subfiles@loadfile\subincludefrom |
| 686 |
\subfiles@subfile |
| 687 |
} |
| 688 |
% \end{macrocode} |
| 689 |
% |
| 690 |
% The main functionality is implemented in |\subfiles@subfile|. |
| 691 |
% It sets up the handling of the sub-preamble, splits the filename and loads the subfile. |
| 692 |
% |
| 693 |
% \begin{macrocode} |
| 694 |
\def\subfiles@subfile#1{% |
| 695 |
\begingroup |
| 696 |
\subfiles@handleSubDocument |
| 697 |
\subfiles@split{#1}% |
| 698 |
\subfiles@loadfile{\filename@area}{\filename@base}% |
| 699 |
\endgroup |
| 700 |
} |
| 701 |
% \end{macrocode} |
| 702 |
% |
| 703 |
% \subsubsection*{Fixing incompatibilities} |
| 704 |
% |
| 705 |
% \DescribeMacro{\subfix} |
| 706 |
% If some package provides a command that takes a filename as argument, then it has to be prefixed with the current |\import@path|. |
| 707 |
% This is what the |\subfix| command tries to do. |
| 708 |
% In order to succeed, the filename has to be expanded immediately, such that the current value of |\import@path| is used. |
| 709 |
% |
| 710 |
% \begin{macrocode} |
| 711 |
\def\subfix#1{\import@path#1} |
| 712 |
% \end{macrocode} |
| 713 |
% |
| 714 |
% 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. |
| 715 |
% |
| 716 |
% \begin{macrocode} |
| 717 |
\def\subfiles@fixfilelist#1{% |
| 718 |
\def\subfiles@list{}% |
| 719 |
\def\subfiles@sep{}% |
| 720 |
\@for\subfiles@tmp:=#1\do{% |
| 721 |
\edef\subfiles@list{\subfiles@list\subfiles@sep\subfix{\subfiles@tmp}}% |
| 722 |
\def\subfiles@sep{,}% |
| 723 |
}% |
| 724 |
} |
| 725 |
\def\subfiles@fixpathlist#1{% |
| 726 |
\def\subfiles@list{}% |
| 727 |
\@tfor\subfiles@tmp:=#1\do{% |
| 728 |
\edef\subfiles@list{\subfiles@list{\subfix\subfiles@tmp}}% |
| 729 |
}% |
| 730 |
} |
| 731 |
% \end{macrocode} |
| 732 |
% |
| 733 |
% \DescribeMacro{\bibliography} |
| 734 |
% \DescribeMacro{\graphicspath} |
| 735 |
% We patch |\bibliography| and |\graphicspath| (from the |graphics|/|graphicx| package) such that users don't have to worry about adding |\subfix|. |
| 736 |
% |
| 737 |
% \begin{macrocode} |
| 738 |
\let\subfiles@bibliography\bibliography |
| 739 |
\def\bibliography#1{% |
| 740 |
\subfiles@fixfilelist{#1}% |
| 741 |
\expandafter\subfiles@bibliography\expandafter{\subfiles@list}% |
| 742 |
} |
| 743 |
\@ifpackageloaded{graphics}{% |
| 744 |
\let\subfiles@graphicspath\graphicspath |
| 745 |
\def\graphicspath#1{% |
| 746 |
\subfiles@fixpathlist{#1}% |
| 747 |
\edef\subfiles@list{{\subfix{}}\subfiles@list}% |
| 748 |
\expandafter\subfiles@graphicspath\expandafter{\subfiles@list}% |
| 749 |
}% |
| 750 |
}{} |
| 751 |
% \end{macrocode} |
| 752 |
% |
| 753 |
% \DescribeMacro{\includestandalone} |
| 754 |
% The command |\includestandalone| handles subfiles in its own way. |
| 755 |
% Therefore we modify it to use the original definition of |\end|. |
| 756 |
% |
| 757 |
% \begin{macrocode} |
| 758 |
\@ifpackageloaded{standalone}{ |
| 759 |
\RequirePackage{xpatch} |
| 760 |
\xpretocmd\includestandalone{% |
| 761 |
\subfiles@saveEndTo\subfiles@end@ |
| 762 |
\ifcsname subfiles@end\endcsname |
| 763 |
\subfiles@restoreEndFrom\subfiles@end |
| 764 |
\fi |
| 765 |
}{}{} |
| 766 |
\xapptocmd\includestandalone{\subfiles@restoreEndFrom\subfiles@end@}{}{} |
| 767 |
}{} |
| 768 |
% \end{macrocode} |
| 769 |
% |
| 770 |
% \subsubsection*{Do we typeset the main file or a subfile?} |
| 771 |
% |
| 772 |
% \DescribeMacro{\ifSubfilesClassLoaded} |
| 773 |
% To add code or text conditionally, depending on whether the main document or a subfile is typeset, we provide the command |\ifSubfilesClassLoaded|. |
| 774 |
% |
| 775 |
% \begin{macrocode} |
| 776 |
\newcommand\ifSubfilesClassLoaded{% |
| 777 |
\expandafter\ifx\csname [email protected]\endcsname\relax |
| 778 |
\expandafter\@secondoftwo |
| 779 |
\else |
| 780 |
\expandafter\@firstoftwo |
| 781 |
\fi |
| 782 |
} |
| 783 |
% \end{macrocode} |
| 784 |
% |
| 785 |
% |
| 786 |
% \subsubsection*{The end is near} |
| 787 |
% |
| 788 |
% The |subfiles| package is loaded near the end of the main preamble. |
| 789 |
% If it is loaded from a subfile, i.e., if |subfiles.cls| has been loaded, then we prepare for skipping the main document. |
| 790 |
% \begin{macrocode} |
| 791 |
\ifSubfilesClassLoaded{% |
| 792 |
\subfiles@handleMainDocument |
| 793 |
}{} |
| 794 |
% \end{macrocode} |
| 795 |
% |
| 796 |
% \iffalse |
| 797 |
%</package> |
| 798 |
% \fi |