aef21f1d30ab735bfdae5949f1c753289fedb83b
[mdwtools] / slowbox.dtx
1 % \begin{meta-comment}
2 %
3 % $Id: slowbox.dtx,v 1.2 2003/09/06 13:08:16 mdw Exp $
4 %
5 % Cop-out for slow things
6 %
7 % (c) 2003 Mark Wooding
8 %
9 % \end{meta-comment}
10 %
11 % \begin{meta-comment} <general public licence>
12 %%
13 %% slowbox package -- skipping of slow boxes
14 %% Copyright (c) 2003 Mark Wooding
15 %%
16 %% This program is free software; you can redistribute it and/or modify
17 %% it under the terms of the GNU General Public License as published by
18 %% the Free Software Foundation; either version 2 of the License, or
19 %% (at your option) any later version.
20 %%
21 %% This program is distributed in the hope that it will be useful,
22 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
23 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 %% GNU General Public License for more details.
25 %%
26 %% You should have received a copy of the GNU General Public License
27 %% along with this program; if not, write to the Free Software Foundation,
28 %% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 % \end{meta-comment}
30 %
31 % \begin{meta-comment} <Package preambles>
32 %<+package>\NeedsTeXFormat{LaTeX2e}
33 %<+package>\ProvidesPackage{slowbox}
34 %<+package> [2003/08/27 1.0 skipping of slow boxes]
35 % \end{meta-comment}
36 %
37 % \CheckSum{222}
38 %% \CharacterTable
39 %% {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
40 %% 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
41 %% Digits \0\1\2\3\4\5\6\7\8\9
42 %% Exclamation \! Double quote \" Hash (number) \#
43 %% Dollar \$ Percent \% Ampersand \&
44 %% Acute accent \' Left paren \( Right paren \)
45 %% Asterisk \* Plus \+ Comma \,
46 %% Minus \- Point \. Solidus \/
47 %% Colon \: Semicolon \; Less than \<
48 %% Equals \= Greater than \> Question mark \?
49 %% Commercial at \@ Left bracket \[ Backslash \\
50 %% Right bracket \] Circumflex \^ Underscore \_
51 %% Grave accent \` Left brace \{ Vertical bar \|
52 %% Right brace \} Tilde \~}
53 %%
54 %
55 % \begin{meta-comment}
56 %
57 %<*driver>
58 \input{mdwtools}
59 \describespackage{slowbox}
60 \newcommand\opt[1]{\textsf{#1}}
61 \def\*{\hspace{0pt plus 0.1pt}\ignorespaces}
62 \mdwdoc
63 %</driver>
64 %
65 % \end{meta-comment}
66 %
67 %^^A-------------------------------------------------------------------------
68 % \section{User guide}
69 %
70 % The problem is that some things, e.g., diagrams, can take a while to
71 % typeset, and aren't particularly interesting as far as the global document
72 % layout is concerned. Once you've got a diagram right, the only thing that
73 % matters as far as general previewing is concerned is its size.
74 %
75 % The solution is \package{slowbox}. Put things you're usually not
76 % interested in seeing in a \env{slowbox} environment. Then they get
77 % replaced by a frame saying that there's meant to be something there, and
78 % that's it.
79 %
80 % There are two modes, chosen by package options: \opt{show} and \opt{hide}.
81 % The default is \opt{show} mode, because I had to pick one. In \opt{show}
82 % mode, everything happens normally; in \opt{hide} mode, slow boxes are
83 % replaced by frames. In order for the package to know the sizes of all of
84 % your boxes, you must first run through once in \opt{show} mode.
85 % Thereafter, all should be well.
86 %
87 % \DescribeEnv{slowbox}
88 % In \opt{show} mode, the \env{slowbox} environment typesets its contents in
89 % LR-mode (for \TeX nicians, restricted horizontal mode).\footnote{^^A
90 % Vertical mode causes all sorts of pain, and makes things way too wide.
91 % Horizontal mode seems a good `standard currency' for \LaTeX\ things like
92 % diagrams.} ^^A
93 % In \opt{hide} mode, the boxes are replaced by frames, and \TeX\ will skip
94 % over the contents of the environment at high speed, ignoring it
95 % completely.
96 %
97 % The \env{slowbox} environment needs an argument, a \emph{tag} which is used
98 % to identify this box. This is needed because otherwise the package has no
99 % way of deciding that you've inserted some new box and need to shunt
100 % everything about. So the full syntax is
101 % \syntax{"\\begin{slowbox}{"<tag>"}" \ldots "\\end{slowbox}"}.
102 % Tags can be any piece of text. If you include control sequences in the
103 % tag, then they get expanded. That might be what you want.
104 %
105 % \DescribeEnv{slowbox$*$}
106 % Adding new slow boxes is best done using the starred version of the
107 % environment. Stuff in a \env{slowbox$*$} environment is always typset in
108 % \opt{show} mode, regardless of the global mode setting. The right thing to
109 % do is wrap your new stuff in a \env{slowbox$*$} while you're writing it.
110 % Then remove the \env{$*$} and it will turn into a frame. The
111 % \env{slowbox$*$} environment takes a tag name, just like \env{slowbox}
112 % does.
113 %
114 % \DescribeMacro\slowboxbegin
115 % \DescribeMacro\slowboxend
116 % It's useful to define your own commands environments which create slow
117 % boxes. Often, though, what happens is that you want to put some stuff
118 % \emph{inside} the slow box, but that doesn't work properly. What you must
119 % do instead is say something like
120 %\begin{verbatim}
121 % \newenvironment{splunt}[1]
122 % {...\slowboxbegin{#1}{...\startsplunt...}}
123 % {\slowboxend{...\finishsplunt...}...}
124 %\end{verbatim}
125 % That is, \syntax{"\\slowboxbegin{"<tag>"}{"<stuff>"}"} starts a slow box,
126 % beginning with \<stuff> if we're in \opt{show} mode; and
127 % \syntax{"\\slowboxend{"<stuff>"}"} finishes it, ending with \<stuff>.
128 %
129 % The |\slowboxbegin| command has a |*|-variant, which causes the box to be
130 % typeset regardless of the current mode setting.
131 %
132 % \DescribeMacro\newslowboxenv
133 % There's a command for building slow box environments. Say
134 % \syntax{"\\newslowboxenv{"<env-name>"}"\* "["<n-args>"]"\*
135 % "{"<before-text>"}"\* "{"<start-text>"}"\* "{"<end-text>"}"\*
136 % "{"<after-text>"}"}. This creates a new environment (just like with
137 % |\newenvironment|, taking \<n-args> arguments, maybe with the first one
138 % optional and defaulting to \<default>). The contents are put in a slow box
139 % with \<start-text> on the front and \<end-text> on the end. Before the box
140 % is begun, \<before-text> is performed, and \<after-text> is done
141 % afterwards. The environments so created have \env{$*$}-variants which
142 % ignore the current mode, just like \env{slowbox$*$}. They also gather an
143 % additional \<tag> argument, which you don't have to bother with.
144 %
145 % \DescribeMacro\doslowbox
146 % Finally, for commands, there's \syntax{"\\doslowbox{"<tag>"}"\*
147 % "{"<stuff>"}"}. This typesets \<stuff> in a slow box with the given tag.
148 % There's a |*|-variant, of course.
149 %
150 % \DescribeMacro\slowboxshow
151 % \DescribeMacro\slowboxhide
152 % \DescribeMacro\ifslowboxshow
153 % The declarations |\showboxshow| and |\slowboxhide| switch between the
154 % \opt{show} and \opt{hide} modes. They obey standard scoping rules.
155 % They're probably not very useful. They twiddle the |\ifslowboxshow| macro,
156 % which is an old-fashioned Plain \TeX\ switch. If you're the sort of person
157 % who uses \package{ifthen}, then you can test |\boolean{slowboxshow}|.
158 %
159 %^^A-------------------------------------------------------------------------
160 % \implementation
161 %
162 % \section{Implementation of \package{slowbox}}
163 %
164 % \begin{macrocode}
165 %<*package>
166 % \end{macrocode}
167 %
168 % \subsection{The mode switch}
169 %
170 % \begin{macro}{\ifslowboxshow}
171 % \begin{macro}{\slowboxshow}
172 % \begin{macro}{\slowboxhide}
173 % The |\if@slowboxshow| switch remembers which mode we're in. The
174 % |\slowboxshow| and |\slowboxhide| macros flip the switch. It's really
175 % easy. The |\relax|es are to annoy people who say |\global\showboxshow|.
176 % \begin{macrocode}
177 \def\slowboxshow{\relax\let\ifslowboxshow\iftrue}
178 \def\slowboxhide{\relax\let\ifslowboxshow\iffalse}
179 % \end{macrocode}
180 % \end{macro}
181 % \end{macro}
182 % \end{macro}
183 %
184 % \subsection{Dependencies}
185 %
186 % The skipping-over-stuff-at-high-speed is done by my \package{sverb}
187 % package.
188 % \begin{macrocode}
189 \RequirePackage{sverb}
190 % \end{macrocode}
191 %
192 % \subsection{Options}
193 %
194 % Oh, this is obvious.
195 % \begin{macrocode}
196 \DeclareOption{show}{\slowboxshow}
197 \DeclareOption{hide}{\slowboxhide}
198 \ExecuteOptions{show}
199 \ProcessOptions
200 % \end{macrocode}
201 %
202 % \subsection{Remembering stuff}
203 %
204 % \begin{macro}{\sb@def}
205 % Box dimensions are recorded in the |.aux| file using
206 % \syntax{"\\sb@def{"<tag>"}"\* "{"<width>"}"\* "{"<height>"}"\*
207 % "{"<depth>"}"}. This turns into a definition of a strange control
208 % sequence.
209 % \begin{macrocode}
210 \def\sb@def#1#2#3#4%
211 {\expandafter\gdef\csname sb@box:#1\endcsname{{#2}{#3}{#4}}}
212 % \end{macrocode}
213 % \end{macro}
214 %
215 % \subsection{The actual work}
216 %
217 % \begin{macro}{\sb@begin}
218 % This is where most of the intelligence is.
219 % \syntax{"\\sb@begin{"<tag>"}{"<stuff>"}"} starts a slow box, putting
220 % \<stuff> at the start if we're in \opt{show} mode. It doesn't ignore stuff
221 % -- though it sets |\sb@ignore| to be |\relax| in \opt{show} mode and
222 % |\ignore| in \opt{hide} mode. It also sets |\sb@end| correctly, so that
223 % \syntax{"\\sb@end{"<stuff>"}"} ends the current box, putting \<stuff> in
224 % it.
225 %
226 % Disappointingly, perhaps, this is in fact just a dispatcher to the right
227 % implementation.
228 % \begin{macrocode}
229 \def\sb@begin{%
230 \ifslowboxshow\expandafter\sb@begin@show%
231 \else\expandafter\sb@begin@hide\fi%
232 }
233 % \end{macrocode}
234 % \end{macro}
235 %
236 % \begin{macro}{\sb@begin@show}
237 % Now we have the implementation for \opt{show} mode. This is sometimes
238 % called directly, by |*|-variants. Needs to be |\long| because \<stuff>
239 % might contain embedded vmode-material which could legitimately have |\par|
240 % tokens in.
241 % \begin{macrocode}
242 \long\def\sb@begin@show#1#2{%
243 \begingroup%
244 \def\sb@end##1{%
245 ##1%
246 \color@endgroup%
247 \egroup%
248 \immediate\write\@auxout%
249 {\string\sb@def{#1}{\the\wd\z@}{\the\ht\z@}{\the\dp\z@}}%
250 \leavevmode\box\z@%
251 \endgroup%
252 }%
253 \let\sb@ignore\relax%
254 \setbox\z@\hbox\bgroup\color@begingroup#2%
255 }
256 % \end{macrocode}
257 % \end{macro}
258 %
259 % \begin{macro}{\sb@begin@hide}
260 % Now for something a bit stranger. If we're in \opt{hide} mode, we need to
261 % find out whether we've saved information about this box before. We pass
262 % the \emph{control sequence} containing the box dimensions to another
263 % macro. Needs to be |\long| to collect the \<stuff> argument.
264 % \begin{macrocode}
265 \long\def\sb@begin@hide#1#2{%
266 \begingroup%
267 \def\sb@end##1{\endgroup}%
268 \let\sb@ignore\ignore%
269 \expandafter\sb@hidden\csname sb@box:#1\endcsname{#1}%
270 }
271 % \end{macrocode}
272 % \end{macro}
273 %
274 % \begin{macro}{\sb@hidden}
275 % We're given the control token and the tag name. If the control sequence is
276 % like |\relax| then |\csname| must have invented it and we just have to make
277 % something up. Otherwise we put the right stuff in a frame. Don't forget
278 % to write out the box dimensions to the new aux file!
279 % \begin{macrocode}
280 \def\sb@hidden#1#2{%
281 \ifx#1\relax\fbox{Unknown slow box \texttt{#2}!}%
282 \else\edef\next@{\noexpand\sb@hidden@i#1{#2}}\expandafter\next@\fi%
283 }
284 \def\sb@hidden@i#1#2#3#4{%
285 \frameonly{#1}{#2}{#3}{Omitted \texttt{#4}}%
286 \immediate\write\@auxout{\string\sb@def{#4}{#1}{#2}{#3}}%
287 }
288 % \end{macrocode}
289 % \end{macro}
290 %
291 % \subsection{Stuff in frames}
292 %
293 % \begin{macro}{\frameonly}
294 % \syntax{"\\frameonly{"<width>"}{"<height>"}{"<depth>"}{"<stuff>"}"}
295 % typesets \<stuff> in a frame with the given dimensions. This is a bit
296 % tricky -- we want the \<stuff> in the middle of the box, but the box's
297 % reference point is somewhere else. We start with a |\vcenter| of the right
298 % height, and then nudge it into position later.
299 %
300 % In case the label is just too big, put shrinky glue all round. It'll look
301 % horrible, but at least it won't mess everything up.
302 % \begin{macrocode}
303 \def\frameonly#1#2#3#4{%
304 \dimen\tw@#2\advance\dimen\tw@#3\advance\dimen\tw@-.8\p@%
305 \setbox\z@\hbox{$\vcenter{\hrule\@height\dimen\tw@\@depth\z@}$}%
306 \dimen@#1%
307 \advance\dimen@-.8\p@%
308 \setbox\z@\vbox{%
309 \hrule\vss%
310 \hb@xt@\dimen@{%
311 \vrule\@height\ht\z@\@depth\dp\z@\hss%
312 \advance\dimen@-2em%
313 \parbox\dimen@{\centering#4}%
314 \hss\vrule}%
315 \vss\hrule%
316 }%
317 \dimen@#3\advance\dimen@-\dp\z@%
318 \setbox\z@\hbox{\lower\dimen@\box\z@}%
319 \leavevmode\box\z@%
320 }
321 % \end{macrocode}
322 % \end{macro}
323 %
324 % \subsection{User commands}
325 %
326 % \begin{macro}{\slowboxbegin}
327 % \begin{macro}{\slowboxend}
328 % This is just a matter of using the machinery we've built already.
329 % \begin{macrocode}
330 \def\slowboxbegin{\@ifstar{\sb@go\sb@begin}{\sb@go\sb@begin@show}}
331 \long\def\sb@go#1#2#3{#1{#2}{#3}\sb@ignore}
332 \def\slowboxend{\sb@end}
333 % \end{macrocode}
334 % \end{macro}
335 % \end{macro}
336 %
337 % \begin{macro}{\doslowbox}
338 % And this too.
339 % \begin{macrocode}
340 \def\doslowbox{\@ifstar{\sb@do\sb@begin}{\sb@do\sb@begin@show}}
341 \long\def\sb@do#1#2#3{#1{#2}{#3}\sb@end}
342 % \end{macrocode}
343 % \end{macro}
344 %
345 % \begin{macro}{\newslowboxenv}
346 % Something a bit more interesting now. Gathering the optional arguments for
347 % |\newenvironment| is a bit tedious, but we do it anyway. Picking up the
348 % arguments for the environment is a bit tricky -- they come in the wrong
349 % order, unfortunately.
350 % \begin{macrocode}
351 \def\newslowboxenv#1{\@ifnextchar[{\sb@nenv@i{#1}}{\sb@nenv@do{#1}{}}}
352 \def\sb@nenv@i#1[#2]{%
353 \@ifnextchar[{\sb@nenv@ii{#1}{[{#2}]}}{\sb@nenv@do{#1}{[{#2}]}}%
354 }
355 \def\sb@nenv@ii#1#2[#3]{\sb@nenv@do{#1}{#2[{#2}]}}
356 \long\def\sb@nenv@do#1#2#3#4#5#6{%
357 \newenvironment{#1}#2{#3\sb@env\sb@begin{#4}}{\sb@end{#5}#6}%
358 \newenvironment{#1*}#2{#3\sb@env\sb@begin@show{#4}}{\sb@end{#5}#6}%
359 }
360 \def\sb@env#1#2#3{#1{#3}{#2}\sb@ignore}
361 % \end{macrocode}
362 % \end{macro}
363 %
364 % \begin{environment}{slowbox}
365 % And the \env{slowbox} is built using the above equipment.
366 % \begin{macrocode}
367 \newslowboxenv{slowbox}{}{}{}{}
368 % \end{macrocode}
369 % \end{environment}
370 %
371 % \begin{macrocode}
372 %</package>
373 % \end{macrocode}
374 %
375 % \Finale
376 \endinput