Debian Codegraph @

texmf-dist/source/latex/marginfix/marginfix.dtx

raw
1 % \iffalse
2 %
3 % Copyright 2010 Stephen Hicks, All rights reserved.
4 % marginfix.dtx - 29 Jul 2010
5 % originally floatprobsbegone, created 21 Mar 2008
6 %
7 % Run LaTeX on this document to produce documentation.
8 % Run LaTeX on marginfix.ins to produce the package.
9 %<*driver>
10 \ProvidesFile{marginfix.dtx}
11 %</driver>
12 %
13 %<package>\NeedsTeXFormat{LaTeX2e}
14 %<package>\ProvidesPackage{marginfix}%
15 [2020/05/06 v1.2 Fix Margin Paragraphs]
16 %<*driver>
17 \documentclass{ltxdoc}
18 \CheckSum{1159}
19 %\OnlyDescription % (un)comment this line to show (hide) source code
20 \RecordChanges
21 \EnableCrossrefs
22 \CodelineIndex % (un)comment this line to index source by page (line)
23 \begin{document}
24 \newcommand*\Lopt[1]{\textsf {#1}}
25 \newcommand*\lit[1]{\texttt{\char`#1}} %% literal character (funny catcodes)
26 \parindent0pt
27 \def\*#1{\texttt{\string#1}} %% sdh - |...| doesn't work in headings
28 \makeatletter
29 %
30 \let\pkg\textsf
31 \newcount\mac@depth\mac@depth\z@
32 \newcommand\@macros{}\newcommand\@endmacros{}
33 \catcode`&3 %% we use a funny catcode to ensure never used.
34 \def\@macros#1,{\macro{#1}\global\advance\mac@depth\@ne\relax
35 \@ifnextchar&\@gobble\@macros}
36 \def\@endmacros{\let\mac@next\relax\ifnum\mac@depth>\z@
37 \endmacro\let\mac@next\@endmacros
38 \global\advance\mac@depth\m@ne\fi\mac@next}
39 \newenvironment{macros}[1]{\@macros#1,&}{\@endmacros}
40 \catcode`&4 %% put it back
41 \makeatother %% must be balanced for character table to work properly
42 %
43 \DocInput{marginfix.dtx}
44 \setcounter{IndexColumns}{2}
45 \PrintIndex
46 \PrintChanges
47 \end{document}
48 %</driver>
49 % \fi
50 % \changes{v0.0}{2008/03/21}
51 % {(SDH) Initial version of floatprobsbegone.}
52 % \changes{v0.9}{2010/08/18}
53 % {(SDH) Initial CTAN version.}
54 % \changes{v0.9.1}{2010/08/28}
55 % {(SDH) Fix bug where we alternated sides in article.}
56 % \changes{v1.0}{2013/09/01}
57 % {(Dario Buttari) Alternate order of \cs{marginpar} arguments to be
58 % consistent with the original macro.}
59 % \changes{v1.0}{2013/09/01}
60 % {(Dario Buttari) Fix bug in deferred note spacing.}
61 % \changes{v1.0}{2013/09/01}
62 % {(Dario Buttari) Fix issues with \cs{marginheightadjustment} and
63 % \cs{extendmargin} not being applied and reset consistently.}
64 % \changes{v1.0}{2013/09/01}
65 % {(SDH) Stop gobbling vertical stretch on page. Margin notes will
66 % not line up properly if page is stretched.}
67 % \changes{v1.0}{2013/09/01}
68 % {(SDH) Add margin phantoms.}
69 % \changes{v1.1}{2013/09/08}
70 % {(SDH) Globally calculate margin phantoms over 4 passes.}
71 % \changes{v1.1}{2013/09/08}
72 % {(SDH) Add \cs{topskip} to notes at the top of the margin.}
73 % \changes{v1.2}{2020/05/06}
74 % {(SDH) Fix long-standing bug where margin notes called out in
75 % the last few points of a page were being entirely dropped.}
76 %
77 % \GetFileInfo{marginfix.dtx}
78 % \title{\Lopt{marginfix} package documentation}
79 % \author{Stephen Hicks\\%
80 % \texttt{[email protected]}\\%
81 % \texttt{http://shicks.github.com/marginfix}}
82 % \date{\fileversion{} -- \filedate}
83 % \maketitle
84 %
85 % \part*{Usage}
86 % \section{Overview}
87 % Authors using \LaTeX\ to typeset books with significant margin material
88 % often run into the problem of long notes running off the bottom of the
89 % page. A typical workaround is to insert \cs{vshift}s by hand, but
90 % this is a tedious process that is invalidated when pagination changes.
91 % Another workaround is \pkg{memoir}'s \cs{sidebar} function, but this can be
92 % unsatisfying for short textual notes, and standard marginpars cannot
93 % be mixed with sidebars. This package implements a solution to
94 % make marginpars "just work" by keeping a list of floating inserts and
95 % arranging them intelligently in the output routine. The credit for the
96 % concept behind this algorithm goes to Prof. Andy Ruina, who employed me
97 % to work on some of his textbook macros in 2007--9.
98 %
99 % \section{Options}
100 % There are currently no options that do anything yet.
101 %
102 % \section{Commands}
103 % For the most part, this is a drop-in replacement. Simply include
104 % a call to |\usepackage{marginfix}| to the preamble, use \cs{marginpar}
105 % normally and hope for the best.
106 % In the event, however, that it doesn't work exactly as hoped,
107 % there are a number of tweaks that the user can apply.
108 %
109 % \DescribeMacro{\marginskip}
110 % Calling \cs{marginskip}\marg{length} will insert an incompressible
111 % skip in the margin. These skips will force neighboring notes on
112 % the same page to be separated, but will disappear at the top or
113 % bottom of a margin.
114 %
115 % \DescribeMacro{\clearmargin}\DescribeMacro{\softclearmargin}
116 % In an analog to \cs{clearpage}, \cs{clearmargin} prevents any further
117 % material from being added to the current margin. These calls are
118 % cumulative, so that two \cs{clearmargin}s in a row will produce a
119 % completely empty margin on the next page as well. If this is not
120 % the desired effect, use \cs{softclearmargin}, which is effectively
121 % idempotent: multiple calls have the same effect as one call to
122 % end the current margin.
123 %
124 % \DescribeMacro{\extendmargin}
125 % If a page has too much margin material to fit and an important note
126 % is floating to the next page, \cs{extendmargin}\marg{length} will
127 % extend the margin (for the current page only) by the given length.
128 % If the length is negative, the margin will shrink. Multiple calls
129 % on the same page are cumulative.
130 %
131 % \DescribeMacro{\mparshift}
132 % To adjust the position of a single note, use \cs{mparshift}\marg{length}
133 % before a call to \cs{marginpar}. Positive lengths move it down the page.
134 % This essentially shifts the call-out location, so the actual position of
135 % the note might not change if the margin is sufficiently crowded.
136 % Multiple calls before the same note are cumulative.
137 %
138 % \DescribeMacro{\marginheightadjustment}
139 % If all the margins are the wrong size, the height of the margin on every
140 % page can be adjusted by assigning a non-zero value to the dimension
141 % register \cs{marginheightadjustment} (as in
142 % \cs{marginheightadjustment}\texttt{=}$\langle$\emph{length}$\rangle$).
143 % This is effectively the same as a call to
144 % \cs{extendmargin} on every page.
145 %
146 % \DescribeMacro{\marginposadjustment}
147 % Similarly, if all the margin notes are in the wrong place, the callout
148 % positions can be adjusted globally by assigning a non-zero value to the
149 % dimension register \cs{marginposadjustment}. This is effectively the same
150 % as a call to \cs{mparshift} before every note. This is particularly useful
151 % at present because the height of the line on which the margin note is called
152 % is currently only estimated, and appears to be off by a point or two.
153 % This may get fixed in the future, but until then, the adjustment is
154 % possibly the easiest workaround.
155 %
156 % \DescribeMacro{\blockmargin}\DescribeMacro{\unblockmargin}
157 % As of version 1.0, we now support ``margin phantoms'': sections of the
158 % margin in which no notes will be placed, which can be useful for large
159 % figures that jut into the margin (note: margins already move out of the
160 % way of floats, regardless of whether or not they extend into the margin;
161 % this is mainly for in-place figures or equations). The easiest way to
162 % block off part of the margin is to call \cs{blockmargin} before the
163 % extended content and \cs{unblockmargin} afterwards. No margin notes
164 % will be placed between these two points (though one must be careful:
165 % if one of these is called in horizontal mode, the toggle will occur at
166 % the \emph{top} of the current line). Each of these commands takes an
167 % optional argument: \cs{blockmargin}\oarg{pos} will begin the margin
168 % block at a position \emph{pos} below the current position (or above
169 % if \emph{pos} is negative), and \cs{unblockmargin}\oarg{pos} will
170 % likewise end the block at a position \emph{pos} below the current
171 % position.
172 %
173 % \DescribeMacro{\marginphantom}
174 % Margin phantoms may also be called out in place with a known size
175 % using \cs{marginphantom}\oarg{pos}\marg{size}, which is essentially
176 % equivalent to \cs{blockmargin}\oarg{pos}\cs{unblockmargin}\oarg{pos$+$size}.
177 % Either argument may be negative to refer upward rather than downward.
178 %
179 % \section{Interaction with other packages}
180 % \subsection{memoir}
181 % There are no known issues with \Lopt{memoir} at present, provided that
182 % \cs{sidebar} is not used.
183 %
184 % \subsection{mparhack}
185 % \Lopt{mparhack} was designed to deal with the problem of margin notes
186 % showing up in the wrong margin because the left/right was decided before
187 % it was known exactly which page the note would be on. Because we defer
188 % this decision to shipout time in this package, we are not susceptible
189 % to this problem, so \Lopt{mparhack} is no longer needed and should not
190 % be included (though I'm unaware whether it causes any actual problems).
191 %
192 % \subsection{Multiple columns}
193 % There is currently no support for multiple columns.
194 %
195 % \section{Coming attractions and known issues}
196 % Here is a list of things to possibly look forward to in a future version.
197 % If any of them are particularly important, please let me know.
198 % \begin{itemize}
199 % \item Use of pdf\TeX's \cs{pdfsavepos} and \cs{pdflastypos} for more
200 % accurate margin placement.
201 % \item \cs{vadjust} to correct inconsistencies with \cs{@pageht}.
202 % \item Margin note placement is irrespective of vertical stretch.
203 % Previously we gobbled any vertical stretch, but now that we have
204 % fixed that bug, there's the possibility of wrong alignment since
205 % we don't know where the positions will ultimately end up. This
206 % may be fixed by \cs{pdfsavepos} as well.
207 % \item Better interaction with floats.
208 % (We can set a default one way
209 % or the other and then allow a macro to override it (presumably with a
210 % CS defined in terms of the box name/meaning, so as not to get in the
211 % way of \LaTeX's use of the insert registers). We would then add or
212 % not add phantoms in the right spots. We'd also need to shift all the
213 % callout points by the size of the top figures (unless we're using
214 % \cs{pdfsavepos}).)
215 % \end{itemize}
216 %
217 % \StopEventually{}
218 %
219 % \makeatletter
220 % \part*{Implementation}
221 % \section{Initial Setup}
222 % Make the |@|-sign into a letter for use in macro names.
223 % \begin{macrocode}
224 %<*package>
225 \makeatletter
226 % \end{macrocode}
227 %
228 % \begin{macros}{\MFX@debug}
229 % We have some optionally-included code for debugging. \cs{MFX@debug}
230 % prints a new line followed by ``|MFX: |'' and then the message.
231 % We'll also ask for more error context in the debug mode.
232 % \begin{macrocode}
233 %<*debug>
234 \def\MFX@debug{\message{^^JMFX:}\message}
235 \errorcontextlines=20
236 \def\MFX@mac#1{\expandafter\MFX@@mac\meaning#1>>>}
237 \def\MFX@@mac#1->{<<<}
238 \def\MFX@htdp#1{\ht#1=\the\ht#1, \dp#1=\the\dp#1}
239 %</debug>
240 % \end{macrocode}
241 % \end{macros}
242 %
243 % The reader might begin to note at this point a convention we
244 % adopt throughout this package. While we strive to avoid introducing
245 % new names as much as possible (with clever usages of \cs{expandafter}),
246 % any new names we do introduce will be prefixed
247 % by |\MFX@|, |\Mfx@|, or |\mfx@|, depending on the type of name.
248 % The all-capitol |\MFX@| is used for fully-constant macros. The
249 % initial-caps |\Mfx@| is used for control sequences that are
250 % technically constant, but that refer to things that change, such
251 % as counters, token lists, dimension registers, etc. Finally,
252 % the lowercase |\mfx@| is used for control sequences whose meaning
253 % changes dynamically (i.e. variable macros).
254 %
255 % \section{Options}
256 % Here we define the various package options.
257 % \iffalse - CURRENTLY UNIMPLEMENTED
258 % \begin{macros}{\ifmfx@ypos}
259 % The \Lopt{ypos} option signifies that we should use the pdf{\TeX}
260 % primitives \cs{pdfsavepos} and \cs{pdflastypos} to improve positioning
261 % of margin notes relative to their callouts. This requires two
262 % passes to work, and the first time through, the margin notes
263 % will be positioned very na\"ively.
264 % \begin{macrocode}
265 \newif\ifmfx@ypos
266 \DeclareOption{ypos}{\mfx@ypostrue}
267 % \end{macrocode}
268 % \end{macros}
269 % END IFFALSE - \else There are no options yet. \fi
270 %
271 % Now we actually process the options.
272 % \begin{macrocode}
273 \ProcessOptions\relax
274 % \end{macrocode}
275 %
276 % \section{Variables}
277 % \begin{macros}{\mfx@marginlist}
278 % We need a place to store our list of marginal material. We
279 % store material in this variable using insert registers and
280 % a variety of macros, to be explained later.
281 % \begin{macrocode}
282 \let\mfx@marginlist\@empty
283 % \end{macrocode}
284 % \end{macros}
285 %
286 % \begin{macros}{\mfx@inject,\Mfx@inject@insert}
287 % These are used to hijack \cs{marginpar} to inject arbitrary code
288 % into the output routine, rather than actually set a note. To
289 % inject code, we unshift two copies of this dummy insert onto
290 % \cs{@freelist} for \cs{marginpar} to pull off. We append
291 % whatever code we want to inject into \cs{mfx@inject} and then
292 % call \cs{marginpar}. Then our custom \cs{@addmarginpar} will
293 % recognize the dummy inserts and run the code instead of setting
294 % a margin note.
295 % \begin{macrocode}
296 \let\mfx@injected\@empty
297 \newinsert\Mfx@inject@insert
298 % \end{macrocode}
299 % \end{macros}
300 %
301 % \begin{macros}{\Mfx@marginbox}
302 % While we're building the margin, we need to put it in a box
303 % before we can attach it to the main columm.
304 % \begin{macrocode}
305 \newbox\Mfx@marginbox
306 % \end{macrocode}
307 % \end{macros}
308 %
309 % \begin{macros}{\Mfx@marginpos@min,\Mfx@marginpos@max,\Mfx@marginspace}
310 % While we build up the margin piece boxes, we need to keep track of the
311 % possible range of positions. The pair \cs{Mfx@marginpos@min} and
312 % \cs{Mfx@marginpos@max} are used to accumulate how much material has
313 % been added so far, with the difference that \cs{Mfx@marginpos@min} doesn't
314 % take into account compressible space, while \cs{Mfx@marginpos@max} does.
315 % Finally, \cs{Mfx@marginspace} is the amount of (incompressible) space
316 % since the last note, which allows skips to span margin phantoms.
317 % \begin{macrocode}
318 \newdimen\Mfx@marginpos@min
319 \newdimen\Mfx@marginpos@max
320 \newdimen\Mfx@marginspace
321 % \end{macrocode}
322 % \end{macros}
323 %
324 % \begin{macros}{\Mfx@marginheight}
325 % Because the margin height can be altered by, \cs{extendmargin},
326 % we must maintain a dimension for the height of the current margin.
327 % This dimension is reused in several different ways in the shipout-time
328 % margin building routines, keeping track of how much much space is left
329 % in (for the global passes) and the end position of the end of (for the
330 % piecewise passes) the current piece.
331 % \begin{macrocode}
332 \newdimen\Mfx@marginheight
333 % \end{macrocode}
334 % \end{macros}
335 %
336 % \begin{macros}{\mfx@marginstart,\mfx@marginpieces,
337 % \Mfx@piece@content,\Mfx@piece@count,\ifmfx@in@phantom}
338 % These control sequences keep track of the margin phantoms. When the
339 % margin is unblocked then \cs{mfx@marginstart} is not \cs{relax}. When
340 % a phantom begins, the current \cs{mfx@marginstart} and page position
341 % are stored as a pair in \cs{mfx@marginpieces}, which is iterated over
342 % while building individual pieces of the margin. We also define a box
343 % to keep the content of each margin piece, and a counter to keep track
344 % of how many pieces we have. Finally, we define a switch for use in
345 % the second pass to indicate that we're inside a phantom.
346 % \begin{macrocode}
347 \def\mfx@marginstart{0pt}
348 \let\mfx@marginpieces\@empty
349 \newbox\Mfx@piece@content
350 \newcount\Mfx@piece@count
351 \newif\ifmfx@in@phantom
352 % \end{macrocode}
353 % \end{macros}
354 %
355 % \begin{macros}{\Mfx@mparshift}
356 % We store the current shift in a dimension register.
357 % \begin{macrocode}
358 \newdimen\Mfx@mparshift
359 % \end{macrocode}
360 % \end{macros}
361 %
362 % \section{User-configurable dimensions}
363 % We export a few dimensions that the user can redefine to tweak behavior.
364 % \begin{macros}{\marginheightadjustment}
365 % This length will be added to the total margin height of each page
366 % (the default is zero).
367 % \begin{macrocode}
368 \newdimen\marginheightadjustment
369 % \end{macrocode}
370 % \end{macros}
371 %
372 % \begin{macros}{\marginposadjustment}
373 % We will offset each margin note from its callout location by this length
374 % (the default is zero).
375 % \begin{macrocode}
376 \newdimen\marginposadjustment
377 % \end{macrocode}
378 % \end{macros}
379 %
380 % \section{Plan of attack}
381 % \subsection{\cs{marginpar}}
382 % The default sequence of events for a \cs{marginpar} is roughly the following
383 % (assuming no errors):
384 % \begin{verbatim}
385 % \marginpar:
386 % let \@floatpenalty := (horizontal ? -10002 : -10003)
387 % allocate inserts \@currbox and \@marbox from \@freelist
388 % let \count\@marbox := -1 % signifies marginpar (not float)
389 % if optional argument then \@xmpar else \@ympar
390 % \@xmpar:
391 % \@savemarbox \@currbox := required argument
392 % \@savemarbox \@marbox := optional argument
393 % \@xympar
394 % \@ympar:
395 % \@savemarbox \@currbox := required argument
396 % copy \@marbox := \@currbox
397 % \@xympar
398 % \@xympar:
399 % append \@marbox to \@currlist
400 % \end@float
401 % \end@float:
402 % append \@currbox to \@currlist
403 % if horizontal then following two lines are in \vadjust:
404 % \penalty -10004
405 % \penalty \@floatpenalty
406 % \end{verbatim}
407 %
408 % To get the rest of the picture, we need to peek into the
409 % output routine. The pertinent parts are as follows (in
410 % vanilla \LaTeX):
411 % \begin{verbatim}
412 % \output:
413 % if \outputpenalty < -10000 then
414 % \@specialoutput
415 % else
416 % do regular output...
417 % details for dealing with footnotes...
418 % \@specialoutput:
419 % switch \outputpenalty:
420 % case -10001: \@doclearpage
421 % case -10004: set box \@holdpg := \vbox{\unvbox255}
422 % case -10002 or -10003:
423 % set box \@holdpg := \vbox{\unvbox\@holdpg \unvbox255}
424 % let \@pageht := \ht\@holdpg, \@pagedp := \dp\@holdpg
425 % \unvbox\@holdpg
426 % pop \@currbox off of \@currlist
427 % \@addmarginpar (assuming \count\@currbox <= 0)
428 % \@addmarginpar:
429 % pop \@marbox off of \@currlist
430 % free \@currbox and \@marbox back to \@freelist
431 % if left-hand margin then let \@marbox := \@currbox
432 % let \@tempdima := \@mparbottom - \@pageht + \ht\@marbox
433 % if \@tempdima < 0 then let \@tempdima := 0
434 % let \@mparbottom := \@pageht + \@tempdima + \dp\@marbox + \marginparpush
435 % decrement \@tempdima := \@tempdima - \ht\@marbox
436 % prepend \vskip\@tempdima to \@marbox
437 % let \ht\@marbox := \dp\@marbox := 0
438 % \kern -\@pagedp, \nointerlineskip
439 % set an \hbox to \columnwidth (zero height/depth):
440 % attach \@marbox to correct margin
441 % set a \vbox with height 0 and depth \@pagedp
442 % \end{verbatim}
443 %
444 % We see from here that \cs{@addmarginpar} is the place where {\LaTeX}
445 % does the work of calculating the current page position and where the
446 % next note should go, and then actually puts it there. We will need
447 % to completely replace this routine, but can leave everything else
448 % as is.
449 %
450 % \subsection{\cs{output}}
451 % While \LaTeX's margin routines end with \cs{@addmarginpar}, we must
452 % dig even deeper to apply our patch, since we need to insert some
453 % code to run during the \emph{main} output routine that ships out
454 % each page. Thus, we'll expand ``\texttt{do regular output...}''
455 % from the previous \cs{output} listing.
456 % \begin{verbatim}
457 % do regular output...:
458 % \@makecol
459 % do { \@opcol \@startcolumn } while @fcolmade
460 % \@makecol:
461 % set box \@outputbox := box255 (plus any footnotes)
462 % let \@freelist := \@freelist + \@midlist, \@midlist := \@empty
463 % \@combinefloats
464 % add \@texttop and \@textbottom to \@outputbox (default no-op)
465 % \@opcol:
466 % \@outputpage (or \@outputdblcol in twocolumn mode)
467 % let \@mparbottom := \@textfloatsheight := 0
468 % \@floatplacement
469 % \@startcolumn:
470 % try to make a float column from \@deferlist, setting @fcolmade
471 % if !@fcolmade then add floats from \@deferlist to next column
472 % \@combinefloats:
473 % aggregate \@toplist floats into a box and prepend to \@outputbox
474 % aggregate \@botlist floats into a box and append to \@coutputbox
475 % free inserts from \@toplist and \@botlist
476 % \@outputpage:
477 % ship out the page
478 % reset a bunch of stuff
479 % let \@colht := \textheight (in \@outputpage)
480 % \end{verbatim}
481 %
482 % We've seen two main times when action occurs: callout time and
483 % shipout time. We proceed chronologically with our patches.
484 %
485 % \section{Callout-time patches}
486 % \begin{macros}{\@addmarginpar}
487 % The first thing we must modify is that at callout time, we need to
488 % get the inserts into \cs{mfx@marginlist}. This should happen in the
489 % output routine so that we can get ahold of the current page
490 % position. Even if we have a better idea of the page position
491 % (e.g. from pdf\TeX), we still might as well do this in the OR.
492 % In addition to actually setting the margin note, we also use this
493 % routine to inject arbitrary code into the OR (see \cs{MFX@inject}).
494 % \begin{macrocode}
495 \def\@addmarginpar{%
496 \@next\@marbox\@currlist{}\MFX@AssertionError
497 %<debug>\MFX@debug{addmarginpar (running insert) \@marbox/ \@currbox at
498 %<debug> \the\c@page:\the\@pageht, marginlist=\MFX@mac\mfx@marginlist}%
499 %<debug>\MFX@debug{addmarginpar outputpenalty=\the\outputpenalty}%
500 \MFX@getypos
501 \expandafter\ifx\@marbox\Mfx@inject@insert
502 \mfx@injected\global\let\mfx@injected\@empty
503 \else
504 \MFX@cons\mfx@marginlist{%
505 \noexpand\mfx@build@note\@currbox\@marbox{\mfx@ypos}%
506 \noexpand\mfx@build@skip{\the\marginparpush}%
507 }%
508 \fi
509 %<debug>\MFX@debug{addmarginpar (exit): marginlist=\MFX@mac\mfx@marginlist}%
510 }
511 % \end{macrocode}
512 % \end{macros}
513 %
514 % \begin{macros}{\MFX@cons,\MFX@snoc}
515 % In passing we'll define the cons macro, which fully-expands
516 % its second argument, but makes sure to only expand the first
517 % one once, so that any fragile control sequences in it are
518 % correctly protected. We also define snoc, which prepends.
519 % Note that we could put the \cs{temp@} definition into a group
520 % if it was really gonna matter\ldots
521 % \begin{macrocode}
522 \def\MFX@cons#1#2{%
523 \edef\temp@{#2}%
524 \expandafter\expandafter\expandafter\gdef
525 \expandafter\expandafter\expandafter#1%
526 \expandafter\expandafter\expandafter{\expandafter#1\temp@}%
527 }
528
529 \def\MFX@snoc#1#2{%
530 \edef\temp@{#2}%
531 \expandafter\expandafter\expandafter\gdef
532 \expandafter\expandafter\expandafter#1%
533 \expandafter\expandafter\expandafter{\expandafter\temp@#1}%
534 }
535 % \end{macrocode}
536 % \end{macros}
537 %
538 % \begin{macros}{\MFX@run@clear}
539 % Finally, \cs{MFX@run@clear} is a quick trick to expand the
540 % contents of a macro and then clear it (to \cs{@empty}) before
541 % any of its tokens are consumed.
542 % \begin{macrocode}
543 \def\MFX@run@clear#1{%
544 \expandafter\global\expandafter\let\expandafter#1\expandafter\@empty#1%
545 }
546 % \end{macrocode}
547 % \end{macros}
548 %
549 % \begin{macros}{\MFX@inject}
550 % As mentioned earlier, \cs{@addmarginpar} is also a hook for injecting
551 % arbitrary code into the output routine, i.e. to get a vertical position
552 % for blocking the margin. We define \cs{MFX@inject} to facilitate this.
553 % \begin{macrocode}
554 \def\MFX@inject#1{
555 \expandafter\def\expandafter\@freelist\expandafter{%
556 \expandafter\@elt\expandafter\Mfx@inject@insert
557 \expandafter\@elt\expandafter\Mfx@inject@insert
558 \@freelist}%
559 \expandafter\def\expandafter\mfx@injected\expandafter{\mfx@injected#1}%
560 \marginpar{}%
561 }
562 % \end{macrocode}
563 % \end{macros}
564 %
565 % \begin{macros}{\MFX@getypos,\mfx@ypos}
566 % We now need to settle on a way to determine the vertical position.
567 % Someday this may be an option, and will depend on a variety
568 % of factors. But for starters, we define the simplest version.
569 % Note the subtraction of \cs{Mfx@strutheight}. Ideally we would simply
570 % grab a copy of \cs{@holdpg} from the middle of \cs{@specialoutput}
571 % and then discard the last box to figure out what height we're really
572 % at, since \cs{@holdpg} includes the box from the line we're currently
573 % on, and we want to be level with the \emph{top} of that box, rather
574 % than the baseline. But since \cs{@holdpg} is accessible only deep
575 % within \cs{@specialoutput}, and it's not worth the risky job of
576 % performing surgery on it (which is unfortunately brittle if anyone
577 % else has a similar idea), we instead resort to this approximation.
578 % And since this should ultimately be only a fallback for when
579 % \cs{pdflastypos} isn't available, it's good enough.
580 % (NOTE: we might be able to use a \cs{vadjust} instead here?)
581 % \begin{macrocode}
582 \def\MFX@getypos{%
583 \dimen@\dimexpr\@pageht+\@pagedp+\marginposadjustment+\Mfx@mparshift\relax
584 \ifnum\outputpenalty=-10002\relax
585 \advance\dimen@-\Mfx@strutheight
586 \fi
587 \edef\mfx@ypos{\the\dimen@}%
588 \global\Mfx@mparshift\z@
589 }
590 % \end{macrocode}
591 % \end{macros}
592 %
593 % \begin{macros}{\marginpar,\Mfx@strutheight}
594 % We need to make sure \cs{Mfx@strutheight} gets defined somewhere,
595 % and the best time is probably right before the \cs{marginpar} does
596 % its work, since that will most likely ensure we're using the right
597 % font for the line.
598 % \begin{macrocode}
599 \newdimen\Mfx@strutheight
600 \edef\marginpar{%
601 \unexpanded{\setbox\@tempboxa\hbox{\strut}\Mfx@strutheight\ht\@tempboxa}%
602 \expandafter\unexpanded\expandafter{\marginpar}%
603 }
604 % \end{macrocode}
605 % \end{macros}
606 %
607 % \section{Shipout-time patches}
608 % \begin{macros}{\@combinefloats}
609 % We need to patch in somewhere before \cs{@combinefloats} at the latest,
610 % so that any heights calculated from \cs{@pageht} are correct---otherwise
611 % the top figures will confuse us. So we'll start by simply adding our
612 % own \cs{MFX@combinefloats@before} at the very beginning of \cs{@combinefloats}
613 % \begin{macrocode}
614 \expandafter\def\expandafter\@combinefloats\expandafter{\expandafter
615 \MFX@combinefloats@before\@combinefloats}
616 % \end{macrocode}
617 % \end{macros}
618 %
619 % \begin{macros}{\MFX@combinefloats@before}
620 % \cs{MFX@combinefloats@before} is then responsible for picking the
621 % needed notes from \cs{mfx@marginlist}, building them into a box, and
622 % attaching that box onto the correct side of \cs{@outputbox}. We also
623 % add any global \cs{marginheightadjustment} to \cs{Mfx@marginheight}
624 % before building the margin, and then reset it back to zero at the end.
625 % This allows any calls to \cs{extendmargin} during the page itself to
626 % work as expected.
627 % \begin{macrocode}
628 \def\MFX@combinefloats@before{%
629 \advance\Mfx@marginheight\marginheightadjustment
630 \MFX@buildmargin
631 \MFX@attachmargin
632 \global\Mfx@marginheight\z@
633 }
634 % \end{macrocode}
635 % \end{macros}
636 %
637 % \begin{macros}{\MFX@attachmargin}
638 % We'll start with the second half of \cs{MFX@combinefloats@before},
639 % since it's simpler. We need to do several things here.
640 % \begin{macrocode}
641 \def\MFX@attachmargin{%
642 %<debug>\MFX@debug{attachmargin}%
643 % \end{macrocode}
644 % We start by moving the reference point of \cs{Mfx@marginbox} to the top.
645 % \begin{macrocode}
646 %<debug>\MFX@debug{attachmargin: \MFX@htdp\@outputbox, \MFX@htdp\Mfx@marginbox}%
647 \setbox\Mfx@marginbox\vtop{%
648 \vskip\z@\unvbox\Mfx@marginbox}%
649 % \end{macrocode}
650 % Next we need to figure out which side of \cs{@outputbox} to attach
651 % the \cs{Mfx@marginbox} on. We now use \cs{columnwidth} instead of
652 % \cs{wd}\cs{@outputbox} to set the right-hand margins, since tufte-\LaTeX
653 % sometimes makes too-wide output boxes. If this becomes a problem,
654 % we'll need to consider making this configurable elsewhere. We should
655 % also pay attention to whether adding a box at the top of \cs{@outputbox}
656 % might have unintended consequences w.r.t. any glue being retained that
657 % should have been swallowed. This will require further investigation.
658 % \begin{macrocode}
659 \setbox\@outputbox\vbox{%
660 \begingroup
661 \setbox\@tempboxa\vbox{%
662 \hbox{%
663 \if\MFX@leftmargin
664 \llap{\box\Mfx@marginbox\hskip\marginparsep}%
665 \else
666 \hskip\columnwidth
667 \rlap{\hskip\marginparsep\box\Mfx@marginbox}%
668 \fi
669 }}%
670 \ht\@tempboxa\z@
671 \dp\@tempboxa\z@
672 \box\@tempboxa
673 \endgroup
674 \unskip
675 \unvbox\@outputbox
676 }%
677 }
678 % \end{macrocode}
679 % \end{macros}
680 %
681 % \begin{macros}{\MFX@buildmargin}
682 % When \cs{MFX@buildmargin} is called, we have a list of tokens in
683 % \cs{mfx@marginlist} that need to be processed: combinations of
684 % \cs{mfx@build@note}, \cs{mfx@build@skip}, and \cs{mfx@build@clear},
685 % with various parameters to indicate what material still needs to be
686 % set in the margin. This macro therefore must pull off the first $n>0$
687 % of these commands to set on the current page ($n$ must be positive to
688 % prevent infinite loops), and leave the rest to be deferred. We do not
689 % currently support taking notes out of order, though that is a possible
690 % feature to allow in the future, on an opt-in basis. The typeset
691 % material will be left in \cs{Mfx@marginbox}, which must have the same
692 % height as \cs{@outputbox} (although because we \cs{unvbox}\cs{@outputbox}
693 % in \cs{MFX@attachmargin}, we can't guarantee that this will correctly
694 % line up the notes with their callouts). This procedure happens in
695 % four passes. But first, we initialize \cs{Mfx@marginheight} to
696 % \cs{@colroom}, which is the height of the page minus any floats that
697 % have been added to the top or bottom (these floats may extend into the
698 % margins: in the future we may look into detecting this and using the
699 % whole page, with overwide floats blocked off as phantoms). We add
700 % \cs{@colroom} rather than assigning it because any global or per-page
701 % adjustments have already been added to \cs{Mfx@marginheight}. We
702 % can then close out any still-open margin pieces (this is the typical
703 % case, where the margin is not blocked across a page boundary, so that
704 % \cs{mfx@marginstart} will hold a position, rather than \cs{relax}).
705 % After this, \cs{Mfx@marginheight} is no longer necessary, so we reuse
706 % it for keeping track of available space in individual margin pieces.
707 % \begin{macrocode}
708 \def\MFX@buildmargin{%
709 \advance\Mfx@marginheight\@colroom
710 \ifx\mfx@marginstart\relax
711 \else
712 \MFX@cons\mfx@marginpieces{%
713 \noexpand\@elt{\mfx@marginstart}{\the\Mfx@marginheight}}%
714 \gdef\mfx@marginstart{0pt}%
715 \global\advance\Mfx@piece@count\@ne
716 \fi
717 %<debug>\MFX@debug{buildmargin: marginheight=\the\Mfx@marginheight,
718 %<debug> marginlist=\MFX@mac\mfx@marginlist,
719 %<debug> marginpieces=\MFX@mac\mfx@marginpieces}%
720 % \end{macrocode}
721 % We now execute the four passes. First is a global downward pass,
722 % whose purpose is to determine the maximum number of notes (and other
723 % material) that can fit in the margin, taking any phantoms into
724 % consideration. Every note identified by \cs{MFX@buildmargin@down}
725 % is guaranteed to show up on this page, so we free its inserts back
726 % to \cs{@freelist}. The second pass is the global upward pass, in
727 % which we determine the lowest possible margin piece each note may
728 % go into without causing lower notes to fall off the bottom. The
729 % third pass is the piecewise downward pass. For each piece, we
730 % figure out where in the piece each note will go by inserting
731 % compressible spaces between the notes. If a note is called out
732 % past the end of the piece and does not need to go into the piece
733 % (as determined by pass 2), it will be deferred to a later piece.
734 % The fourth pass is the piecewise upward pass, in which the compressible
735 % spaces are shrunk just enough to fit everything into the piece. The
736 % last two (piecewise) passes both occur in each piece before the next
737 % piece is addressed. The whole process is bypassed if there are
738 % no eligible margin pieces.
739 % \begin{macrocode}
740 \ifx\mfx@marginpieces\@empty\else
741 \MFX@buildmargin@down
742 \MFX@buildmargin@up
743 \MFX@buildmargin@pieces
744 \fi
745 }
746 % \end{macrocode}
747 % \end{macros}
748 %
749 % \subsection{First pass: global downward}
750 % \begin{macros}{\MFX@buildmargin@down,\mfx@pieceheights}
751 % The first step is the global ``down'' step, in which we move the
752 % notes that will fit on the current page into \cs{mfx@marginout} in
753 % reverse order (to prepare for the second, upward, pass), and anything
754 % that doesn't fit is deferred back into \cs{mfx@marginlist}. We do
755 % this by changing the meaning of \cs{mfx@build@note}, \cs{mfx@build@skip},
756 % and \cs{mfx@build@clear}, which delimit the different types of material
757 % in \cs{mfx@marginlist}. Note that as we continue processing, these
758 % macros will change from time to time (i.e. changing \cs{mfx@build@skip}
759 % to actually doing something once we find a note, rather than gobbling
760 % so as to remove skips at page boundaries; or changing them to save
761 % material back onto \cs{mfx@marginlist} once the margin fills up).
762 % The first thing we need to do is iterate over the piece positions
763 % to get the list of heights.
764 % \begin{macrocode}
765 \def\MFX@buildmargin@down{%
766 \let\mfx@pieceheights\@empty
767 \def\@elt##1##2{%
768 \MFX@cons\mfx@pieceheights{\noexpand\@elt{\the\dimexpr##2-##1}}}%
769 \mfx@marginpieces
770 \MFX@popdimen\Mfx@marginheight\mfx@pieceheights
771 % \end{macrocode}
772 % Now we run forwards over the \cs{mfx@marginlist} to actually
773 % operate on each thing in the margin.
774 % \begin{macrocode}
775 \let\mfx@build@note\MFX@margin@note@down
776 \let\mfx@build@skip\@gobble
777 \let\mfx@build@clear\MFX@build@clear@down
778 \let\mfx@marginout\@empty
779 \MFX@run@clear\mfx@marginlist
780 %<debug>\MFX@debug{buildmargin@down: RETURN
781 %<debug> marginout=\MFX@mac\mfx@marginout,
782 %<debug> marginlist=\MFX@mac\mfx@marginlist}%
783 }
784 % \end{macrocode}
785 % \end{macros}
786 %
787 % We now define the various |\MFX@margin@...@down| macros.
788 % At this stage in the game, the only difference between notes
789 % and skips is that we ignore skips before any notes by setting
790 % \cs{mfx@build@skip} initially to \cs{@gobble}. Once we've
791 % seen the first note, skips are treated exactly the same: as
792 % fixed-height material. If there is room in the current piece
793 % for the given height, then we prepend it to \cs{mfx@marginout},
794 % decrement the remaining height, and arrange for the boxes to
795 % be freed. If not, we unshift the next piece height from
796 % \cs{mfx@pieceheights} and try again, until \cs{mfx@pieceheights}
797 % is empty and we simply defer everything to later pages.
798 %
799 % \begin{macros}{\MFX@margin@note@down}
800 % Upon seeing a note, we must do several things:
801 % \begin{enumerate}
802 % \item determine which box (left or right) is needed for the
803 % current page, by calling \cs{MFX@whichbox}
804 % \item if the box fits, free both boxes, prepend \cs{mfx@marginout}
805 % with a call to \cs{mfx@build@note}, and re-enable skips
806 % \item otherwise, defer the current note and all future notes
807 % \end{enumerate}
808 % The latter two steps are taken care of by \cs{MFX@margin@fit},
809 % which takes the height and two blocks of material: one to prepend
810 % to \cs{mfx@marginout} if it fits, the other to append to
811 % \cs{mfx@marginlist} if it doesn't.
812 % \begin{macrocode}
813 \def\MFX@margin@note@down#1#2#3{%
814 %<debug>\MFX@debug{margin@note@down: ENTRY: #1/ #2 at #3}%
815 \MFX@whichbox\@marbox#1#2%
816 \if\MFX@check@fit{}{\ht\@marbox+\dp\@marbox}%
817 \MFX@snoc\mfx@marginout{%
818 \noexpand\@cons\noexpand\@freelist#1%
819 \noexpand\@cons\noexpand\@freelist#2%
820 \noexpand\mfx@build@note\@marbox{#3}}%
821 \let\mfx@build@skip\MFX@margin@skip@down
822 \else
823 \mfx@build@clear
824 \mfx@build@note{#1}{#2}{#3}%
825 \fi
826 }
827 % \end{macrocode}
828 % \end{macros}
829 %
830 % \begin{macros}{\MFX@margin@skip@down}
831 % Skips are similar. A skip needs only to save itself back into
832 % \cs{mfx@marginout}, provided it fits. If not, there is no need
833 % to defer it because it will just get gobbled at the top of the
834 % next page anyway.
835 % \begin{macrocode}
836 \def\MFX@margin@skip@down#1{%
837 %<debug>\MFX@debug{margin@skip@down #1}%
838 \if\MFX@check@fit{}{#1}%
839 \MFX@snoc\mfx@marginout{\noexpand\mfx@build@skip{#1}}%
840 \else
841 \mfx@build@clear
842 \fi
843 }
844 % \end{macrocode}
845 % \end{macros}
846 %
847 % \begin{macros}{\MFX@margin@clear@down}
848 % Finally, \cs{MFX@margin@clear@down} is the only place we actually
849 % need to handle full-margin clears, since the downward pass does not
850 % ever push \cs{mfx@build@clear} onto \cs{mfx@marginout}. When we
851 % see this, we simply redefine all three commands to append themselves
852 % back to \cs{mfx@marginlist}.
853 % \begin{macrocode}
854 \def\MFX@build@clear@down{%
855 %<debug>\MFX@debug{clear@down}%
856 \def\mfx@build@note##1##2##3{%
857 \MFX@cons\mfx@marginlist{\noexpand\mfx@build@note##1##2{\MFX@minus@inf}}}%
858 \def\mfx@build@skip##1{%
859 \MFX@cons\mfx@marginlist{\noexpand\mfx@build@skip{##1}}}%
860 \def\mfx@build@clear{%
861 \MFX@cons\mfx@marginlist{\noexpand\mfx@build@clear}}%
862 }
863 % \end{macrocode}
864 % \end{macros}
865 %
866 % \begin{macros}{\MFX@check@fit}
867 % We factored out some of the common functionality between the
868 % note and skip routines, so that must now be defined. The
869 % \cs{MFX@check@fit} macro acts as a conditional and should be
870 % used as \cs{if}\cs{MFX@check@fit}\marg{piece-hook}\marg{size}.
871 % It takes care of iterating through the list of heights and
872 % accumulating the total size of material encountered so far.
873 % The \emph{piece-hook} is executed each time a new piece height
874 % is popped.
875 % \begin{macrocode}
876 \def\MFX@check@fit#1#2{%
877 00\fi % close out the \if
878 %<debug>\MFX@debug{check@fit{\unexpanded{#1}}{#2=\the\dimexpr#2} ENTRY:
879 %<debug> marginheight=\the\Mfx@marginheight}%
880 \@tempswafalse
881 \ifdim\dimexpr#2<\Mfx@marginheight % it fits
882 \advance\Mfx@marginheight-\dimexpr#2\relax % deduct the size
883 \@tempswatrue
884 \else % didn't fit: check the next piece
885 %<debug>\MFX@debug{check@fit overflow: pieceheights=\MFX@mac\mfx@pieceheights}%
886 \ifx\mfx@pieceheights\@empty\else % make sure there's anything there
887 #1%
888 \MFX@popdimen\Mfx@marginheight\mfx@pieceheights
889 \if\MFX@check@fit{#1}{#2}\fi
890 \fi
891 \fi
892 %<debug>\MFX@debug{check@fit RETURN \meaning\if@tempswa:
893 %<debug> marginheight=\the\Mfx@marginheight,}%
894 \if@tempswa % start a new \if
895 }
896 % \end{macrocode}
897 % \end{macros}
898 % \begin{macros}{\MFX@popdimen}
899 % Here is a quick convenience routine. \cs{MFX@popdimen}\marg{dimen}%
900 % \marg{list} removes the first dimension from \emph{list} and stores
901 % it into \emph{dimen}.
902 % \begin{macrocode}
903 \def\MFX@popdimen#1#2{%
904 \def\@elt##1{%
905 #1##1\relax
906 \def\@elt####1{%
907 \MFX@cons#2{\noexpand\@elt{####1}}%
908 }%
909 }%
910 \MFX@run@clear#2%
911 }
912 % \end{macrocode}
913 % \end{macros}
914 %
915 % \begin{macros}{\MFX@whichbox}
916 % We also need to determine which box should be used, since they
917 % may have different heights. The macro \cs{MFX@whichbox}%
918 % \marg{target-box}\marg{left-box}\marg{right-box} checks which margin
919 % we're setting and stores the correct box into \emph{target-box}.
920 % Note that \emph{target-box} must be a single control sequence.
921 % \begin{macrocode}
922 \def\MFX@whichbox#1#2#3{%
923 \if\MFX@leftmargin
924 \def#1{#2}%
925 \else
926 \def#1{#3}%
927 \fi
928 %<debug>\MFX@debug{whichbox: \@marbox (\the\dimexpr\ht#1+\dp#1)}%
929 }
930 % \end{macrocode}
931 % \end{macros}
932 %
933 % \begin{macros}{\MFX@leftmargin}
934 % And here is the logic to figure out which margin we're in, based on
935 % the page number and other flags. This is another conditional-like
936 % macro, and should be used after an \cs{if}, as in
937 % \cs{if}\cs{MFX@leftmargin}\ldots\cs{else}\ldots\cs{fi}.
938 %
939 % This is different from the corresponding code in the {\LaTeX}
940 % routines because we don't support double columns. In addition, we
941 % would ideally allow \cs{if@reversemargin} to work on a per-note
942 % basis (i.e. at callout time) but we also need something working at
943 % shipout time so we can figure out which margin to use. Thus, until
944 % we figure out how to use multiple margins, this will have to do.
945 % \begin{macrocode}
946 \def\MFX@leftmargin{%
947 00\fi % close out the \if
948 \@tempcnta\@ne
949 \if@mparswitch
950 \unless\ifodd\c@page
951 \@tempcnta\m@ne
952 \fi
953 \fi
954 \if@reversemargin
955 \@tempcnta-\@tempcnta
956 \fi
957 %<debug>\MFX@debug{margin on \ifnum\@tempcnta<\z@ left\else right\fi}%
958 \ifnum\@tempcnta<\z@ % start a new \if
959 }
960 % \end{macrocode}
961 % \end{macros}
962 %
963 % \begin{macros}{\MFX@minus@inf}
964 % Finally, note that when deferring notes to the next page, we
965 % adjust their position to the top of the page, rather than the
966 % callout position. This is a large negative dimension (near
967 % \TeX's maximum), but we may reconsider making this zero or even
968 % a small positive amount, since there seems to be a small amount
969 % of space before the first paragraph in normal text, though I'm
970 % not sure where that comes from.
971 % \begin{macrocode}
972 \def\MFX@minus@inf{-4000\p@}
973 % \end{macrocode}
974 % \end{macros}
975 %
976 % \subsection{Second pass: global upward}
977 % \begin{macros}{\MFX@buildmargin@up,\mfx@phantomheights}
978 % The next step is the global ``up'' step, in which we figure out the
979 % lowest piece a note can possibly occupy (without pushing later notes
980 % off the bottom) and add this information to the \cs{mfx@build@note} in
981 % \cs{mfx@marginout}. We start similar to \cs{MFX@buildmargin@down},
982 % except we need the list of heights to be backwards. We also need a
983 % list of phantom heights, in order to handle skips properly, which we
984 % intersperse as negative heights.
985 % \begin{macrocode}
986 \def\MFX@buildmargin@up{%
987 %<debug>\MFX@debug{buildmargin@up: ENTRY
988 %<debug> marginpieces=\MFX@mac\mfx@marginpieces,
989 %<debug> marginout=\MFX@mac\mfx@marginout}%
990 \let\mfx@pieceheights\@empty
991 \let\mfx@phantomheights\@empty
992 \let\temp@@\relax
993 \def\@elt##1##2{%
994 %<debug>\MFX@debug{ -> piece (##1,##2), temp@@=\meaning\temp@@}%
995 \MFX@snoc\mfx@pieceheights{\noexpand\@elt{\the\dimexpr##2-##1}}%
996 \ifx\temp@@\relax\else
997 %<debug>\MFX@debug{ -> phantom (\temp@@,##1)}%
998 \MFX@snoc\mfx@phantomheights{\noexpand\@elt{\the\dimexpr##1-\temp@@}}%
999 \fi
1000 \def\temp@@{##2}%
1001 }%
1002 \mfx@in@phantomfalse
1003 \mfx@marginpieces
1004 % \end{macrocode}
1005 % The piece counter, \cs{Mfx@piece@count} has not been touched yet,
1006 % so now we will start decrementing it for each piece to keep track
1007 % of where we are. Note that \cs{mfx@marginout} will never contain
1008 % \cs{mfx@build@clear} so we don't need to reassign it. Since we
1009 % don't do any deferrals here, we don't need to empty out a new target
1010 % list. Instead, we operate ``in-place'' in \cs{mfx@marginout}, after
1011 % popping off the first height.
1012 % \begin{macrocode}
1013 \MFX@popdimen\Mfx@marginheight\mfx@pieceheights
1014 \let\mfx@build@note\MFX@margin@note@up
1015 \let\mfx@build@skip\@gobble
1016 \MFX@run@clear\mfx@marginout
1017 %<debug>\MFX@debug{buildmargin@up: RETURN marginout=\MFX@mac\mfx@marginout}%
1018 }
1019 % \end{macrocode}
1020 % \end{macros}
1021 %
1022 % \begin{macros}{\MFX@margin@note@up}
1023 % We must again define the specific behavior of each build command.
1024 % These macros simply reuse \cs{MFX@check@fit}, but ask it to
1025 % decrement the piece counter when a piece runs out of space.
1026 % Aside from that, the only thing that actually happens here is
1027 % that we append the current piece to each note, and also end
1028 % up reversing the contents by again prepending everything.
1029 % We are guaranteed that \cs{MFX@check@fit} will never fail.
1030 % Since we cannot put notes in a phantom, we start by ensuring
1031 % we're not in one.
1032 % \begin{macrocode}
1033 \def\MFX@margin@note@up#1#2{%
1034 %<debug>\MFX@debug{margin@note@up: #1at #2, marginheight=\the\Mfx@marginheight}%
1035 \ifmfx@in@phantom
1036 \MFX@popdimen\Mfx@marginheight\mfx@pieceheights
1037 \advance\Mfx@piece@count\m@ne
1038 \mfx@in@phantomfalse
1039 \fi
1040 % \end{macrocode}
1041 % Now we're guaranteed to be in a piece, rather than a phantom, we
1042 % look for a piece that can fit this note, making sure to decrement
1043 % the piece count and pop off a phantom for each new piece we check.
1044 % Once it's found, we add the note back to \cs{mfx@marginout} with
1045 % the correct piece.
1046 % \begin{macrocode}
1047 \if\MFX@check@fit{\advance\Mfx@piece@count\m@ne
1048 \MFX@popdimen\dimen@\mfx@phantomheights}{\ht#1+\dp#1}%
1049 \MFX@snoc\mfx@marginout{%
1050 \noexpand\mfx@build@note{#1}{#2}{\the\Mfx@piece@count}}%
1051 \let\mfx@build@skip\MFX@margin@skip@up
1052 \else\MFX@AssertionError\fi
1053 }
1054 % \end{macrocode}
1055 % \end{macros}
1056 %
1057 % \begin{macros}{\MFX@margin@skip@up}
1058 % Skips are similar, but we have the added complication of handling
1059 % margin phantoms. When we cross between phantom and piece, we split
1060 % the skip so that we can use the simplest recursion possible.
1061 % \begin{macrocode}
1062 \def\MFX@margin@skip@up#1{%
1063 %<debug>\MFX@debug{margin@skip@up: #1}%
1064 \dimen@#1\relax
1065 \advance\Mfx@marginheight-\dimen@
1066 \ifdim\Mfx@marginheight<\z@
1067 % \end{macrocode}
1068 % This skip was bigger than the piece, so we need to split this skip,
1069 % adding the overflow back, and try again. Since we're done with this
1070 % piece, we'll pop the next one and recurse on whatever's left. This
1071 % looks slightly different depending on whether or not we're in a phantom.
1072 % \begin{macrocode}
1073 \advance\dimen@\Mfx@marginheight
1074 \MFX@snoc\mfx@marginout{%
1075 \noexpand\mfx@build@skip{\the\dimen@}{\the\Mfx@piece@count}}%
1076 \dimen@-\Mfx@marginheight
1077 \ifmfx@in@phantom
1078 \MFX@popdimen\Mfx@marginheight\mfx@pieceheights
1079 \advance\Mfx@piece@count\m@ne
1080 \mfx@in@phantomfalse
1081 \else
1082 \MFX@popdimen\Mfx@marginheight\mfx@phantomheights
1083 \mfx@in@phantomtrue
1084 \fi
1085 \mfx@build@skip\dimen@
1086 \else
1087 % \end{macrocode}
1088 % This skip fit entirely within the phantom, so we simply emit it.
1089 % \begin{macrocode}
1090 \MFX@snoc\mfx@marginout{%
1091 \noexpand\mfx@build@skip{\the\dimen@}{\the\Mfx@piece@count}}%
1092 \fi
1093 }
1094 % \end{macrocode}
1095 % \end{macros}
1096 %
1097 % \subsection{Margin pieces}
1098 % \begin{macros}{\MFX@buildmargin@pieces}
1099 % Before we can start the third and fourth passes, we need to set up
1100 % a loop over the pieces so that each piece can do these passes at
1101 % one time. In case we didn't use up all the pieces in the second
1102 % phase, we'll reset \cs{Mfx@piece@count} to zero. We also reset
1103 % \cs{Mfx@marginspace} and \cs{Mfx@marginbox}.
1104 % \begin{macrocode}
1105 \def\MFX@buildmargin@pieces{%
1106 \Mfx@piece@count\z@
1107 \Mfx@marginspace\z@
1108 \setbox\Mfx@marginbox\vbox{\vskip\z@}% TODO - do we need this?
1109 % \end{macrocode}
1110 % Now we run over the individual margin pieces, clearing it out as we
1111 % build up the contents of \cs{Mfx@marginbox}. Once we're done with
1112 % that, we need to do a bit of clean-up before finishing.
1113 % \begin{macrocode}
1114 \let\@elt\MFX@buildmargin@piece
1115 \MFX@run@clear\mfx@marginpieces
1116 \let\@elt\relax
1117 \global\Mfx@piece@count\z@
1118 }
1119 % \end{macrocode}
1120 % \end{macros}
1121 %
1122 % \begin{macros}{\MFX@buildmargin@piece}
1123 % The \cs{MFX@buildmargin@piece} macro is called for each piece of
1124 % the margin, and is passed the top and bottom positions of the piece.
1125 % Here we need to do a few things. First, if the output so far is
1126 % smaller than the top of the piece (this is generally true, except
1127 % sometimes for the first piece) then we need to insert padding into
1128 % \cs{Mfx@marginbox} before continuing. We accumulate this padding
1129 % into \cs{Mfx@marginspace}, which allows skips to do double-duty
1130 % across phantoms.
1131 % \begin{macrocode}
1132 \def\MFX@buildmargin@piece#1#2{%
1133 %<debug>\MFX@debug{buildmargin@piece: ENTRY (#1, #2)}%
1134 \ifdim\ht\Mfx@marginbox<#1\relax
1135 \dimen@\dimexpr#1-\ht\Mfx@marginbox\relax
1136 %<debug>\MFX@debug{buildmargin@piece: padding \the\dimen@}%
1137 \setbox\Mfx@marginbox\vbox{%
1138 \unvbox\Mfx@marginbox
1139 \vskip\dimen@
1140 }%
1141 \advance\Mfx@marginspace\dimen@
1142 \fi
1143 % \end{macrocode}
1144 % Now that \cs{Mfx@marginbox} has been padded, we proceed to set
1145 % things up for our down and up passes, and then run them to build
1146 % \cs{Mfx@piece@content}.
1147 % \begin{macrocode}
1148 \Mfx@marginpos@min#1\relax
1149 \Mfx@marginpos@max#1\relax
1150 \Mfx@marginheight#2\relax
1151 \advance\Mfx@piece@count\@ne
1152 \MFX@buildpiece@down
1153 \MFX@buildpiece@up
1154 % \end{macrocode}
1155 % Once \cs{Mfx@piece@content} has been built, we append it to the
1156 % \cs{Mfx@marginbox}.
1157 % \begin{macrocode}
1158 \setbox\Mfx@marginbox\vbox{%
1159 \unvbox\Mfx@marginbox
1160 \box\Mfx@piece@content
1161 \vskip\z@
1162 }%
1163 }
1164 % \end{macrocode}
1165 % \end{macros}
1166 %
1167 % \subsection{Third pass: piecewise downward}
1168 % \begin{macros}{\MFX@buildpiece@down,\mfx@pieceout}
1169 % The next pass is another downward pass. This is very similar
1170 % to the first (global downward) pass, except we're now moving
1171 % the first several notes from \cs{mfx@marginout} into
1172 % \cs{mfx@pieceout} (again, inverting the order) and deferring
1173 % the rest back into \cs{mfx@marginout}.
1174 % \begin{macrocode}
1175 \def\MFX@buildpiece@down{%
1176 %<debug>\MFX@debug{buildpiece@down: ENTRY piece=\the\Mfx@piece@count,
1177 %<debug> marginpos=(\the\Mfx@marginpos@min, \the\Mfx@marginpos@max),
1178 %<debug> marginspace=\the\Mfx@marginspace,
1179 %<debug> marginout=\MFX@mac\mfx@marginout}%
1180 \let\mfx@build@note\MFX@piece@note@down
1181 \let\mfx@build@skip\MFX@piece@skip@down
1182 \let\mfx@pieceout\@empty
1183 \MFX@run@clear\mfx@marginout
1184 %<debug>\MFX@debug{buildpiece@down: RETURN pieceout=\MFX@mac\mfx@pieceout,
1185 %<debug> marginout=\MFX@mac\mfx@marginout}%
1186 }
1187 % \end{macrocode}
1188 % \end{macros}
1189 %
1190 % \begin{macros}{\MFX@piece@note@down}
1191 % We again define each of our building macros. First, the note builder.
1192 % When we encounter a note, we first zero out \cs{Mfx@marginspace}. Then
1193 % we need to decide whether to put it in the current piece, or whether
1194 % to defer it to the next piece, based on a few signals. A note will only
1195 % be deferred for one of two reasons. In either case, we store the decision
1196 % to defer in |@tempswa|, so we'll start by clearing it.
1197 % \begin{macrocode}
1198 \def\MFX@piece@note@down#1#2#3{%
1199 %<debug>\MFX@debug{piece@note@down: ENTRY: #1at #2, lowest=#3,
1200 %<debug> marginpos=(\the\Mfx@marginpos@min,\the\Mfx@marginpos@max,
1201 %<debug> marginspace=\the\Mfx@marginspace}%
1202 \Mfx@marginspace\z@
1203 \@tempswafalse
1204 % \end{macrocode}
1205 % The first reason to defer is if its callout position (|#2|)
1206 % is beneath the end of the current piece (\cs{Mfx@marginheight}) \emph{and}
1207 % if its lowest-possible-piece (|#3|, as computed in the second pass) is
1208 % \emph{after} the current one (\cs{Mfx@piece@count}).
1209 % \begin{macrocode}
1210 \ifdim#2>\Mfx@marginheight
1211 \ifnum#3>\Mfx@piece@count
1212 \@tempswatrue
1213 \fi
1214 \fi
1215 % \end{macrocode}
1216 % The second possibility is that we've run out of room in this piece.
1217 % In this case, there's no need to check |#3|, since that number was
1218 % computed assuming everything that fit was as low as possible.
1219 % \begin{macrocode}
1220 \ifdim\dimexpr\ht#1+\dp#1+\Mfx@marginpos@min>\Mfx@marginheight
1221 \@tempswatrue
1222 \fi
1223 % \end{macrocode}
1224 % If a note is deferred, then we push it and everything after it
1225 % back onto \cs{mfx@marginout}.
1226 % \begin{macrocode}
1227 \if@tempswa
1228 %<debug>\MFX@debug{piece@note@down: clearing margin}%
1229 \MFX@piece@clear
1230 \mfx@build@note{#1}{#2}{#3}%
1231 \else
1232 % \end{macrocode}
1233 % Otherwise, we have decided the note is going into this piece. In this
1234 % case, we now need to check if any compressible space is needed above it.
1235 % In order to get better alignment for deferred notes, we check for the
1236 % case that the current position is zero and the note's callout position
1237 % is \cs{MFX@minus@inf}. In this case, we change the callout position
1238 % to instead be the greater of 0 or $\cs{topskip}-\cs{ht}\#1$.
1239 % \begin{macrocode}
1240 \dimen@#2\relax
1241 \ifdim\dimen@=\MFX@minus@inf
1242 \ifdim\Mfx@marginpos@max=\z@
1243 \dimen@\topskip
1244 \advance\dimen@-\ht#1\relax
1245 \ifdim\dimen@<\z@ \dimen@\z@ \fi
1246 \fi
1247 \fi
1248 \advance\dimen@-\Mfx@marginpos@max
1249 \ifdim\dimen@>\z@
1250 %<debug>\MFX@debug{piece@note@down: adding compressible \the\dimen@}%
1251 \MFX@snoc\mfx@pieceout{\noexpand\mfx@build@compressible{\the\dimen@}}%
1252 \advance\Mfx@marginpos@max\dimen@
1253 \fi
1254 % \end{macrocode}
1255 % After (maybe) adding the compressible space, we now add the
1256 % (incompressible) box itself, and accumulate its height in both position
1257 % registers. We no longer need the piece index or the position, so we
1258 % only store the box itself here.
1259 % \begin{macrocode}
1260 \MFX@snoc\mfx@pieceout{\noexpand\mfx@build@note{#1}}%
1261 \advance\Mfx@marginpos@min\dimexpr\ht#1+\dp#1\relax
1262 \advance\Mfx@marginpos@max\dimexpr\ht#1+\dp#1\relax
1263 \fi
1264 }
1265 % \end{macrocode}
1266 % \end{macros}
1267 %
1268 % \begin{macros}{\MFX@piece@skip@down}
1269 % Skips are a bit more complicated now. We no longer gobble the
1270 % initial skips (since the skips at the top and bottom of the page
1271 % have already been eaten). Instead, we need to look at
1272 % \cs{Mfx@marginspace}: if it's nonzero, we substract it from the
1273 % skip length before adding it incompressibly (if there's any left).
1274 % While we have piece number information here, we just pass it on
1275 % to the upward pass, which can choose to ``late-defer'' initial
1276 % (i.e. the bottom-most) skips in a piece.
1277 % \begin{macrocode}
1278 \def\MFX@piece@skip@down#1#2{%
1279 \dimen@#1\relax
1280 \ifdim\Mfx@marginspace>\z@
1281 \advance\dimen@-\Mfx@marginspace
1282 \ifdim\dimen@<\z@ \dimen@\z@ \fi
1283 \advance\Mfx@marginspace-\dimen@
1284 \fi
1285 % \end{macrocode}
1286 % At this point, \cs{dimen@} now stores any further incompressible
1287 % skip we need to add, which is now relative to the top of this piece.
1288 % In that case (note that \cs{Mfx@marginspace} is now necessarily zero),
1289 % we need to check that it actually fits in the current piece, and if
1290 % not, defer it.
1291 % \begin{macrocode}
1292 \ifdim\dimen@>\z@
1293 \ifdim\dimexpr#1+\Mfx@marginpos@min>\Mfx@marginheight
1294 \MFX@piece@clear
1295 \mfx@build@skip{\the\dimen@}{#2}%
1296 \else
1297 % \end{macrocode}
1298 % If the skip \emph{does} fit, we need to add it to \cs{mfx@pieceout}
1299 % and advance the position registers.
1300 % \begin{macrocode}
1301 \MFX@snoc\mfx@pieceout{\noexpand\mfx@build@skip{\the\dimen@}{#2}}%
1302 \advance\Mfx@marginpos@min\dimen@
1303 \advance\Mfx@marginpos@max\dimen@
1304 \fi
1305 \fi
1306 }
1307 % \end{macrocode}
1308 % \end{macros}
1309 %
1310 % \begin{macros}{\MFX@piece@clear}
1311 % Finally, we need to handle the case of deferring material. By
1312 % analogy with the previous two passes, we'll continue to refer to
1313 % this as clearing. In this case, we need to redefine the note and
1314 % skip macros to save themselves back to \cs{mfx@marginout}.
1315 % \begin{macrocode}
1316 \def\MFX@piece@clear{%
1317 %<debug>\MFX@debug{piece@clear}%
1318 \def\mfx@build@note##1##2##3{%
1319 \MFX@cons\mfx@marginout{\noexpand\mfx@build@note##1{##2}{##3}}}%
1320 \def\mfx@build@skip##1##2{%
1321 \MFX@cons\mfx@marginout{\noexpand\mfx@build@skip{##1}{##2}}}%
1322 }
1323 % \end{macrocode}
1324 % \end{macros}
1325 %
1326 % \subsection{Fourth pass: piecewise upward}
1327 % \begin{macros}{\MFX@buildpiece@up}
1328 % We are now ready for the final pass, where we take the reversed
1329 % list of notes for this piece and stack them up, compressing as much
1330 % compressible space as necessary to make them all fit. Since this
1331 % is the last pass, we can finally prepend all the boxes into
1332 % \cs{Mfx@piece@content}. Because it is still possible for pieces
1333 % to have skips at the bottom, we need to worry about which piece
1334 % these skips belong in. Since we've passed forward the lowest-piece
1335 % information from pass 2, we can choose at the last possible moment
1336 % to defer the bottom-most skips of a piece to the next piece (by
1337 % prepending it to \cs{mfx@marginout}. Note that compressible spaces
1338 % will \emph{never} be at the beginning of \cs{mfx@pieceout}. We
1339 % store the excess space in \cs{Mfx@marginheight}.
1340 % \begin{macrocode}
1341 \def\MFX@buildpiece@up{%
1342 \Mfx@marginheight\dimexpr\Mfx@marginpos@max-\Mfx@marginheight\relax
1343 \ifdim\Mfx@marginheight<\z@\Mfx@marginheight\z@\fi
1344 %<debug>\MFX@debug{buildpiece@up: excess=\the\Mfx@marginheight}%
1345 \let\mfx@build@note\MFX@piece@note@up
1346 \let\mfx@build@compressible\MFX@piece@compressible@up
1347 \let\mfx@build@skip\MFX@piece@skip@maybedefer
1348 \MFX@run@clear\mfx@pieceout\relax
1349 }
1350 % \end{macrocode}
1351 % \end{macros}
1352 %
1353 % \begin{macros}{\MFX@piece@skip@maybedefer}
1354 % As mentioned earlier, the initial skips in a piece may be deferred
1355 % to later pieces, provided they can possibly go there. Here we
1356 % check the |#2| argument against \cs{Mfx@piece@count} and defer if
1357 % it is larger. Otherwise, we switch back to the standard behavior
1358 % defined in \cs{MFX@piece@skip@up}. Deferred skips do not need to
1359 % be subtracted from the excess because they were never compressible
1360 % in the first place.
1361 % \begin{macrocode}
1362 \def\MFX@piece@skip@maybedefer#1#2{%
1363 \ifnum#2>\Mfx@piece@count
1364 %<debug>\MFX@debug{piece@skip deferring: #1, #2 (\the\Mfx@piece@count)}%
1365 \MFX@snoc\mfx@marginout{\noexpand\mfx@build@skip{#1}{#2}}%
1366 \else
1367 \let\mfx@build@skip\MFX@piece@skip@up
1368 \mfx@build@skip{#1}{#2}%
1369 \fi
1370 }
1371 % \end{macrocode}
1372 % \end{macros}
1373 %
1374 % \begin{macros}{\MFX@piece@note@up}
1375 % Now that we've taken care of late deferrals, we can define the
1376 % standard behavior without worrying as much about that.
1377 % These macros finally set their contents into \cs{Mfx@piece@content},
1378 % as well as reset \cs{mfx@build@skip} back to its standard value.
1379 % \begin{macrocode}
1380 \def\MFX@piece@note@up#1{%
1381 %<debug>\MFX@debug{piece@note@up: #1}%
1382 \setbox\Mfx@piece@content\vbox{%
1383 \box#1%
1384 \unvbox\Mfx@piece@content}%
1385 \let\mfx@build@skip\MFX@piece@skip@up
1386 }
1387 % \end{macrocode}
1388 % \end{macros}
1389 % \begin{macros}{\MFX@piece@skip@up}
1390 % Skips are also straightforward.
1391 % \begin{macrocode}
1392 \def\MFX@piece@skip@up#1#2{%
1393 %<debug>\MFX@debug{skip@up: #1=\the\dimexpr#1\relax (#2)}%
1394 \setbox\Mfx@piece@content\vbox{%
1395 \vskip#1\relax
1396 \unvbox\Mfx@piece@content}%
1397 }
1398 % \end{macrocode}
1399 % \end{macros}
1400 %
1401 % \begin{macros}{\MFX@piece@compressible@up}
1402 % Finally we come to the compressible space. Here, as long as
1403 % there's excess space (\cs{Mfx@marginheight}) we drop the
1404 % compressible space. Once the excess is exhausted, we insert
1405 % them as normal skips.
1406 % \begin{macrocode}
1407 \def\MFX@piece@compressible@up#1{%
1408 %<debug>\MFX@debug{compressible@up: #1, excess=\the\Mfx@marginheight}%
1409 \advance\Mfx@marginheight-#1\relax
1410 \ifdim\Mfx@marginheight<\z@
1411 \MFX@piece@skip@up{-\Mfx@marginheight}\relax
1412 \Mfx@marginheight\z@
1413 \fi
1414 }
1415 % \end{macrocode}
1416 % \end{macros}
1417 %
1418 % \section{Cleaning up}
1419 % We need to worry about a few more things. First, what happens
1420 % if we reach the end of the document and there are still deferred
1421 % margin notes? We need to be able to dump all the margin notes
1422 % whenever the user wants (i.e. before a new chapter), so we'll
1423 % make a macro \cs{dumpmargins} to do this, and then make sure it
1424 % gets called \cs{AtEndDocument}. Since we're looping to do this,
1425 % we need to make darned sure that every \cs{newpage} shrinks the
1426 % marginlist.
1427 % \begin{macros}{\dumpmargins}
1428 % \begin{macrocode}
1429 \def\dumpmargins{%
1430 %<debug>\MFX@debug{dumpmargins}%
1431 \loop
1432 \unless\ifx\mfx@marginlist\@empty
1433 %<debug>\MFX@debug{dumpmargins: marginlist=\MFX@mac\mfx@marginlist}%
1434 \let\temp@\mfx@marginlist
1435 \vbox{}\clearpage
1436 \ifx\temp@\mfx@marginlist
1437 \PackageError{marginfix}{lost some margin notes%
1438 \ifx\mfx@marginstart\relax\ (missing \noexpand\unblockmargin)\fi
1439 %<debug>: \MFX@mac\mfx@marginlist
1440 }\@eha
1441 \let\mfx@marginlist\@empty % be nicer by just dropping one?
1442 % TODO: also, set an emergency mode to allow oversized notes
1443 \fi
1444 \repeat
1445 }
1446 \AtEndDocument{\dumpmargins}
1447 % \end{macrocode}
1448 % \end{macros}
1449 %
1450 % \section{User macros}
1451 %
1452 % \begin{macros}{\marginskip}
1453 % Inserting a skip in the margin list is simple. We need only
1454 % append \cs{mfx@build@skip} to \cs{mfx@marginlist}.
1455 % \begin{macrocode}
1456 \def\marginskip#1{%
1457 \MFX@cons\mfx@marginlist{\noexpand\mfx@build@skip{#1}}%
1458 }
1459 % \end{macrocode}
1460 % \end{macros}
1461 %
1462 % \begin{macros}{\clearmargin}
1463 % Likewise, \cs{clearmargin} is easy too.
1464 % \begin{macrocode}
1465 \def\clearmargin{%
1466 \MFX@cons\mfx@marginlist{\noexpand\mfx@build@clear}%
1467 }
1468 % \end{macrocode}
1469 % \end{macros}
1470 % \begin{macros}{\softclearmargin}
1471 % While we call \cs{softclearmargin} a ``clear margin'', it's
1472 % actually just a big \cs{marginskip}. This allows us to stack
1473 % multiple copies without backing them all up.
1474 % \begin{macrocode}
1475 \def\softclearmargin{%
1476 \marginskip{\the\textheight}%
1477 }
1478 % \end{macrocode}
1479 % \end{macros}
1480 %
1481 % \begin{macros}{\extendmargin}
1482 % We overload \cs{Mfx@marginheight} to be the amount of extension
1483 % at all times except shipout-time.
1484 % \begin{macrocode}
1485 \def\extendmargin#1{%
1486 \advance\Mfx@marginheight#1\relax
1487 }
1488 % \end{macrocode}
1489 % \end{macros}
1490 %
1491 % \begin{macros}{\mparshift}
1492 % This is as simple as setting the dimen register. We advance so that
1493 % the shifts are cumulative, but there's not really any point either way.
1494 % \begin{macrocode}
1495 \def\mparshift#1{%
1496 \advance\Mfx@mparshift#1\relax
1497 }
1498 % \end{macrocode}
1499 % \end{macros}
1500 %
1501 % \begin{macros}{\blockmargin,\MFX@blockmargin}
1502 % We need two macros to process the optional bracket argument.
1503 % Essentially, \cs{blockmargin} just checks that
1504 % \cs{mfx@marginstart} is defined and then appends a new item to
1505 % the \cs{mfx@marginpieces} list to indicate a piece that starts
1506 % at \cs{mfx@marginstart} and ends at the current position, potentially
1507 % modified by the argument. It then resets \cs{mfx@marginstart} to
1508 % \cs{relax} and optionally adds a \cs{softclearmargin} if there was no
1509 % star, to keep margin material separated on either side of the phantom.
1510 % \begin{macrocode}
1511 \def\blockmargin{%
1512 \@ifnextchar[%]
1513 \MFX@blockmargin
1514 {\MFX@blockmargin[0\p@]}%
1515 }
1516 \def\MFX@blockmargin[#1]{%
1517 \MFX@inject{%
1518 \ifx\mfx@marginstart\relax
1519 \PackageError{marginfix}{two \\blockmargin with no \\unblockmargin}\@eha
1520 \else
1521 \MFX@cons\mfx@marginpieces{\noexpand
1522 \@elt{\mfx@marginstart}{\expandafter\dimexpr\mfx@ypos+#1\relax}}%
1523 \global\let\mfx@marginstart\relax
1524 \global\advance\Mfx@piece@count\@ne
1525 \fi
1526 }%
1527 }
1528 % \end{macrocode}
1529 % \end{macros}
1530 %
1531 % \begin{macros}{\unblockmargin,\MFX@unblockmargin}
1532 % This is the other half of \cs{blockmargin}. It ensures that the
1533 % margin is currently blocked, and if so, sets \cs{mfx@marginstart}
1534 % back to a real dimension (the current page position, plus the optional
1535 % modifier).
1536 % \begin{macrocode}
1537 \def\unblockmargin{%
1538 \@ifnextchar[%]
1539 \MFX@unblockmargin
1540 {\MFX@unblockmargin[0\p@]}%
1541 }
1542 \def\MFX@unblockmargin[#1]{%
1543 \MFX@inject{%
1544 \ifx\mfx@marginstart\relax
1545 \xdef\mfx@marginstart{\dimexpr\mfx@ypos+#1\relax}%
1546 \else
1547 \PackageError{marginfix}{\\unblockmargin with no \\blockmargin}\@eha
1548 \fi
1549 }%
1550 }
1551 % \end{macrocode}
1552 % \end{macros}
1553 %
1554 % \begin{macros}{\marginphantom,\MFX@marginphantom}
1555 % The \cs{marginphantom} command is basically just a concatenation of
1556 % \cs{blockmargin} and \cs{unblockmargin}. We reimplement it from the
1557 % ground up mainly so that the error messages make more sense.
1558 % \begin{macrocode}
1559 \def\marginphantom{%
1560 \@ifnextchar[%]
1561 \MFX@marginphantom
1562 {\MFX@marginphantom[0\p@]}%
1563 }
1564 \def\MFX@marginphantom[#1]#2{%
1565 \ifdim#2<\z@\MFX@marginphantom[#1+#2]{-#2}\else
1566 \MFX@inject{%
1567 \ifx\mfx@marginstart\relax
1568 \PackageError{marginfix}{\\marginphantom while margin blocked}\@eha
1569 \else
1570 \MFX@cons\mfx@marginpieces{\noexpand
1571 \@elt{\mfx@marginstart}{\expandafter\dimexpr\mfx@ypos+#1\relax}}%
1572 \xdef\mfx@marginstart{\dimexpr\mfx@ypos+#1+#2\relax}%
1573 \global\advance\Mfx@piece@count\@ne
1574 \fi
1575 }%
1576 \fi
1577 }
1578 % \end{macrocode}
1579 % \end{macros}
1580 %
1581 % \section{Random scribbles}
1582 %
1583 % Later we'll get fancier with putting notes next to top/bottom
1584 % figures but for now, not so much.
1585 %
1586 % In the future we will support the use of \cs{pdfsavepos} and
1587 % \cs{pdflastypos} for more accurately determining where the callouts
1588 % actually were, which will end up going right around here. But in
1589 % order to work with older versions of \LaTeX, we still need to
1590 % support the old style of using \cs{@pageht} to figure that out, so for
1591 % now that's all we'll do.
1592 %
1593 % \section{Parting words}
1594 % Finish it up.
1595 % \begin{macrocode}
1596 \makeatother
1597 %</package>
1598 % \end{macrocode}
1599 % \makeatother
1600 % \Finale
1601 %
1602 %
1603 % \iffalse
1604 %
1605 % The next line of code prevents DocStrip from adding the
1606 % character table to the generated files(s).
1607 \endinput
1608 % \fi
1609 %
1610 %% \CharacterTable
1611 %% {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
1612 %% 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
1613 %% Digits \0\1\2\3\4\5\6\7\8\9
1614 %% Exclamation \! Double quote \" Hash (number) \#
1615 %% Dollar \$ Percent \% Ampersand \&
1616 %% Acute accent \' Left paren \( Right paren \)
1617 %% Asterisk \* Plus \+ Comma \,
1618 %% Minus \- Point \. Solidus \/
1619 %% Colon \: Semicolon \; Less than \<
1620 %% Equals \= Greater than \> Question mark \?
1621 %% Commercial at \@ Left bracket \[ Backslash \\
1622 %% Right bracket \] Circumflex \^ Underscore \_
1623 %% Grave accent \` Left brace \{ Vertical bar \|
1624 %% Right brace \} Tilde \~}
1625 %%