Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % This program is free software; you can redistribute it and/or
4 % modify it under the terms of the GNU General Public License
5 % as published by the Free Software Foundation; either version 2
6 % of the License, or (at your option) any later version.
7 %
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
12 %
13 % You should have received a copy of the GNU General Public License
14 % along with this program; if not, write to the Free Software
15 % Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 %
17 % \fi
18 %
19 % \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
20 % \DoNotIndex{\def,\long,\edef,\xdef,\gdef,\let,\global}
21 % \DoNotIndex{\if,\ifnum,\ifdim,\ifcat,\ifmmode,\ifvmode,\ifhmode,%
22 % \iftrue,\iffalse,\ifvoid,\ifx,\ifeof,\ifcase,\else,\or,\fi,\loop,\do}
23 % \DoNotIndex{\box,\copy,\setbox,\unvbox,\unhbox,\hbox,%
24 % \vbox,\vtop,\vcenter}
25 % \DoNotIndex{\@empty,\immediate,\write}
26 % \DoNotIndex{\egroup,\bgroup,\expandafter,\begingroup,\endgroup}
27 % \DoNotIndex{\divide,\advance,\multiply,\count,\dimen}
28 % \DoNotIndex{\relax,\space,\string}
29 % \DoNotIndex{\csname,\endcsname,\@spaces,\openin,\openout,%
30 % \closein,\closeout}
31 % \DoNotIndex{\catcode,\endinput}
32 % \DoNotIndex{\jobname,\message,\read,\the,\noexpand}
33 % \DoNotIndex{\hsize,\vsize,\hskip,\vskip,\kern,\hfil,\hfill,\hss}
34 % \DoNotIndex{\m@ne,\z@,\@m,\z@skip,\@ne,\tw@,\p@}
35 % \DoNotIndex{\DeclareRobustCommand,\DeclareOption,\newcommand,\newcommand*}
36 % \DoNotIndex{\newcount,\newif,\newlinechar,\newread,\newtoks,\newwrite}
37 % \DoNotIndex{\dp,\wd,\ht,\vss,\unskip}
38 %
39 % \CheckSum{417}
40 %
41 % \MakeShortVerb\|
42 %
43 % \iffalse
44 % \begin{macrocode}
45 %<*driver>
46 \documentclass{ltxdoc}
47 \RequirePackage{hyperref}
48 \EnableCrossrefs
49 %\DisableCrossrefs % Say \DisableCrossrefs if index is ready
50 \CodelineIndex
51 \RecordChanges
52 \setcounter{IndexColumns}{2} % make a twocolumn index
53 \setlength{\columnseprule}{0pt} % no rules between columns ...
54 \setlength{\columnsep}{2em} % ... but more spacing instead.
55 \setcounter{unbalance}{4}
56 \setlength{\IndexMin}{100pt}
57 \OnlyDescription
58 \setlength\hfuzz{3pt}
59 %\hbadness=7000
60 \frenchspacing
61 \begin{document}
62 \DocInput{mparhack.dtx}
63 \raggedright
64 \end{document}
65 %</driver>
66 % \end{macrocode}
67 % ^^A See CTAN:tools/filehdr/filehdr.dvi for an explanation for the following stuff
68 %
69 % ===================================================================
70 % @LaTeX-package-file{
71 % version = "1.5",
72 % date = "2021-05-02",
73 % filename = "mparhack.sty",
74 % author-1 = "Tom Sgouros",
75 % email-1 = "[email protected]",
76 % author-2 = "Stefan Ulrich",
77 % email-2 = "[email protected]",
78 % codetable = "ISO/ASCII",
79 % keywords = "marginpar, marginpars, marginal notes, wrong margin"
80 % supported = "yes",
81 % docstring = "User manual and documented source code and for the
82 % LaTeX package mparhack.sty"
83 % }
84 % ===================================================================
85 % \fi
86 % \newcommand*\Com[1]{\texttt{\char`\\#1}}
87 % \newcommand*\File[1]{\texttt{#1}}
88 % \newcommand*\Explanation[1]{\noindent
89 % \rule[.5ex]{.4\linewidth}{.6pt}\kern1em
90 % ^^A$\bullet\bullet\bullet$
91 % {\itshape\small #1}^^A
92 % }^^A\kern1em\rule[.5ex]{6em}{.6pt}}
93 % \newenvironment{Addition}{\par\noindent\Explanation{begin addition}\par\noindent\ignorespaces}^^A
94 % {\Explanation{end addition}}
95 % \newenvironment{Replacement}{\par\noindent\Explanation{begin replacement:}\par\noindent\ignorespaces}^^A
96 % {\Explanation{end replacement}}
97 % \newcommand*\ReplacedBy{\par\noindent\Explanation{by:}\par\noindent\ignorespaces}
98 % \newcommand*\PackageNameNoExt{mparhack}
99 % \newcommand*\PackageName{\texttt{\PackageNameNoExt.sty}}
100 % \def\AfterElseFi#1\else#2\fi{\else#2\fi#1}
101 % \GetFileInfo{mparhack.sty}
102 % \title{\PackageName}
103 % \author{Tom Sgouros\\ {\small\texttt{[email protected]}} \and
104 % Stefan Ulrich\\ {\small\href{mailto:[email protected]}{\texttt{[email protected]}}}}
105 % \date{v1.5 2021-05-02\\[1ex] {\footnotesize CTAN:
106 % \href{https://ctan.org/pkg/mparhack}
107 % {\texttt{https://ctan.org/pkg/mparhack}}}
108 % \\[1ex]
109 % {\footnotesize Github:
110 % \href{https://github.com/u-fischer/mparhack}
111 % {\texttt{https://github.com/u-fischer/mparhack}}}
112 % }
113 % \maketitle
114 %
115 % \begin{abstract}
116 % This package implements a workaround for the \LaTeX\ bug
117 % that marginpars sometimes show up on the wrong margin.
118 % \end{abstract}
119 %
120 % \changes{v1.0}{1999/03/02}{Created}
121 % \changes{v1.1}{1999/09/23}{Rewrite to use only one label per page, and to implement
122 % `twocolumn' document option.}
123 % \changes{v1.2}{2000/02/29}{Small documentation changes}
124 % \changes{v1.2a}{2000/03/14}{Fixed the \Com{NeedsTeXFormat} to use 1997/04/14}
125 % \changes{v1.2c}{2001/02/07}{Small documentation changes}
126 % \changes{v1.3}{2001/02/07}{Use \Com{mph@tempa} instead of \Com{@tempa} to avoid
127 % possible clashes with other classes (e.g. scrpage2)}
128 % \changes{v1.4}{2005/04/17}{Fix a bug with eso-pic.sty (latex/3775)}
129 %
130 %
131 % {\parskip 0pt \tableofcontents }
132 %
133 % ^^A --------------------------------------------------
134 % \section{Introduction}
135 % ^^A --------------------------------------------------
136 %
137 % A persistent problem with the |\marginpar| command is that the
138 % marginalia produced often show up on the wrong margin. This has
139 % been noted in the \LaTeX\ bugs database.^^A
140 % \footnote{See \url{https://www.latex-project.org/cgi-bin/ltxbugs2html}:
141 % \href{https://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/2361}{latex/2361},
142 % \href{https://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/2484}{latex/2484},
143 % \href{https://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/2617}{latex/2617}.}
144 %
145 %
146 % The problem occurs most likely when a marginpar appears near
147 % the top of a page. The problem is exacerbated when
148 % the page break penalties are adjusted so that the page look is
149 % quite ragged. In cases like this, if there is a deal of white
150 % space on a page, the marginpars on the next page will be on the
151 % wrong side for the same length of the page.
152 % The reason for this is that the \LaTeX\ output routine is not
153 % in complete synchronization with the \TeX\ page breaker, so that
154 % sometimes
155 % material might still move to a later page \emph{after} \LaTeX's
156 % marginpar algorithm has determined a page number for the marginpar.
157 %
158 % The fix suggested in the bugs database is to insert |\pagebreak|
159 % commands at the places where the page breaks naturally fall;
160 % then the marginpars will appear on the correct side. However, this is
161 % awkward and unpleasant work for a document that changes
162 % regularly, and seems to fly against the spirit of \LaTeX.
163 %
164 % A different approach was suggested by D. E. Knuth in his
165 % \emph{Macros for Jill}\footnote{Originally published in TUGboat
166 % \textbf{8}, pp.~309--314, reprinted in his \emph{Digital
167 % Typography} pp.~185--193.}: make two |.dvi| files, one with all
168 % notes in the left margin, the other one with all notes in the
169 % right margin. Then print the odd pages from one file, and the
170 % even pages from the other file; merge them by hand.
171 %
172 % However, Knuth also mentions the possibility to use an auxiliar
173 % file to save the page information for each marginpar and use this
174 % information to position it correctly in a second \TeX\ run; and
175 % this is the strategy adopted by \PackageName.
176 % Each marginpar is assigned a unique number; the output routine
177 % writes the number of the last marginpar of each page\slash column
178 % to the |.aux| file, and this information is used in
179 % |\@addmarginpar| to position the current marginpar on the correct
180 % margin.
181 %
182 %
183 % ^^A --------------------------------------------------
184 % \section{Usage}
185 % ^^A --------------------------------------------------
186 % Just insert |\usepackage{mparhack}| into your document preamble.
187 % After the first run, you'll probably get the warning message
188 % `\texttt{Marginpars may have changed. Rerun to get them right}'.
189 % This is similar to \LaTeX's warning `\texttt{Label(s) may have
190 % changed}'; it indicates that the |.aux| file doesn't contain the
191 % information needed to position the marginpars correctly, due to
192 % some changes in the document since the last run. The warning
193 % should go away after running \LaTeX\ again (eventually several
194 % times if there's also a table of contents etc.).
195 %
196 % \DescribeMacro{debug}^^A
197 % If you think something is going wrong, the package also has a
198 % \texttt{debug} option; this will output a lot of tracing
199 % information and will add numbers to the marginpars in the |.dvi|
200 % file for reference.
201 %
202 %
203 % ^^A --------------------------------------------------
204 % \section{Bugs\slash Restrictions}
205 % ^^A --------------------------------------------------
206 % \begin{itemize}
207 % \item
208 % The package works with the |twocolumn| document option, but not
209 % with the |multicol| package (you can't use marginpars inside
210 % the |multicols| environment).
211 % \item
212 % The package uses one command sequence per page, and the total number
213 % of command sequences available is limited on most \TeX\ systems.
214 % Note however that most current \TeX\ implementations
215 % can allocate more than several thousand command names, and some can
216 % be configured to allocate more than the default during run-time,
217 % so this shouldn't be a problem for moderately sized documents.
218 % \item
219 % The package has to hook into three commands of the \LaTeX2e\ output
220 % routine (by redefining some command), so it requires at least
221 % \LaTeX\ version 1997/04/14 and might break with future \LaTeX\
222 % versions that change these commands.
223 % We try to detect this by requiring a version newer than 1997/04/14
224 % and using some heuristics to check if the redefinitions have worked.
225 % You should watch out for this warning message:\par
226 % {\ttfamily\footnotesize\begin{tabular}{@{}l@{}l@{}}
227 %LaTeX Warning:&\ You have requested release `1997/04/14' of LaTeX,\\
228 % &\ but only release `...' is available.
229 % \end{tabular}}\par\noindent
230 % It might be that the package also works with older versions of \LaTeX,
231 % but this hasn't been tested. If one of the heuristics detects an incompatible
232 % change, it will raise an error:\par
233 % {\ttfamily\footnotesize\begin{tabular}{@{}l@{}}
234 %Package mparhack Error: Couldn't hook into command `xyz'.
235 % \end{tabular}}\par\noindent
236 % \vskip-\lastskip\noindent
237 % It might also happen that the heuristics don't detect a change
238 % properly. If the package simply seems to fail, this could be a reason.
239 % In both cases, please send a bug report to
240 % \href{mailto:[email protected]}{\texttt{[email protected]}}
241 % (if neccessary including the
242 % |.log| file of your \LaTeX\ run with the \texttt{debug}
243 % package option enabled). Suggestions/improvements are also welcome,
244 % of course.
245 % \end{itemize}
246 %
247 % ^^A --------------------------------------------------
248 % \section{Acknowledgements}
249 % ^^A --------------------------------------------------
250 % Thanks to Donald Arseneau and Robin Fairbairns for providing
251 % detailed hints\footnote{in \texttt{<[email protected]>} and
252 % \texttt{<7prb8s\$dpr\[email protected]>}, respectively.}
253 % on how to implement the page referencing mechanism using only one
254 % label per page (instead of one label each marginpar, as in a
255 % previous version).
256 %
257 % \AlsoImplementation
258 % \StopEventually{\relax}
259 % ^^A --------------------------------------------------
260 % \section{Implementation}
261 % ^^A --------------------------------------------------
262 %
263 % ^^A --------------------------------------------------
264 % \subsection{Overview}\label{implementation}
265 % ^^A --------------------------------------------------
266 % Here's the algorithm for recording the positioning information for
267 % marginpars in more detail: Every marginpar has a unique number $n$ and
268 % writes to the |.aux| file a string like\par\medskip
269 % ^^A
270 % \begin{tabular}{@{}l}
271 % |\def\mph@nr{|$n$|}|.
272 % \end{tabular}\par\medskip
273 %
274 % The commands shipping out the columns and pages write to the
275 % |.aux| file strings like:\par\medskip
276 % ^^A
277 % \begin{tabular}{@{}l}
278 % |\mph@setcol{|$x{\texttt{:}}$|\thepage}{\mph@nr}|,
279 % \end{tabular}\par\medskip\noindent
280 % ^^A
281 % with $x = \mbox{i}$ for the first column and $x = \mbox{ii}$
282 % otherwise. Then when reading the |.aux|
283 % file at the begin of the \LaTeX\ run, |\mph@nr| will be substitued
284 % by the number of the last marginpar (the value of the |\mph@nr| command
285 % read before that),
286 % and |\mph@setcol| will assign that number to the command sequence |\csname|
287 % |mph@last@|$x{\texttt{:}}$|\thepage\endcsname|; so this will contain
288 % the number of the last marginpar on column\slash page $x{\texttt{:}}$|\thepage|.
289 %
290 % Later on, in the document itself, |\@addmarginpar| will be able to check the value of
291 % |\csname| |mph@last@|$x{\texttt{:}}$\Com{thepage}\Com{end\-csname},
292 % using the current values for $x$ and |\thepage|. Then there are two
293 % cases:
294 %
295 % (a) |\csname| |mph@last@|$x{\texttt{:}}$|\thepage\endcsname| is
296 % undefined, which means that there has been no |.aux| file
297 % previously, or that there are more pages\slash marginpars than in
298 % the previous run; in this case, |\@addmarginpar| will fall back to using
299 % the value of |\c@page| to determine the current page number (yielding the
300 % same result as without this package), or
301 %
302 % (b) |\csname| |mph@last@|$x{\texttt{:}}$|\thepage\endcsname| is defined as
303 % an integer $z$, the number of the last marginpar on |\thepage|.
304 % Then $z \leq n$, the number of the current marginpar.^^A
305 % \footnote{$z$ cannot be greater than $n$: this would
306 % mean that \TeX\ had shifted some material (including a marginpar)
307 % from later pages to earlier pages, which AFAIK cannot happen.}
308 % While $z < n$, the page counter will be incremented (inside a group)
309 % until |\csname| |mph@last@|$x{\texttt{:}}$|\thepage\endcsname| for
310 % that new page is $\geq n$, or until
311 % we end up in case (a). The incremented value for the page counter
312 % now represents the actual page number of the current marginpar,
313 % and it will be used instead of the original page counter in the
314 % check for even-\slash oddness.
315 %
316 % ^^A --------------------------------------------------
317 % \subsection{Code}
318 % ^^A --------------------------------------------------
319 %
320 % We need at least \LaTeX\ version 1997/04/14 which introduced
321 % |\normalsfcodes| into |\@outputdblcol|.
322 % \begin{macrocode}
323 %<*sty>
324 \NeedsTeXFormat{LaTeX2e}[1997/04/14]
325 \ProvidesPackage{mparhack}
326 [2021-05-02 v1.5 (T. Sgouros and S. Ulrich)]
327 % \end{macrocode}
328 % ^^A --------------------------------------------------
329 % \subsubsection{Booleans, Options, etc.}
330 % ^^A --------------------------------------------------
331 % Some boolean switches which we will be using:
332 % |\if@mph@firstcol@| indicates that the current column is the
333 % first column, |\if@mph@warning@| toggles the warning message
334 % about changed marginpars, and |\if@debug@| toggles printing (lots
335 % of) debugging messages.
336 % \begin{macrocode}
337 \newif\if@mph@firstcol@
338 \newif\if@mph@warning@
339 \newif\if@debug@
340 % \end{macrocode}
341 % \begin{macro}{\mph@debug}
342 % This command will print the debugging messages if |@debug@| is |true|:
343 % \begin{macrocode}
344 \newcommand*\mph@debug[2][]{%
345 \if@debug@
346 \typeout{DBG: #2}%
347 \else
348 \ifx#1\@empty\else
349 \typeout{DBG (#1): #2}%
350 \fi
351 \fi
352 }
353 % \end{macrocode}
354 % \end{macro}
355 % The |debug| package option sets |@debug@| to true and adds
356 % numbers to each marginpar by redefining \Com{@xmpar} and
357 % \Com{@ympar}. The numbers are put into boxes of dimension
358 % zero so that they don't alter the spacing.
359 % \begin{macrocode}
360
361 \DeclareOption{debug}{%
362 \newcommand\mph@info{%
363 \@tempcnta=\mph@cnt
364 \advance\@tempcnta1
365 \raisebox{0pt}[0pt][0pt]{\makebox[0pt][r]{\tiny\the\@tempcnta}}%
366 }%
367 \long\def\@xmpar[#1]#2{%
368 \@savemarbox\@marbox{\mph@info#1}%
369 \@savemarbox\@currbox{\mph@info#2}%
370 \@xympar
371 }%
372 \long\def\@ympar#1{%
373 \@savemarbox\@marbox{\mph@info#1}%
374 \global\setbox\@currbox\copy\@marbox
375 \@xympar
376 }%
377 \global\@debug@true
378 }%
379 \ProcessOptions*
380 % \end{macrocode}
381 % ^^A --------------------------------------------------
382 % \subsubsection{Commands}
383 % ^^A --------------------------------------------------
384 % \begin{macro}{\mph@nr}
385 % A command sequence containing the current marginpar number (which
386 % will be written to the |.aux| file).
387 % \begin{macrocode}
388 \newcommand*\mph@nr{0}
389 % \end{macrocode}
390 % \end{macro}
391 % \begin{macro}{\mph@pg@new}
392 % A command sequence for saving the newly determined value of |\thepage| for later use.
393 % \begin{macrocode}
394 \newcommand*\mph@pg@new{0}
395 \newcommand*\mph@lastpage{0}
396 % \end{macrocode}
397 % \end{macro}
398 %
399 % \begin{macro}{\mph@cnt}
400 % Emulate a counter with a command name to save one count
401 % register. Oh well~\dots \texttt{;-)}
402 % \begin{macrocode}
403 \newcommand*\mph@cnt{0}
404 % \end{macrocode}
405 % \end{macro}
406 % \begin{macro}{\mph@step@cnt}
407 % Use this instead of |\stepcounter| to increment the `counter'
408 % passed as the argument.
409 % \begin{macrocode}
410 \newcommand*\mph@step@cnt[1]{%
411 \begingroup
412 \@tempcnta#1
413 \advance\@tempcnta\@ne
414 \protected@xdef#1{\the\@tempcnta}%
415 \endgroup
416 }
417
418 % \end{macrocode}
419 % \end{macro}
420 % \begin{macro}{\mph@setcol}
421 % The command sequence written to the |.aux| file that will save
422 % the column number\slash page number of the last marginpar on the
423 % column\slash page.^^A The additional |ignore@warn#1| macro is used
424 % ^^A in the |\mph@check| macro.
425 % \begin{macrocode}
426 \newcommand*\mph@setcol[2]{%
427 % \@ifundefined{mph@last@#1}{%
428 % \relax
429 % }{%
430 % \expandafter\xdef\csname ignore@warn#1\endcsname{x}%
431 % }%
432 \expandafter\xdef\csname mph@last@#1\endcsname{#2}%
433 }
434 % \end{macrocode}
435 % \end{macro}
436 % \begin{macro}{\mph@check}
437 % Similar to \LaTeX's |\@testdef| for labels, this will be called
438 % at the end of the \LaTeX\ run to check if the meaning of |#2| is
439 % still equal to |mph@last@#1| or if it has changed since the last
440 % run.
441 % \begin{macrocode}
442 \newcommand*\mph@tempa{}
443 \newcommand*\mph@check[2]{%
444 \protected@edef\mph@tempa{#2}%
445 \expandafter\ifx\csname mph@last@#1\endcsname\mph@tempa
446 \mph@debug{\csname mph@last@#1\endcsname\space == \mph@tempa}%
447 \else
448 \mph@debug{\csname mph@last@#1\endcsname\space != \mph@tempa!}%
449 \global\@mph@warning@true
450 \fi
451 }
452 \newcommand*\mph@do@warn{%
453 \if@mph@warning@
454 \PackageWarningNoLine{mparhack}{Marginpars may have
455 changed.\MessageBreak Rerun to get them right%
456 }%
457 \fi
458 }
459 % \end{macrocode}
460 % \end{macro}
461 % To avoid warnings about undefined commands when someone stops using
462 % this package, we |\providecommand| the |\mph@setcol| command that
463 % we're going to write to the |.aux| file.
464 % \begin{macrocode}
465 \AtBeginDocument{%
466 \protected@write\@auxout{}{%
467 \string\providecommand\string\mph@setcol[2]{}%
468 }%
469 % \end{macrocode}
470 % Fix for latex/3775\footnote{^^A
471 % \url{https://www.latex-project.org/cgi-bin/ltxbugs2html?pr=latex/3775}^^A
472 % }: eso-pic uses \Com{@picture} inside the output routine,
473 % which causes problems with our redefinition of \Com{hb@xt@} in \Com{@outputdblcol}.
474 % So, as suggested by M. H\o gholm in his reply to the above bug report, we patch
475 % \Com{@picture} to revert to the original definition of \Com{hb@xt@}.
476 % \begin{macrocode}
477 \@ifpackageloaded{eso-pic}{%
478 \let\mph@orig@picture\@picture
479 \renewcommand\@picture{\let\hb@xt@\mph@orig@hb@xt@\mph@orig@picture}%
480 }{%
481 \relax
482 }%
483 }
484 % \end{macrocode}
485 % Add the needed checks to |\@enddocumenthook|. Again, use
486 % |\csname| ... |\endcsname| to avoid warnings about undefined
487 % commands.
488 % \changes{v1.2}{2000/02/29}{%
489 % Use \Com{csname...} when writing commands to \texttt{.aux} file
490 % }%
491 % \begin{macrocode}
492 \AtEndDocument{%
493 \let\mph@setcol\mph@check
494 \clearpage
495 \immediate\write\@auxout{%
496 \string\gdef\string\mph@lastpage{\the\c@page}^^J%
497 \string\csname\space mph@do@warn\string\endcsname
498 }%
499 }
500 % \end{macrocode}
501 %
502 % \begin{macro}{\mph@ifundef@or@smaller}
503 % A helper macro that checks if the command sequence in |#1| is
504 % undefined or if its value is smaller than |\mph@cnt| (it assumes
505 % that |#1| is never empty). If one of these conditions is true,
506 % the second argument is evaluated, else the third argument is
507 % evaluated (similar to \LaTeX's |\@ifundefined|). This macro
508 % will be used to shorten the body of the next macro definition.
509 % \changes{v1.2}{2000/02/29}{%
510 % Renamed this command, and updated the documentation
511 % }%
512 % \begin{macrocode}
513 \newcommand*\mph@ifundef@or@smaller[1]{%
514 \expandafter\ifx#1\relax
515 \let\mph@tempa\@firstoftwo
516 \else
517 \ifnum#1<\mph@cnt\relax
518 \let\mph@tempa\@firstoftwo
519 \else
520 \let\mph@tempa\@secondoftwo
521 \fi
522 \fi
523 \mph@tempa
524 }
525
526 % \end{macrocode}
527 % \end{macro}
528 %
529 % \begin{macro}{\mph@pg@orig}
530 % \changes{v1.2}{2000/02/29}{%
531 % Added the \Com{mph@pg@orig} macro
532 % }%
533 % The macro |\mph@pg@orig| is used to
534 % save the original value of |\c@page| which is used in case the |.aux|
535 % file information is not up-to date and we can't determine the
536 % correct value; this should give the same results as if \PackageName\
537 % hadn't been used.
538 % \begin{macrocode}
539 \newcommand*\mph@pg@orig{}
540 % \end{macrocode}
541 % \end{macro}
542 %
543 % \begin{macro}{\mph@get@margin}
544 % This macro does most of the work described in
545 % section~\ref{implementation}.
546 % \changes{v1.2}{2000/02/29}{%
547 % Use \Com{c@page} instead of \Com{thepage} as value in \Com{mph@pg@new}
548 % }%
549 % \begin{macrocode}
550 \newcommand*\mph@get@margin{%
551 \begingroup
552 \edef\mph@pg@orig{\the\c@page}
553 \loop
554 % \end{macrocode}
555 % This loop will be controlled by |\@tempswa|; all changes to that
556 % and to |\thepage| will be local to the current group.
557 % \begin{macrocode}
558 \@tempswafalse
559 \mph@debug{--- checking marginpar \mph@cnt}%
560 \if@twocolumn
561 % \end{macrocode}
562 % When we are in twocolumn mode, first check if the current
563 % marginpar number is greater than the last marginpar in the first
564 % column. If it isn't, we're in the first column. If it is, do the
565 % check for the last marginpar of the entire page. If this returns
566 % false, we're in the second column. Else, advance the page number
567 % and try again.
568 % \begin{macrocode}
569 \mph@debug{last on page \thepage:
570 \csname mph@last@i:\thepage\endcsname(i)
571 \csname mph@last@ii:\thepage\endcsname(ii),
572 }%
573 \mph@ifundef@or@smaller{%
574 \csname mph@last@i:\thepage\endcsname
575 }{%
576 \mph@ifundef@or@smaller{%
577 \csname mph@last@ii:\thepage\endcsname
578 }{%
579 \global\@mph@firstcol@true
580 \@tempswatrue
581 \advance\c@page by 1
582 \mph@debug{\mph@cnt\space >
583 \csname mph@last@ii:\thepage\endcsname,
584 incrementing \thepage, set col to i
585 }%
586 }{%
587 \global\@mph@firstcol@false
588 \@tempswafalse
589 \mph@debug{\mph@cnt\space <=
590 \csname mph@last@ii:\thepage\endcsname,
591 exiting loop
592 }%
593 }%
594 }{%
595 \@tempswafalse
596 \global\@mph@firstcol@true
597 \mph@debug{exiting loop}%
598 }%
599 \else
600 % \end{macrocode}
601 % Not in twocolumn mode, we need only check for |mph@last@ii|:
602 % \begin{macrocode}
603 \mph@debug{last on page \thepage\space is
604 \csname mph@last@ii:\thepage\endcsname
605 }%
606 \mph@ifundef@or@smaller{%
607 \csname mph@last@ii:\thepage\endcsname
608 }{%
609 \@tempswatrue
610 \advance\c@page by 1
611 \mph@debug{\mph@cnt\space >
612 \csname mph@last@ii:\thepage\endcsname,
613 incrementing page number
614 }%
615 }{%
616 \@tempswafalse
617 \mph@debug{\mph@cnt\space <=
618 \csname mph@last@ii:\thepage\endcsname,
619 exiting loop
620 }%
621 }%
622 \fi
623 % \end{macrocode}
624 % Now we have either found the correct page, or reached the end of the document
625 % without finding the appropriate page (e.\,g., if the |.aux| file hasn't existed
626 % before, or if the document has grown a lot since the last run).
627 % In this case we reset |\c@page| and |\if@mph@firstcol@| to their original values
628 % and jump out of the loop.
629 % \changes{v1.2}{2000/02/29}{%
630 % Use original values for page and column number by default%
631 % }%
632 % \begin{macrocode}
633 \ifnum\mph@lastpage>\c@page
634 \else
635 \@tempswafalse
636 \mph@debug{\c@page >= \mph@lastpage!}%
637 \c@page=\mph@pg@orig
638 \if@firstcolumn
639 \global\@mph@firstcol@true
640 \else
641 \global\@mph@firstcol@false
642 \fi
643 \mph@debug{using original value: \c@page for
644 \string\c@page and exiting loop.
645 }%
646 \fi
647 \if@tempswa
648 \mph@debug{iterating ...}%
649 \repeat
650 % \end{macrocode}
651 % After having finished the loop, we save the current (eventually modified)
652 % value of |\c@page| to |\mph@pg@new|:
653 % \begin{macrocode}
654 \mph@debug{=== marginpar \mph@cnt\space is on page \thepage%
655 \if@twocolumn, col \if@mph@firstcol@ 1 \else 2 \fi\fi
656 }%
657 \protected@xdef\mph@pg@new{\the\c@page}%
658 \endgroup
659 }
660
661 % \end{macrocode}
662 % \end{macro}
663 %
664 % ^^A --------------------------------------------------
665 % \subsubsection{Patching the output routine}
666 % ^^A --------------------------------------------------
667 % We have to hook into 3 commands that are part of \LaTeX's output routine:
668 % |\@addmarginpar|, |\@outputdblcol| and |\@outputpage|. For the latter, we can use the default
669 % \LaTeX\ mechanism |\g@addto@macro|. For the others, this is more difficult,
670 % since the changes need to go somewhere `in between'.
671 %
672 % One could use hard redefinitions (plus
673 % \Com{CheckCommand*} to verify that the correct version of
674 % the command was being defined); but this soon gets too bothersome
675 % with new \LaTeX2e\ versions introducing minor changes that don't
676 % affect the stuff which we are concerned, but that nevertheless
677 % would require multiplying the redefinitions for the various versions.
678 %
679 % So instead, we locally redefine some
680 % commands to hook into the existing definitions. As a heuristic of
681 % detecting whether we change the appropriate commands, we check
682 % the values of some special counters \Com{mph@chk@xyz} that get
683 % incremented by some of the `hooks'.
684 %
685 % \changes{v1.3a}{2003/05/22}{%
686 % Use \Com{g@addto@macro} for \Com{@outputpage} to
687 % improve compatibility with e.g. the memoir class
688 % }%
689 % \changes{v1.2}{2000/02/29}{%
690 % Added \Com{CheckCommand*} for \Com{@addmarginpar}, \Com{@outputpage}
691 % and \Com{@outputdblcol}
692 % }%
693 % \changes{v1.2b}{2000/09/01}{%
694 % Replaced \Com{CheckCommand*} and the full redefinitions of
695 % \Com{@addmarginpar}, \Com{@outputpage} and \Com{@outputdblcol}
696 % by hooking into certain commands inside them
697 % }%
698 %
699 % \begin{macro}{\mph@error}
700 % Error message if something suspicious happened with the redefintions:
701 % \begin{macrocode}
702 \newcommand\mph@error[1]{%
703 \PackageError{mparhack}{%
704 Couldn't hook into command `#1'
705 }{%
706 This means that a LaTeX version incompatible with
707 mparhack.sty^^J%
708 has been used. See also the section on `Bugs/Restrictions'^^J%
709 in mparhack.dvi. Please send an email about this bug to^^J%
710 <[email protected]>, along with the file `\jobname.log'.
711 }%
712 }
713 % \end{macrocode}
714 % \end{macro}
715 %
716 % \begin{macro}{\@addmarginpar}
717 % This command adds the marginpar box to the current page.
718 % We change the \Com{c@page} counter and the \Com{if@firstcolumn} switch
719 % to the values that have been set correctly by the \Com{mph@get@margin}
720 % command. There's no further error checking done here.
721 %
722 % \begin{macrocode}
723 \newcommand\mph@orig@c@page{}
724 \newcommand\mph@orig@addmarginpar{}
725 \let\mph@orig@addmarginpar\@addmarginpar
726
727 \renewcommand\@addmarginpar{%
728 \mph@step@cnt\mph@cnt
729 \protected@write\@auxout{}%
730 {\string\def\string\mph@nr{\mph@cnt}}%
731 \mph@get@margin
732 \def\mph@orig@c@page{\c@page}
733 \c@page=\mph@pg@new
734 \if@mph@firstcol@
735 \@firstcolumntrue
736 \else
737 \@firstcolumnfalse
738 \fi
739 \mph@orig@addmarginpar
740 \c@page=\mph@orig@c@page
741 }
742
743 % \end{macrocode}
744 % \end{macro}
745 %
746 % \begin{macro}{\@outputpage}
747 % Add the |\mph@outputpage@hook| hook to |\@outputpage|.
748 % We need to decrement |\c@page|, since |\@outputpage| will
749 % have incremented it after dumping the page contents.
750 % The group in |\mph@outputpage@hook| is to keep this change local.
751 % \begin{macrocode}
752 \newcommand*\mph@outputpage@hook{%
753 \bgroup
754 \advance\c@page\m@ne
755 \immediate\write\@auxout{%
756 \string\mph@setcol{ii:\thepage}{\string\mph@nr}%
757 }%
758 \egroup
759 }
760 \g@addto@macro{\@outputpage}{\mph@outputpage@hook}
761
762 % \end{macrocode}
763 % \end{macro}
764 %
765 % \begin{macro}{\@outputdblcol}
766 % In case an entire twocolumn page is finished, this command calls
767 % \Com{hb@xt@} with the boxes for the two columns inside, and each
768 % one needs to set a marker with the current column number. So
769 % we hook into \Com{hb@xt@} and check if its first argument equals
770 % \Com{columnwidth} (\Com{hb@xt@} is also called for the entire page,
771 % but in this case the first argument is \Com{textwidth}).
772 %
773 % The current column number is represented by \Com{mph@chk@dcl}, and at
774 % the end we check if this number equals 0 (in which case the
775 % page hadn't been fininshed yet, and we didn't need to do anything)
776 % or 2 (the `interesting' case); else we signal an error.
777 % \changes{v1.5}{2021-04-02}{%
778 % Changed definition of \Com{@outputdblcol} to allow
779 % \Com{hb@xt@} to correctly expands in shipout hooks. See
780 % \url{https://tex.stackexchange.com/q/595272/2388}
781 % }%
782
783 % \begin{macrocode}
784 \newcommand\mph@orig@hb@xt@{}
785 \newcommand\mph@orig@outputdblcol{}
786 \newcommand\mph@chk@dcl{0}%
787 \let\mph@orig@hb@xt@\hb@xt@
788 \let\mph@orig@outputdblcol\@outputdblcol
789
790 \renewcommand\@outputdblcol{%
791 \def\mph@chk@dcl{0}%
792 \def\hb@xt@##1##2{%
793 \ifx##1\columnwidth
794 \mph@step@cnt\mph@chk@dcl
795 \protected@edef\mph@tempa{%
796 \noexpand\write\noexpand\@auxout{%
797 \noexpand\string\noexpand\mph@setcol{%
798 \romannumeral\mph@chk@dcl:\noexpand\thepage%
799 }{%
800 \noexpand\string\noexpand\mph@nr%
801 }%
802 }%
803 }%
804 \mph@orig@hb@xt@##1{##2\mph@tempa}%
805 \else
806 \mph@orig@hb@xt@##1{##2}%
807 \fi
808 }%
809 \mph@orig@outputdblcol
810 \def\hb@xt@{\mph@orig@hb@xt@}%
811 \ifnum\mph@chk@dcl=0\relax
812 \else
813 \ifnum\mph@chk@dcl=2\relax
814 \else
815 \mph@error{\string\@outputdblcol}%
816 \fi
817 \fi
818 }
819
820 % \end{macrocode}
821 % \end{macro}
822 %
823 % \begin{macro}{\mph@orig@picture}
824 % \begin{macrocode}
825 \newcommand\mph@orig@picture{}
826 % \end{macrocode}
827 % \end{macro}
828 %
829 % \begin{macrocode}
830 %</sty>
831 % \end{macrocode}
832 % ^^A\typeout{End of package documentation.}
833 % ^^A makeindex -s gind.ist \PackageNameNoExt^^J}
834 % \PrintIndex
835 % \PrintChanges
836 % \Finale