3 % $Id: slowbox.dtx,v 1.2 2003/09/06 13:08:16 mdw Exp $
5 % Cop-out for slow things
7 % (c) 2003 Mark Wooding
11 % \begin{meta-comment} <general public licence>
13 %% slowbox package -- skipping of slow boxes
14 %% Copyright (c) 2003 Mark Wooding
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.
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.
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.
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]
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 \~}
55 % \begin{meta-comment}
59 \describespackage{slowbox}
60 \newcommand\opt[1]{\textsf{#1}}
61 \expandafter\def\csname sb@box:tag\endcsname{{2cm}{1cm}{0.5cm}}
62 \def\*{\hspace{0pt plus 0.1pt}\ignorespaces}
68 %^^A-------------------------------------------------------------------------
69 % \section{User guide}
71 % The problem is that some things, e.g., diagrams, can take a while to
72 % typeset, and aren't particularly interesting as far as the global document
73 % layout is concerned. Once you've got a diagram right, the only thing that
74 % matters as far as general previewing is concerned is its size.
76 % The solution is \package{slowbox}. Put things you're usually not
77 % interested in seeing in a \env{slowbox} environment. Then they get
78 % replaced by a frame saying that there's meant to be something there, and
81 % There are two modes, chosen by package options: \opt{show} and \opt{hide}.
82 % The default is \opt{show} mode, because I had to pick one. In \opt{show}
83 % mode, everything happens normally; in \opt{hide} mode, slow boxes are
84 % replaced by frames. In order for the package to know the sizes of all of
85 % your boxes, you must first run through once in \opt{show} mode.
86 % Thereafter, all should be well.
88 % \DescribeEnv{slowbox}
89 % In \opt{show} mode, the \env{slowbox} environment typesets its contents in
90 % LR-mode (for \TeX nicians, restricted horizontal mode).\footnote{^^A
91 % Vertical mode causes all sorts of pain, and makes things way too wide.
92 % Horizontal mode seems a good `standard currency' for \LaTeX\ things like
94 % In \opt{hide} mode, the boxes are replaced by frames, and \TeX\ will skip
95 % over the contents of the environment at high speed, ignoring it
98 % The \env{slowbox} environment needs an argument, a \emph{tag} which is used
99 % to identify this box. This is needed because otherwise the package has no
100 % way of deciding that you've inserted some new box and need to shunt
101 % everything about. So the full syntax is
102 % \syntax{"\\begin{slowbox}{"<tag>"}" \ldots "\\end{slowbox}"}.
103 % Tags can be any piece of text. If you include control sequences in the
104 % tag, then they get expanded. That might be what you want.
106 % \DescribeEnv{slowbox$*$}
107 % Adding new slow boxes is best done using the starred version of the
108 % environment. Stuff in a \env{slowbox$*$} environment is always typset in
109 % \opt{show} mode, regardless of the global mode setting. The right thing to
110 % do is wrap your new stuff in a \env{slowbox$*$} while you're writing it.
111 % Then remove the \env{$*$} and it will turn into a frame. The
112 % \env{slowbox$*$} environment takes a tag name, just like \env{slowbox}
115 % \DescribeMacro\slowboxbegin
116 % \DescribeMacro\slowboxend
117 % It's useful to define your own commands environments which create slow
118 % boxes. Often, though, what happens is that you want to put some stuff
119 % \emph{inside} the slow box, but that doesn't work properly. What you must
120 % do instead is say something like
122 % \newenvironment{splunt}[1]
123 % {...\slowboxbegin{#1}{...\startsplunt...}}
124 % {\slowboxend{...\finishsplunt...}...}
126 % That is, \syntax{"\\slowboxbegin{"<tag>"}{"<stuff>"}"} starts a slow box,
127 % beginning with \<stuff> if we're in \opt{show} mode; and
128 % \syntax{"\\slowboxend{"<stuff>"}"} finishes it, ending with \<stuff>.
130 % The |\slowboxbegin| command has a |*|-variant, which causes the box to be
131 % typeset regardless of the current mode setting.
133 % \DescribeMacro\newslowboxenv
134 % There's an environment for building slow box environments. Say
135 % \syntax{"\\newslowboxenv{"<env-name>"}"\* "["<n-args>"]"\*
136 % "{"<before-text>"}"\* "{"<start-text>"}"\* "{"<end-text>"}"\*
137 % "{"<after-text>"}"}. This creates a new environment (just like with
138 % |\newenvironment|, taking \<n-args> arguments, maybe with the first one
139 % optional and defaulting to \<default>). The contents are put in a slow box
140 % with \<start-text> on the front and \<end-text> on the end. Before the box
141 % is begun, \<before-text> is performed, and \<after-text> is done
142 % afterwards. The environments so created have \env{$*$}-variants which
143 % ignore the current mode, just like \env{slowbox$*$}. They also gather an
144 % additional \<tag> argument, which you don't have to bother with.
146 % \DescribeMacro\doslowbox
147 % Finally, for commands, there's \syntax{"\\doslowbox{"<tag>"}"\*
148 % "{"<stuff>"}"}. This typesets \<stuff> in a slow box with the given tag.
149 % There's a |*|-variant, of course.
151 % \DescribeMacro\slowboxshow
152 % \DescribeMacro\slowboxhide
153 % \DescribeMacro\ifslowboxshow
154 % The declarations |\showboxshow| and |\slowboxhide| switch between the
155 % \opt{show} and \opt{hide} modes. They obey standard scoping rules.
156 % They're probably not very useful. They twiddle the |\ifslowboxshow| macro,
157 % which is an old-fashioned Plain \TeX\ switch. If you're the sort of person
158 % who uses \package{ifthen}, then you can test |\boolean{slowboxshow}|.
160 %^^A-------------------------------------------------------------------------
163 % \section{Implementation of \package{slowbox}}
169 % \subsection{The mode switch}
171 % \begin{macro}{\ifslowboxshow}
172 % \begin{macro}{\slowboxshow}
173 % \begin{macro}{\slowboxhide}
174 % The |\if@slowboxshow| switch remembers which mode we're in. The
175 % |\slowboxshow| and |\slowboxhide| macros flip the switch. It's really
176 % easy. The |\relax|es are to annoy people who say |\global\showboxshow|.
178 \def\slowboxshow{\relax\let\ifslowboxshow\iftrue}
179 \def\slowboxhide{\relax\let\ifslowboxshow\iffalse}
185 % \subsection{Dependencies}
187 % The skipping-over-stuff-at-high-speed is done by my \package{sverb}
190 \RequirePackage{sverb}
193 % \subsection{Options}
195 % Oh, this is obvious.
197 \DeclareOption{show}{\slowboxshow}
198 \DeclareOption{hide}{\slowboxhide}
199 \ExecuteOptions{show}
203 % \subsection{Remembering stuff}
205 % \begin{macro}{\sb@def}
206 % Box dimensions are recorded in the |.aux| file using
207 % \syntax{"\\sb@def{"<tag>"}"\* "{"<width>"}"\* "{"<height>"}"\*
208 % "{"<depth>"}"}. This turns into a definition of a strange control
212 {\expandafter\gdef\csname sb@box:#1\endcsname{{#2}{#3}{#4}}}
216 % \subsection{The actual work}
218 % \begin{macro}{\sb@begin}
219 % This is where most of the intelligence is.
220 % \syntax{"\\sb@begin{"<tag>"}{"<stuff>"}"} starts a slow box, putting
221 % \<stuff> at the start if we're in \opt{show} mode. It doesn't ignore stuff
222 % -- though it sets |\sb@ignore| to be |\relax| in \opt{show} mode and
223 % |\ignore| in \opt{hide} mode. It also sets |\sb@end| correctly, so that
224 % \syntax{"\\sb@end{"<stuff>"}"} ends the current box, putting \<stuff> in
227 % Disappointingly, perhaps, this is in fact just a dispatcher to the right
231 \ifslowboxshow\expandafter\sb@begin@show%
232 \else\expandafter\sb@begin@hide\fi%
237 % \begin{macro}{\sb@begin@show}
238 % Now we have the implementation for \opt{show} mode. This is sometimes
239 % called directly, by |*|-variants. Needs to be |\long| because \<stuff>
240 % might contain embedded vmode-material which could legitimately have |\par|
243 \long\def\sb@begin@show#1#2{%
249 \immediate\write\@auxout%
250 {\string\sb@def{#1}{\the\wd\z@}{\the\ht\z@}{\the\dp\z@}}%
254 \let\sb@ignore\relax%
255 \setbox\z@\hbox\bgroup\color@begingroup#2%
260 % \begin{macro}{\sb@begin@hide}
261 % Now for something a bit stranger. If we're in \opt{hide} mode, we need to
262 % find out whether we've saved information about this box before. We pass
263 % the \emph{control sequence} containing the box dimensions to another
264 % macro. Needs to be |\long| to collect the \<stuff> argument.
266 \long\def\sb@begin@hide#1#2{%
268 \def\sb@end##1{\endgroup}%
269 \let\sb@ignore\ignore%
270 \expandafter\sb@hidden\csname sb@box:#1\endcsname{#1}%
275 % \begin{macro}{\sb@hidden}
276 % We're given the control token and the tag name. If the control sequence is
277 % like |\relax| then |\csname| must have invented it and we just have to make
278 % something up. Otherwise we put the right stuff in a frame. Don't forget
279 % to write out the box dimensions to the new aux file!
282 \ifx#1\relax\fbox{Unknown slow box \texttt{#2}!}%
283 \else\edef\next@{\noexpand\sb@hidden@i#1{#2}}\expandafter\next@\fi%
285 \def\sb@hidden@i#1#2#3#4{%
286 \frameonly{#1}{#2}{#3}{Omitted \texttt{#4}}%
287 \immediate\write\@auxout{\string\sb@def{#4}{#1}{#2}{#3}}%
292 % \subsection{Stuff in frames}
294 % \begin{macro}{\frameonly}
295 % \syntax{"\\frameonly{"<width>"}{"<height>"}{"<depth>"}{"<stuff>"}"}
296 % typesets \<stuff> in a frame with the given dimensions. This is a bit
297 % tricky -- we want the \<stuff> in the middle of the box, but the box's
298 % reference point is somewhere else. We start with a |\vcenter| of the right
299 % height, and then nudge it into position later.
301 % In case the label is just too big, put shrinky glue all round. It'll look
302 % horrible, but at least it won't mess everything up.
304 \def\frameonly#1#2#3#4{%
305 \dimen\tw@#2\advance\dimen\tw@#3\advance\dimen\tw@-.8\p@%
306 \setbox\z@\hbox{$\vcenter{\hrule\@height\dimen\tw@\@depth\z@}$}%
308 \advance\dimen@-.8\p@%
312 \vrule\@height\ht\z@\@depth\dp\z@\hss%
314 \parbox\dimen@{\centering#4}%
318 \dimen@#3\advance\dimen@-\dp\z@%
319 \setbox\z@\hbox{\lower\dimen@\box\z@}%
325 % \subsection{User commands}
327 % \begin{macro}{\slowboxbegin}
328 % \begin{macro}{\slowboxend}
329 % This is just a matter of using the machinery we've built already.
331 \def\slowboxbegin{\@ifstar{\sb@go\sb@begin}{\sb@go\sb@begin@show}}
332 \long\def\sb@go#1#2#3{#1{#2}{#3}\sb@ignore}
333 \def\slowboxend{\sb@end}
338 % \begin{macro}{\doslowbox}
341 \def\doslowbox{\@ifstar{\sb@do\sb@begin}{\sb@do\sb@begin@show}}
342 \long\def\sb@do#1#2#3{#1{#2}{#3}\sb@end}
346 % \begin{macro}{\newslowboxenv}
347 % Something a bit more interesting now. Gathering the optional arguments for
348 % |\newenvironment| is a bit tedious, but we do it anyway. Picking up the
349 % arguments for the environment is a bit tricky -- they come in the wrong
350 % order, unfortunately.
352 \def\newslowboxenv#1{\@ifnextchar[{\sb@nenv@i{#1}}{\sb@nenv@do{#1}{}}}
353 \def\sb@nenv@i#1[#2]{%
354 \@ifnextchar[{\sb@nenv@ii{#1}{[{#2}]}}{\sb@nenv@do{#1}{[{#2}]}}%
356 \def\sb@nenv@ii#1#2[#3]{\sb@nenv@do{#1}{#2[{#2}]}}
357 \long\def\sb@nenv@do#1#2#3#4#5#6{%
358 \newenvironment{#1}#2{#3\sb@env\sb@begin{#4}}{\sb@end{#5}#6}%
359 \newenvironment{#1*}#2{#3\sb@env\sb@begin@show{#4}}{\sb@end{#5}#6}%
361 \def\sb@env#1#2#3{#1{#3}{#2}\sb@ignore}
365 % \begin{environment}{slowbox}
366 % And the \env{slowbox} is built using the above equipment.
368 \newslowboxenv{slowbox}{}{}{}{}