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