summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/storebox/storebox.dtx
blob: b81f06d36dd42178eb44bb2bbf183b890085f169 (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
% \iffalse meta-comment
%<=*COPYRIGHT>
%% Copyright (C) 2011 by Martin Scharrer <martin@scharrer-online.de>
%% ---------------------------------------------------------------------------
%% 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 Martin Scharrer.
%%
%% This work consists of the files storebox.dtx and storebox.ins
%% and the derived filebase storebox.sty.
%%
%<=/COPYRIGHT>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{storebox.dtx}[%
%<=*DATE>
    2011/10/21
%<=/DATE>
%<=*VERSION>
    v1.1
%<=/VERSION>
    DTX file for storebox]
\documentclass{ydoc}
\GetFileInfo{storebox.dtx}
\usepackage{storebox}[\filedate]
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{137}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.99}{2011/08/05}{Converted to DTX file.}
% \changes{v1.0}{2011/09/22}{First release.}
% \changes{v1.1}{2011/10/21}{Fixed issue with \env{storebox} environment with new \pkg{collectbox} version.}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{storebox.dtx}
% \author{Martin Scharrer}
% \email{martin@scharrer.me}
% \ifdefined\repository
%   \repository{https://bitbucket.org/martin_scharrer/storebox}
% \fi
%
% \maketitle
%
% \begin{abstract}\noindent
% This package allows the placement of identical content multiple times in a document while only storing it once in the output file.
% At the moment only \LaTeX\ compilers with native PDF output are supported (|pdflatex| or |lualatex|).
% For other \LaTeX\ compilers a fallback to |\savebox| is implemented.
% \end{abstract}
%
% \section{Introduction}
% \LaTeX{} provides box registers to save content and use it later in the document once or multiple times (or none at all).
% However, the box content is then written every time to the output file.
% The PDF file format provides a way to store material as object and reference it later. A similar technique is theoretically
% possible for PostScript output (but not yet implemented). This has the benefit that the content is really only stored once
% in the output file. However, if file compression is used for the final output file the size benefit might be very small
% if the content is only reused a low number of times.
%
% This package provides ``store boxes'' which have the same user interface like normal \LaTeX{} ``save boxes'', but only
% store the content once in the output file even if it is used several times.
% At the moment only PDF output is supported (i.e.\ \texttt{pdflatex} and \texttt{lualatex}).
% If the stored content is not used in the document after all it is not written to the PDF (at least the \texttt{pdftex} manual says so).
% For any other \TeX{} and output format the package simply falls back to use the normal savebox equivalents.
%
% \section{Options}
% The package should be loaded without any options and then automatically checks for PDF output (using the \pkg{ifpdf} package).
% It also provides two options \opt{enable} and \opt{disable} to explicitly switch the storebox feature on or off.
% The \opt{disable} option is useful to see the resulting file size difference.
% The usage of the \opt{enable} option should not be required and will lead to errors if used with unsupported \TeX{} formats.
%
% \Needspace*{3cm}
% \section{Macros}
%
% \DescribeMacro{\newstorebox}{<\textbackslash boxname>}
% Because storeboxes are stored as PDF objects and not as \TeX\ box registers
% it is not required to allocate a register for them. However, in fall-back mode
% the used control sequence \meta{\textbackslash boxname} must be defined as savebox.
% This macro defines \meta{\textbackslash boxname} simply as \Macro\relax\relax (just as precaution) and is equal to
% \Macro\newsavebox in fall-back mode.
%
%
% \DescribeMacro{\storebox}{<\textbackslash boxname>}{<content>}
% This stores the \meta{content} as \meta{\textbackslash boxname} (which is actually just a numeric reference) for later use.
% The \pkg{collectbox} package is used to collect the \meta{content} as box and not as macro argument and therefore it can
% contain verbatim and other special material. The braces can also be written in their explicit form \Macro\bgroup and \Macro\egroup
% and then split in two different macros or across the begin and end of an environment.
% It is possible to use \Macro\storebox multiple times with the same \meta{\textbackslash boxname}. This will create
% a new PDF object without affected the old one.
% In fall-back mode this macro is functional equal to \Macro\savebox\relax (but then still uses \pkg{collectbox}).
%
%
% \DescribeEnv[<content>]{storebox}{<\textbackslash boxname>}
% This is the environment version of \Macro\storebox. Special care is taken to allow for an identical name.
% In fall-back mode this macro is equal to the \env{lrbox} environment.
%
%
% \DescribeMacro{\usestorebox}{<\textbackslash boxname>}
% This macro typesets the stored content at the current position (as horizontal box in horizontal mode).
% This is realized by adding a PDF reference to the stored content.
% In fall-back mode this macro is equal to \Macro\usebox.
%
%
% \DescribeMacro{\ifstorebox}
% This if-switch is set to \emph{true} if \Macro\storebox was successfully defined as intended
% but to \emph{false} if the \Macro\savebox fall-back was used.
%
% \clearpage
% \section{Example}
% \begin{lstlisting}[gobble=2,language={[latex]tex},numbers=none]
% \documentclass{article}
%
% \usepackage{storebox}
%
% \newstorebox{\mybox} % Not really required for PDF output
% \begin{document}
%
% \storebox{\mybox}{\verb+Supports verbatim #$\empty+}
%
% \usestorebox{\mybox}
% \usestorebox{\mybox}
%
% \storebox\mybox\bgroup
% Can also be split
% \verb+\empty+
% \egroup
%
% \usestorebox{\mybox}
% \usestorebox{\mybox}
%
% \begin{storebox}{\mybox}
%     Or used as environment
%     (then will ignore leading and trailing spaces)
% \end{storebox}
%
% \usestorebox{\mybox}
% \usestorebox{\mybox}
%
% \end{document}
% \end{lstlisting}
%
% \setcounter{lstnumber}{1}
% \StopEventually{}
%
% \clearpage
% \section{Implementation}
%
% \iffalse
%<*storebox.sty>
% \fi
%    \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{storebox}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
    2099/01/01 develop
%</DRIVER>
    Store and reuse boxes in a file size efficient way]
%    \end{macrocode}
%
%    \begin{macrocode}
\DeclareOption{disable}{\let\ifstorebox\iffalse}
\DeclareOption{enable}{\let\ifstorebox\iftrue}
\ProcessOptions*
\edef\@tempa{\@ptionlist{\@currname.\@currext}}
\ifx\@tempa\empty
    \RequirePackage{ifpdf}
    \expandafter\let\csname ifstorebox\expandafter\endcsname\csname ifpdf\endcsname
\fi
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{collectbox}[2011/08/04]
%    \end{macrocode}
%
% \begin{macro}{\storebox}
%    \begin{macrocode}
\newcommand*\storebox{%
    \begingroup
        \def\@tempa{storebox}%
    \ifx\@currenvir\@tempa
        \endgroup
        \expandafter\@storebox@env
    \else
        \endgroup
        \expandafter\@storebox
    \fi
}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\ifstorebox
%    \end{macrocode}
%
% \begin{macro}{\@storebox}
% Macro version:
%    \begin{macrocode}
\def\@storebox#1{%
    \begingroup
    \@collectboxto\collectedbox{\pdfxform\collectedbox\endgroup\mathchardef#1=\pdflastxform}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@storebox@env}
% Environment version. Code adapted from \env{lrbox} environment. The group added by \Macro\begin and \Macro\end must
% be specially handled to allow for a local assignment.
%    \begin{macrocode}
\def\@storebox@env#1{%
    \edef\@tempa{%
        \setbox\collectedbox\hbox\bgroup%
            \def\noexpand\the@storebox{\noexpand#1}%
    }%
    \@tempa
    \begingroup
    \aftergroup\@storebox@env@end
    \@endpefalse
    \color@setgroup
    \begingroup
    \def\@currenvir{storebox\empty}%
    \ignorespaces
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endstorebox}
%    \begin{macrocode}
\def\endstorebox{%
    \unskip
    \endgroup
    \color@endgroup
}
%    \end{macrocode}
% \end{macro}
%
%
%
% \begin{macro}{\@storebox@env@end}
% This ends the box assignment and stores the box as PDF xform.
% Then the given control sequence is set to the xform number.
%    \begin{macrocode}
\def\@storebox@env@end{%
    \edef\@tempa{%
        \egroup
        \pdfxform\collectedbox
        \endgroup
        \mathchardef\expandafter\noexpand\the@storebox=\pdflastxform
    }%
    \@tempa
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\newstorebox}
%    \begin{macrocode}
\newcommand*\newstorebox[1]{%
    \@ifdefinable{#1}{\let#1\relax}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\usestorebox}
%    \begin{macrocode}
\newcommand*\usestorebox[1]{%
    \mbox{\pdfrefxform#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\else
%    \end{macrocode}
%
%
% \begin{macro}{\@storebox}
% Macro version:
%    \begin{macrocode}
\def\@storebox#1{\@collectboxto{#1}{}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@storebox@env}
%    \begin{macrocode}
\def\@storebox@env{%
  \edef\@currenvir{\@currenvir\noexpand\noexpand\noexpand\empty}%
  \lrbox
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\endstorebox}
%    \begin{macrocode}
\def\endstorebox{%
    \endlrbox
    \edef\@currenvir{\@currenvir}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\newsavebox}
%    \begin{macrocode}
\@ifdefinable\newstorebox{%
\let\newstorebox\newsavebox
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\usestorebox}
%    \begin{macrocode}
\@ifdefinable\usestorebox{%
\let\usestorebox\usebox
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\fi
%    \end{macrocode}
%

% \iffalse
%</storebox.sty>
% \fi
%
% \Finale
\endinput