summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/subfiles/subfiles.dtx
blob: dacece04617e6cd7b0176e849ad37963bce8ea10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
% \iffalse meta-comment
%
% subfiles - class and package for multi-file projects in LaTeX
% Copyright 2002, 2012 Federico Garcia (feg8@pitt.edu, fedegarcia@hotmail.com)
% 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
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Gernot Salzer.
%
% This work consists of the files subfiles.dtx and subfiles.ins
% and the derived files subfiles.sty and subfiles.pdf
%
% -------------------------------------------
%
%<*driver>
% \fi
\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-File Projects}
\date{\filedate{}}
 \author{Federico Garcia, Gernot Salzer}

\begin{document}
\maketitle
 \DocInput{\filename}
\end{document}
%</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.
%   This is achieved by reusing the preamble of the main file also for the subfiles.
% \end{abstract}
% \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.%
% \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 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 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 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}[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}
% Now there are two possibilities.
% \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.
% \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.
% 
% \subsection{Additional definitions for subfiles}
%
% 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.
%
% \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).
%
% \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}
%
% \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.
%
% \section{Version history}
%
% \begin{description}
% \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}
%\subsection{The class}
%    \begin{macrocode}
%<*class>
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{subfiles}[2019/09/28 v1.3 Federico Garcia, Gernot Salzer]
\DeclareOption*{\typeout{Preamble taken from file `\CurrentOption'}%
    \let\preamble@file\CurrentOption}
\ProcessOptions  
%    \end{macrocode}
%
% We start by saving the regular \LaTeX\ definition of |\documentclass|:
% 
%    \begin{macrocode}
\let\subfiles@documentclass\documentclass
%    \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.
%
%    \begin{macrocode}
\let\documentclass\LoadClass\relax
%    \end{macrocode}
%
% 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}
{\catcode`\@=11
\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.
%
%
% \subsection{The package}
%
% Any option will be ignored.
%
%    \begin{macrocode}
%<*package>
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{subfiles}[2019/09/28 v1.3 Federico Garcia, Gernot Salzer]
\DeclareOption*{\PackageWarning{\CurrentOption ignored}}
\ProcessOptions
\RequirePackage{verbatim}
%    \end{macrocode}
%
% To handle subfiles in separate directories, we load the |import| package.
% 
%    \begin{macrocode}
\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}
% 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
  \renewcommand\documentclass[2][subfiles]{\ignorespaces}%
  \renewenvironment{document}{\let\document\@onlypreamble\ignorespaces}{}%
%    \end{macrocode}
%
% 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\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 |\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|.