summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/ltxtools/ltxtools-incluput.sty
blob: b7262a4979b9e027fea370b1aecf6b8d5f8a7c9b (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
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%
% This is file ltxtools-incluput.sty, version 0.0.1, December 2011.        %
%                                                                          %
% It can be used to input standalone subsidiary documents without          %
% also including their preamble and/or postamble. Files can be             %
% 'inputted' or 'included'.                                                %
%                                                                          %
% If you enter 'persistent=false' in the call to the package,              %
% you would have to put \let\documentclass\ltxtools-incluput at the top    %
% of every file to be included in the main document, otherwise enter it    %
% only in the first file to be input.                                      %
%                                                                          %
% Use the 'nested' option to input documents that themselves input         %
% other documents.                                                         %
%                                                                          %
% 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 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.       %
%                                                                          %
% Copyright (c) 2011 Ahmed Musa (a.musa@rocketmail.com).                   %
%                                                                          %
%++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%

\@ifpackageloaded{ltxtools}{%
  \@ifpackagelater{ltxtools}{2011/12/12}{}{%
    \@latex@error{Loaded version of ltxtools package is
      not current}\@ehc
  }%
}{%
  \RequirePackage{ltxtools}[2011/12/12]%
}
\@ifpackageloaded{ltxkeys}{%
  \@ifpackagelater{ltxkeys}{2011/12/12}{}{%
    \@latex@error{Loaded version of ltxkeys package is
      not current}\@ehc
  }%
}{%
  \RequirePackage{ltxkeys}[2011/12/12]%
}
\UseNormalCatcodes
\StyleFilePurpose{Input standalone documents (AM)}
\StyleFileRCSInfo
$Id: ltxtools-incluput.sty,v 0.0.1 2011/12/12 09:00:00 Ahmed Musa Exp $
\ProvidesPackage{ltxtools-incluput}[\StyleFileInfo]
\NeedsTeXFormat{LaTeX2e}[2011/06/27]
\SetStyleFileMessages[inclp@]{info}{warn}{err}
\ifx\documentclass\@twoclasseserror\else
  \inclp@err{Package 'ltxtools-incluput' can't be loaded
    \MsgBrk outside document preamble}\@ehc
\fi
\AfterStartOfDocument{%
  \ifoneloadedTF{%
    docmute,standalone,fileimport-filedate
  }{%
    \inclp@err{Package 'ltxtools-incluput' can't be used
      \MsgBrk with package '\ltspkgcls'}\@ehc
    \expandafter\endinput
  }%
  \ifinclp@useincluput
    \gletcs\documentclass\incluput
    \incluputtrue
  \else
    \incluputfalse
  \fi
}
\cptnewbools{incluput}
\def\inclp@gobblepreamble{%
  \begingroup
  \everyeof{\begin{EOF}\relax}%
  \inclp@g@bblepreamble
}
\long\def\inclp@g@bblepreamble#1\begin#2{%
  \ifstrcmpTF{#2}{document}{%
    \endgroup
    \ifinclp@persistent\else
      \let\documentclass\@twoclasseserror
    \fi
    \inclp@afterpreamble
  }{%
    \ifstrcmpTF{#2}{EOF}{%
      \endgroup\endinput
    }{%
      \inclp@g@bblepreamble
    }%
  }%
}
\let\inclp@document\document
\let\inclp@enddocument\enddocument
\let\inclp@afterbegindoc\relax
\def\inclp@beforeenddoc{%
  \let\enddocument\inclp@enddocument
  \ifinclp@persistent\else
    \incluputfalse
  \fi
  \begingroup
}
\def\inclp@afterpreamble{%
  \inclp@afterbegindoc
  \def\enddocument{%
    \inclp@beforeenddoc
    \endinput
    \aftergroup\ignorespaces
  }%
  \ignorespaces
}
\newletcs\incluput\inclp@gobblepreamble
\ltxkeys@boolkey[KV]{incluput}[inclp@]{useincluput}[true]{}
\ltxkeys@boolkey[KV]{incluput}[inclp@]{persistent}[true]{%
  \ifinclp@persistent
    \def\document{%
      \inclp@document
      \let\documentclass\incluput
    }%
  \fi
}
\ltxkeys@boolkey[KV]{incluput}[inclp@]{nested}[true]{%
  \ifinclp@nested
    \let\inclp@afterbegindoc\begingroup
    \let\inclp@beforeenddoc\relax
  \else
    \inclp@info{Option 'nested' is false}%
  \fi
}
\ltxkeys@declareoption*{%
  \inclp@warn{Unknown option '#3' with value '#4' ignored}%
}
\ltxkeys@executeoptions[KV]<incluput>{persistent,useincluput=false}
\ltxkeys@processoptions*[KV]<incluput>\relax
\providecommand*\input@path{}
\robust@def*\InputDocument{%
  \let\inclp@input\@@input
  \inclp@inputdocument
}
\robust@def*\IncludeDocument{%
  \let\inclp@input\include
  \inclp@inputdocument
}
\robust@def*\inclp@inputdocument{%
  \ifinclp@persistent\else
    \inclp@warn{Option 'persistent' is false.\MsgBrk
      If you don't want to set 'persistent=true',\MsgBrk
      then you must put '\detokenize{\let\documentclass\incluput}'%
      \MsgBrk at the top of each document to input,
      \MsgBrk ie, before the \string\documentclass
    }%
  \fi
  \gletcs\inclp@inputpath\input@path
  \let\inclp@newgeometry\newgeometry
  \let\inclp@restoregeometry\restoregeometry
  \let\newgeometry\@gobble
  \let\restoregeometry\relax
  \cpt@teststopt\inclp@inputfile{.}%
}
\new@def*\inclp@inputfile[#1]#2{%
  \ifblankTF{#1}{}{%
    \edef\input@path{\expandcsonce\input@path{#1/}}%
  }%
  \ifboolTF{cpt@st}{%
    \inclp@inp@tfile{#2}%
  }{%
    \ifcsndefTF{#2@readx}{%
      \inclp@info{File '#2' already read:\MsgBrk
        File '#2' wasn't read again}%
    }{%
      \inclp@inp@tfile{#2}%
    }%
  }%
}
\new@def*\inclp@inp@tfile#1{%
  \IfFileExists{#1}{%
    \inclp@input\@filef@und
    \letcsntocs{#1@readx}\noboundary
    \inclp@afterfileinput
  }{%
    \typein[\answer]{^^J! File '\string#1' does not exist.
      ^^JEnter filename or 'x' and <return> to continue.}%
    \def\reserved@a{x}%
    \ifxTF\answer\reserved@a{%
      \inclp@afterfileinput
    }{%
      \cptexpandarg{\inclp@inputfile[]}\answer
    }%
  }%
}
\new@def*\inclp@afterfileinput{%
  \gletcs\input@path\inclp@inputpath
  \let\newgeometry\inclp@newgeometry
  \let\restoregeometry\inclp@restoregeometry
}

\endinput