summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/subdocs/subdocs.sty
blob: 9cb64cbbd07ab720618e65fd6bb1d8ac753b0342 (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
%
% Copyright (C) 2009-2019 Javier Bezos http://www.texnia.com
%
% This file may be distributed and/or modified under the conditions of
% the MIT License. A version can be found at the end of this file.
%
%
% Usage
% ~~~~~
%
% This is a tentative package aiming to provide a different approach
% for multidocument works (mainly, books with a document per chapter).
% Unlike the \include mechanism, every subdocument is a complete LaTeX
% document and can be typeset separately.  What the package does is
% sharing the aux files.  It's still an alpha version, and no attempt
% has been done yet to make it to work with, say, hyperref.
%
% This approach has the following advantages:
% 1) Packages can be loaded just when a part requires them.
% 2) Generated pdf/dvi/ps files are kept.
%
% You need a driver tex file containing a \subdocuments command
% listing the files. With an optional argument you can set
% its behaviour:
% 
% \subdocuments[clear=double]{file1, file2, file3}
%
% The option clear=double adds an empty page at the end to give a even
% number of pages.  clear=single doesn't and it's the default.  Spaces
% and new lines before and after commas are ignored, as well as empty
% items.  It relies on the keyval package, so names containing = must
% be given within braces.
%
% This file can be used as a shared preample, with common settings
% for all the documents.
%
% Documents must have the folowing line:
%
% \usepackage[master]{subdocs}
%
% where master is the name of the file described above.
%
% Since every subdocument reads all the aux files, \tableofcontents
% may be used where desired. 
%
% Code
% ~~~~

\ProvidesPackage{subdocs}[2009/06/12 v0.1 Subdocuments]

\RequirePackage{keyval}

% Los dos siguientes son lo encargados de hacer lo que
% normalmente hace \include.

\AtBeginDocument{%
  \if@filesw
    \immediate\write\@auxout{\noexpand\sdoc@readaux}%
  \fi}

% At end of document we write the counters using writeckpt
% but with \@partaux redirected to \@auxout. Like \include
% there is a \clearpage

\AtEndDocument{%
  \sdoc@clear
  \let\@partaux\@auxout
  \@writeckpt{\jobname}}

\def\@wckptelt#1{%
  \immediate\write\@partaux{%
    \string\sdoc@setcounter{#1}{\the\@nameuse{c@#1}}}}

\def\sdoc@setcounter#1{%
  \@ifundefined{c@#1}%
    {\@gobble}%
    {\setcounter{#1}}}

% Optional arguments

\def\sdoc@opt@clearsingle{\let\sdoc@clear\clearpage}
\def\sdoc@opt@cleardouble{\let\sdoc@clear\cleardoublepage}
\sdoc@opt@clearsingle

\define@key{sdoc@options}{clear}{\@nameuse{sdoc@opt@clear#1}}

% 

\let\sdoc@loadaux\@empty

\newcommand\subdocuments[2][]{%
  \let\sdoc@save@kverrx\KV@errx
  \def\KV@errx##1{% \@tempa contains the file name
    {\let\sdoc@elt\relax % local
     \edef\sdoc@a{\expandafter\strip@prefix\meaning\@tempa}%
     \protected@xdef\sdoc@loadaux{\sdoc@loadaux\sdoc@elt{\sdoc@a}}}}%
  \setkeys{sdoc@files}{#2}%
  \let\KV@errx\sdoc@save@kverrx
  \setkeys{sdoc@options}{#1}}

% A "recursive" call. When a \jobname.aux file is
% read the first thing is (or should be) \sdoc@readaux.
% It reads all the aux files, and exits from the current
% aux without further reading because it is already read.
% This requires a file could \input itself, but, does
% that work in all distributions?

\def\sdoc@elt#1{%
  \InputIfFileExists{#1.aux}{}%
    {\PackageWarningNoLine{subdocs}{No file #1.aux -- Data may be wrong}}%
  \sdoc@setcp{#1}}%

\def\sdoc@readaux{%
  \global\let\sdoc@readaux\relax
  \sdoc@loadaux
  \global\let\sdoc@readaux\sdoc@readaux@x
  \endinput}

\let\sdoc@readaux@x\sdoc@readaux

\def\sdoc@setcp#1{%
  \def\sdoc@a{#1}\edef\sdoc@b{\jobname}%
  \ifx\sdoc@a\sdoc@b
    \PackageInfo{subdocs}{Ignoring counters from #1 on, }%
    \global\let\sdoc@setcp\@gobble
  \else
    \csname cp@#1\endcsname
  \fi}

% The master file name must be saved because it may contain
% \usepackage's, not allowed before \ProcessOptions.

\DeclareOption*{\edef\sdoc@a{\CurrentOption}}
\ProcessOptions
\InputIfFileExists{\sdoc@a}{}{}%

\endinput

MIT License
-----------

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.