Debian Codegraph @

texmf-dist/source/lualatex/marginalia/marginalia.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2025--26 Alan J. Cain
4 %
5 % This file may be distributed and/or modified under the conditions of the LaTeX Project Public License, either version
6 % 1.3c of this license or (at your option) any later version. The latest version of this license is in:
7 %
8 % http://www.latex-project.org/lppl.txt
9 %
10 % and version 1.3c or later is part of all distributions of LaTeX version 2008-05-04 or later.
11 %
12 % \fi
13 %
14 % \iffalse
15 %<*driver>
16 \PassOptionsToPackage{inline}{enumitem}
17 \documentclass{l3doc}
18
19
20 \makeatletter
21 \ExplSyntaxOn
22
23 \cs_gset:Npn \l@subsection { \@dottedtocline{2}{2.5em}{2.8em} } % #2 = 1.5em
24 \cs_gset:Npn \l@subsubsection { \@dottedtocline{3}{5.3em}{3.5em} } % #2 = 1.5em
25 \cs_gset:Npn \l@paragraph { \@dottedtocline{4}{8.8em}{3.2em} } % #2 = 1.5em
26
27 \ExplSyntaxOff
28 \makeatother
29
30
31 \usepackage{xcolor}
32
33 \definecolor{linkcolor}{rgb}{0.0,0.4,0.7}
34 \colorlet{citecolor}{linkcolor}
35 \colorlet{urlcolor}{linkcolor}
36
37 \hypersetup{
38 linkcolor=linkcolor,%
39 citecolor=citecolor,%
40 urlcolor=urlcolor,%
41 }
42 \usepackage{xurl}
43 \renewcommand*\UrlBigBreaks{}
44
45 \newcommand*\fullref[2]{%
46 \hyperref[#2]{#1\penalty 200\ \ref*{#2}}%
47 }
48
49
50 \setcounter{tocdepth}{7}
51 \numberwithin{figure}{section}
52
53
54 \renewcommand*\topfraction{.8}
55
56
57 \usepackage{marginalia}
58
59
60 \renewcommand*\marginpar[1]{%
61 \marginalia[
62 pos=left,
63 ysep=0pt,
64 ysep page top=10mm,
65 ysep page bottom=5mm,
66 width=50mm,
67 ]
68 {#1}%
69 }
70
71
72 \usepackage{titleps}
73
74 \newpagestyle{sideheadings}[\normalfont]{
75 \sethead{%
76 \smash{%
77 \marginalia[
78 pos=left,
79 valign=b,
80 yshift={-\topskip-2\baselineskip},
81 type=optfixed,
82 xsep=2em,
83 ysep={2\baselineskip},
84 column=one,
85 width=40mm,
86 ]{%
87 \raggedleft
88 \itshape
89 \large
90 \sectiontitle
91 }%
92 }%
93 }
94 {}
95 {}
96 \setfoot{}
97 {\thepage}
98 {}
99 }
100
101
102
103
104
105 \usepackage{booktabs}
106 \usepackage{graphicx}
107
108
109 \newlist{vallist}{description}{1}
110 \setlist[vallist]{
111 leftmargin=3em,
112 style=unboxed,
113 labelsep=1em,
114 font=\descriptionitemcolon,
115 nosep,
116 }
117
118 \newcommand*{\descriptionitemcolon}[1]{\kern 1em #1:}
119
120
121 \usepackage{siunitx}
122 \sisetup{
123 mode=match,
124 }
125 \DeclareSIUnit\inch{in}
126 \DeclareSIUnit\point{pt}
127
128
129 \usepackage{tikz}
130 \usetikzlibrary{decorations.pathmorphing}
131
132
133 \newrobustcmd*\examplelabel[1]{%
134 \smash{%
135 \begin{tikzpicture}[baseline=(labelnode.base)]
136 \node[node font=\sffamily\footnotesize] (labelnode) at (0,0) {#1};
137 \draw[gray] (labelnode.center) circle (5pt);
138 \end{tikzpicture}%
139 }%
140 }
141
142
143 \usepackage{mathtools}
144
145 \DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
146 \DeclarePairedDelimiter{\set}{\lbrace}{\rbrace}
147
148
149 \newcommand*\key[1]{\texttt{#1}}
150 \newcommand*\val[1]{\texttt{#1}}
151 \newcommand*\keyvalue[2]{\texttt{#1=#2}}
152
153 \NewDocumentCommand{\default}{ m }{(\textit{Default:} #1)}
154
155
156 \newcommand*\luafunc[1]{\texttt{#1}}
157 \newcommand*\luavar[1]{\texttt{#1}}
158
159
160 \usepackage{listings}
161
162 \lstset{
163 language=[LaTeX]TeX,
164 basicstyle=\small\ttfamily,
165 basewidth=0.5em,
166 }
167
168
169
170 \newcounter{sidenote}
171 \NewDocumentCommand\sidenote{ m }{%
172 \stepcounter{sidenote}%
173 \textsuperscript{\thesidenote}%
174 \marginalia[
175 pos=left,
176 style recto inner=\raggedright\footnotesize,
177 width recto inner=35mm,
178 xsep recto inner=10pt,
179 ysep=5pt,
180 ]{%
181 \leavevmode\strut\llap{\thesidenote~}#1\strut%
182 }%
183 }
184
185
186
187 \newcommand\TikZ{Ti\textit{k}Z}
188
189
190
191 \begin{document}
192
193 \DocInput{marginalia.dtx}
194
195 \PrintIndex
196
197 \end{document}
198 %</driver>
199 % \fi
200 %
201 %
202 %
203 % \GetFileInfo{marginalia.sty}
204 %
205 %
206 %
207 % \title{^^A
208 % \pkg{marginalia} ^^A
209 % --- Non-floating marginal content with automatic placement for Lua\LaTeX^^A
210 % \sidenote{This document\\describes \fileversion,\\last revised \filedate.}^^A
211 % }
212 %
213 % \author{^^A
214 % Alan J. Cain\sidenote{\texttt{a.j.cain (AT) gmail.com}}^^A
215 % }
216 %
217 % \date{Released \filedate}
218 %
219 % \maketitle
220 %
221 %
222 %
223 % \begin{abstract}
224 % This Lua\LaTeX\ package allows the placement of marginal content anywhere, without \cs{marginpar}'s limits, and
225 % automatically adjusts positions to prevent overlaps or content being pushed off the page, and offers key--value
226 % settings that allow fine-grained customization.
227 % \end{abstract}
228 %
229 %
230 %
231 % \section*{Demonstration}
232 % \addcontentsline{toc}{section}{Demonstration}
233 %
234 % \begingroup
235 %
236 % \marginaliasetup{
237 % style recto inner=\footnotesize\sffamily,
238 % style recto outer=\raggedright\footnotesize\sffamily,
239 % width recto inner=35mm,
240 % width recto outer=15mm,
241 % xsep recto inner=10pt,
242 % xsep recto outer=13pt,
243 % ysep=5pt,
244 % }
245 %
246 %
247 % \noindent
248 % The \pkg{marginalia} package permits intelligent placement of marginal content, such as notes.^^A
249 % \marginalia{(Like this one.)}
250 % This page demonstrates some of its capabilities.
251 %
252 % Key--value options allow global or local settings of the style, width, placement, and spacing of marginal content.
253 % In particular, styles and widths can be specified globally depending on the margin in which the content is placed;
254 % the correct style or width will be selected for each item. For example, in this demonstration,
255 % \qty{15}{\milli\metre}-wide ragged-right sans-serif has been specified globally as the default for items in the
256 % right margin and \qty{35}{\milli\metre}-wide justified sans-serif for items in the left margin. Note that the
257 % vertical position of items is adjusted automatically to avoid overlaps, but there is an option to specify that items
258 % have fixed positions.^^A
259 % \marginalia[pos=left]{By default, this marginal note would have its top line aligned with
260 % the last line of the adjacent paragraph, but it has been automatically moved upwards to avoid clashing with the
261 % figure caption below, which has been set with an option that fixes its position.}^^A
262 % \marginalia[valign=t]{Notes can be placed on both sides of the paragraph.}^^A
263 %
264 % \begin{figure}[h]^^A
265 % \centering
266 % \begin{tikzpicture}
267 % \node[
268 % minimum width={\textwidth-.4pt},
269 % minimum height=30mm,
270 % draw,
271 % fill=lightgray!50!white,
272 % rounded corners,
273 % align=center,
274 % ] at (0,0) {{\Large A demonstration of \pkg{marginalia}.}\\[2pt](This is a \texttt{figure} enviroment.)};
275 % \end{tikzpicture}^^A
276 % \marginalia[valign=b,pos=left,style=\small,type=fixed]{{\raggedleft\textbf{Figure~0}\par}\pkg{marginalia} can
277 % place content alongside floats, so it can be used to place float captions in the margin. (Here, the text style has
278 % been locally switched to Roman.)}^^A
279 % \end{figure}
280 %
281 % Unlike the usual \cs{marginpar} command,^^A
282 % \marginalia[pos=left,mark={{
283 % \begin{tikzpicture}[xscale=-1,scale=.1,baseline=-2.5pt]
284 % \fill[blue!70!black] (60:1) -- (180:1) -- (-60:1) -- cycle;
285 % \end{tikzpicture}
286 % }}]{This is a top-aligned margin item pointing to the relevant line of text.}
287 % \pkg{marginalia} allows marginal content to be placed next to floats, footnotes, or the page head or footer.
288 % ‘Marks’ can also be added pointing to the relevant line of text^^A
289 % \marginalia[valign=m,mark={{
290 % \begin{tikzpicture}[scale=.15,baseline=-2.5pt]
291 % \fill[red!70!black] (60:1) -- (180:1) -- (-60:1) -- cycle;
292 % \end{tikzpicture}
293 % }}]{This is a middle-aligned margin item pointing to the relevant line of text.}
294 % and there are various vertical alignment options.
295 %
296 % The automatic adjustment of the vertical positions of items will not result in items being closer than specified
297 % distances from the top or bottom of the page (unless there is actually insufficient space to place the items).^^A
298 % \marginalia[pos=left,ysep page bottom=10mm,width=25mm,style=\raggedleft\footnotesize\sffamily,mark={{
299 % \begin{tikzpicture}[xscale=-1,scale=.1,baseline=-2.5pt]
300 % \fill[blue!70!black] (60:1) -- (180:1) -- (-60:1) -- cycle;
301 % \end{tikzpicture}
302 % }}]{^^A
303 % This marginal note, the width and style of which have locally been set to \qty{25}{mm} and ragged left, would by
304 % default have its top line aligned with the last line of the adjacent paragraph, but it has been moved upwards to
305 % maintain a distance of \qty{10}{mm} from the bottom of the page.^^A
306 % }
307 % \endgroup
308 %
309 %
310 %
311 % \clearpage
312 % \tableofcontents
313 %
314 %
315 %
316 % \begin{documentation}
317 %
318 %
319 %
320 % \section{Introduction}
321 % \pagestyle{sideheadings}
322 %
323 % The \LaTeX\ \cs{marginpar} command is the basic method for placing content in the margin. For purposes such as drawing
324 % attention to particular points in the text, it functions well. Its main limitation is that \cs{marginpar} works via
325 % the \LaTeX\ float mechanism and so cannot be used to create marginal content next to a figure, table, or other float,
326 % or next to a footnote, or to place running heads in the margin, such as are found in the left-hand margin of this
327 % document except for the `implementation' section. (Bringhurst called this style `running shoulder\-heads'
328 % \cite[p.~65]{bringhurst_elements}, but the term may be non-standard.)
329 %
330 % Trying to set many separate pieces of marginal content using \cs{marginpar} can lead to other problems. If two
331 % \texttt{marginpar}s would clash, \LaTeX\ shifts the second item downward. But the cumulative effect can lead to
332 % \texttt{marginpar}s being shifted downward off the bottom of the page. Further, the asynchronous nature of \TeX's
333 % page-breaking can cause:
334 % \begin{enumerate*}[label={(\arabic*)}]
335 % \item a \texttt{marginpar} to be placed in the wrong margin;
336 % \item the topmost \texttt{marginpar} on a page to be unnecessarily shifted downward because of a hypothetical clash
337 % that would have occured with the previous \texttt{marginpar}, had they been on the same page.
338 % \end{enumerate*}
339 %
340 % Packages like \pkg{mparhack}\sidenote{\textsc{url:} \url{https://ctan.org/pkg/mparhack}} (Tom Sgouros \& Stefan
341 % Ulrich), \pkg{marginnote}\sidenote{\textsc{url}: \url{https://ctan.org/pkg/marginnote}} (Markus Kohm),
342 % \pkg{marginfix}\sidenote{\textsc{url:} \url{https://ctan.org/pkg/marginfix}} (Stephen Hicks) and
343 % \pkg{marginfit}\sidenote{\textsc{url:} \url{https://ctan.org/pkg/marginfit}} (Maurice Leclaire) were created to avoid
344 % these limitations and problems. \pkg{mparhack} only ensures that each \texttt{marginpar} appears on the correct side
345 % of the page. \pkg{marginnote} allows marginal content to be placed anywhere, but does not adjust positions to avoid
346 % clashes. \pkg{marginfix} adjusts positions, but the unadjusted vertical positioning can be slightly off, and the
347 % package still uses floats. \pkg{marginfit} gets positions exactly right, but uses the insert mechanism and so marginal
348 % content cannot appear next to floats or footnotes.
349 %
350 % This Lua\LaTeX\ package, \pkg{marginalia}, provides a \cs{marginalia} command that attempts to avoid these
351 % limitations. Marginal content is placed, not via floats or inserts, but by a calculated per-item horizontal shift
352 % inside an (invisible) \cs{rlap} or \cs{llap} from the position where the \cs{marginalia} command was issued (which is
353 % similar to the technique used by \pkg{marginnote}), plus a calculated per-item vertical shift to avoid clashes with
354 % other content. The vertical shift is usually downward, but may be upward when necessary to prevent content from being
355 % shifted off the bottom of the page (which is similar to the vertical shifts performed by \pkg{marginfix} and
356 % \pkg{marginfit}).
357 %
358 % The calculation of the horizontal and vertical shifts uses information written to the \file{.aux} file during the
359 % previous Lua\LaTeX\ run. It thus takes at least two runs for all content to appear in the correct places. The package
360 % reports any changes from the previous run and any problems encountered.
361 %
362 % \bigskip
363 %
364 % \noindent\emph{Note:} \pkg{marginalia} was written to typeset running heads in the margin, sidenote references,
365 % side-captions for floats, and small marginal figures in the author's book \textit{Form \& Number: A History of
366 % Mathematical Beauty} \cite{cain_formandnumber_ebook_large}.\sidenote{\textit{Form \& Number} is freely available on
367 % the Internet Archive under a Creative Commons licence.
368 % \textsc{url}:~\url{https://archive.org/details/cain_formandnumber_ebook_large}} Thus the basic functionality has been
369 % tested extensively, and it has performed correctly.
370 %
371 %
372 %
373 % \paragraph*{Licence.} \noindent\pkg{marginalia} is released under the \LaTeX\ Project Public Licence v1.3c or
374 % later.\sidenote{\textsc{url}: \url{https://www.latex-project.org/lppl.txt}}
375 %
376 %
377 %
378 % \paragraph*{Acknowledgements.} The author thanks Ulrike Fischer for explaining how to add tagging support, and Julien
379 % Labbé for some valuable suggestions.
380 %
381 %
382 %
383 % \paragraph*{Translation.} A French translation of the documentation has been made by members of GUTenberg (Le~Groupe
384 % francophone des Utilisateurs de \TeX).\sidenote{\textsc{url}:
385 % \url{https://gitlab.gutenberg-asso.fr/gutenberg/traduction-de-marginalia/}}
386 %
387 %
388 %
389 % \paragraph*{Feature requests and bug reports}
390 %
391 % The development code and issue tracker are hosted at Codeberg.\sidenote{\textsc{url}:
392 % \url{https://codeberg.org/ajcain/marginalia}}
393 %
394 %
395 %
396 % \paragraph*{Other resources.} An introduction to \pkg{marginalia} has appeared in \textit{TUGboat} \cite{c_marginalia}.
397 %
398 %
399 %
400 % \section{Requirements}
401 %
402 % \pkg{marginalia} requires
403 % \begin{enumerate}[label={(\arabic*)}]
404 % \item Lua\LaTeX,
405 % \item a recent \LaTeX\ kernel with \pkg{expl3} support (any kernel version since 2020-02-02 should suffice).
406 % \end{enumerate}
407 % It does not depend on any other packages.
408 %
409 %
410 %
411 % \section{Installation}
412 %
413 % To install \pkg{marginalia} manually, run \texttt{luatex marginalia.ins} and copy \texttt{marginalia.sty} and
414 % \texttt{marginalia.lua} to somewhere Lua\LaTeX\ can find them.
415 %
416 %
417 %
418 % \section{Getting started}
419 %
420 % \pkg{marginalia} works `out of the box'. Load the package (there are no package options) and use the main
421 % \cs{marginalia} command to place marginal content. \fullref{Figure}{fig:example-getting-started} shows the source code
422 % for a small demonstration and the resulting document. \emph{The source code must be processed \textsl{twice} by
423 % Lua\LaTeX\ for the marginal content to be placed correctly.} (See \fullref{Section}{sec:usage} for discussion of the
424 % need for multiple runs.)
425 %
426 % \begin{figure}[t]
427 % \lstinputlisting{marginalia-doc-example-getting-started.tex}
428 % \begin{tikzpicture}
429 % \pgfmathsetmacro{\s}{(\textwidth+.4pt)/210mm}
430 % \pgfmathsetlengthmacro{\w}{\s*210mm}
431 % \pgfmathsetlengthmacro{\h}{\s*110mm}
432 % \path[save path=\outline] (0,0) -| ++(\w,-\h) -| cycle;
433 % \begin{scope}
434 % \clip[use path=\outline];
435 % \node[anchor=north west,inner sep=0] at (0,0)
436 % {\includegraphics[scale=\s]{marginalia-doc-example-getting-started.pdf}};
437 % \end{scope}
438 % \draw[gray,use path=\outline];
439 % \end{tikzpicture}
440 %
441 % \caption{A small demonstration of \pkg{marginalia}.}
442 % \label{fig:example-getting-started}
443 % \end{figure}
444 %
445 % Turn to \fullref{Section}{sec:commands} for a detailed description of the available user commands, and
446 % \fullref{Section}{sec:options} for the various options (such as \keyvalue{style}{\meta{code}}) that can be used to
447 % change the placement and formatting of the marginal content.
448 %
449 %
450 %
451 % \section{User commands}
452 % \label{sec:commands}
453 %
454 % \begin{function}{\marginalia}
455 % \begin{syntax}
456 % \cs{marginalia}\oarg{options}\marg{content}
457 % \end{syntax}
458 % This is the basic command for placing marginal content. The \meta{content} can, roughly speaking, be anything: text,
459 % mathematics, included graphics, \TikZ. The optional argument \meta{options} is a key--value list that
460 % specifies how the content is typeset. The keys are described in \fullref{Section}{sec:options}.
461 % \end{function}
462 %
463 %
464 %
465 % \begin{function}{\marginaliasetup}
466 % \begin{syntax}
467 % \cs{marginaliasetup}\marg{options}
468 % \end{syntax}
469 % This command is used to set options for subsequent calls to \cs{marginalia}. The argument \meta{options} is the same
470 % kind of key--value list as the \meta{options} argument for the \cs{marginalia} command, and the keys are described
471 % in \fullref{Subsection}{sec:options}.
472 %
473 % Note that \cs{marginaliasetup} can be used in the preamble or in the body of the document. Options set using
474 % \cs{marginaliasetup} have effect only within the current group.
475 % \end{function}
476 %
477 %
478 %
479 % \begin{function}{\marginalianewgeometry}
480 % \begin{syntax}
481 % \cs{marginalianewgeometry}
482 % \end{syntax}
483 % This command signals to \pkg{marginalia} that the page layout has been changed, for instance by using the
484 % \cs{newgeometry} command from the \pkg{geometry} package,\sidenote{\textsc{url}:
485 % \url{https://ctan.org/pkg/geometry}} or by using the \LaTeX\ command \cs{twocolumn} to switch to two-column mode. It
486 % should be issued immediately after such a change, and certainly before the first page with the new layout has been
487 % shipped out. There is no harm in using it unnecessarily.
488 % \end{function}
489 %
490 %
491 %
492 % \subsection{Access to page and column}
493 %
494 % Within the \meta{content} of \cs{marginalia}, two counters are available which specify the actual page and column in
495 % which the call to \cs{marginalia} appears. These counters can be used to select different actions depending on the
496 % page on which the content appears or (in two-column mode) whether it pertains to the left or right column. It is best
497 % to use the variants of the \key{style} and \key{width} keys if marginal content should have different widths or styles
498 % depending on whether they appear on a recto/verso page or pertain to a particular column. These counters are made
499 % available for purposes not covered by the \key{style} and \key{width} variants. The value of each counter is based on
500 % the position of the call to \cs{marginalia} on the previous Lua\LaTeX\ run.
501 %
502 % \begin{variable}{\marginaliapage}
503 % A counter register, available within the \meta{content} of \cs{marginalia}, that holds the actual page on which the
504 % marginal content appears. The value is based on the previous Lua\LaTeX\ run and will default to \(1\).
505 % \end{variable}
506 %
507 % \begin{variable}{\marginaliacolumn}
508 % A counter register, available within the \meta{content} of \cs{marginalia}, that holds the actual column to which
509 % the marginal content pertains. The value is \(1\) for the left column, \(2\) for the right column. In one-column
510 % mode, the value is always \(0\). (If the key \key{column} is used to manually specify the column to which the
511 % content pertains, the value of \cs{marginaliacolumn} will change accordingly.) The value is based on the previous
512 % Lua\LaTeX\ run and will default to \(0\).
513 % \end{variable}
514 %
515 % ^^A Hack: need to manually deactivate page heading here, since the "function" environment does not use marginalia.
516 % \thispagestyle{plain}
517 %
518 %
519 %
520 % \section{Options}
521 % \label{sec:options}
522 %
523 % The description of keys in this section, which are summarized in \fullref{Tables}{tbl:keys-type-position} and
524 % \ref{tbl:keys-appearance} (on pages \pageref{tbl:keys-type-position} and \pageref{tbl:keys-appearance} respectively),
525 % should be read in conjunction with the discussion of how marginal content is placed in
526 % \fullref{Section}{sec:placement}. In particular, the variants of the keys \key{style}, \key{width}, and \key{mark}
527 % follow the terminology shown in \fullref{Figure}{fig:terminology}.
528 %
529 % Note that the initial values of the various keys that take dimensions (namely \key{width}\ldots, \key{xsep}\ldots,
530 % \key{ysep}\ldots) are assigned at \cs{begin}\texttt{\{}\env{document}\texttt{\}}. Thus their defaults are determined
531 % by the values of \cs{marginparwidth}, \cs{marginparsep}, \cs{marginparpush} and the page layout at
532 % \cs{begin}\texttt{\{}\env{document}\texttt{\}}, \emph{not} at package load time. Similarly, if the user sets these
533 % keys in the preamble, the assignment is evaluated at \cs{begin}\texttt{\{}\env{document}\texttt{\}}. For example,
534 % using \cs{marginaliasetup}\texttt{\{\key{width}=.5\cs{oddsidemargin}\}} in the preamble results in the \key{width} key
535 % being set to half the value of \cs{oddsidemargin} at \cs{begin}\texttt{\{}\env{document}\texttt{\}}, \emph{not} to
536 % half the value of \cs{oddsidemargin} at the call to \cs{marginaliasetup}.
537 %
538 % \begin{table}[t]
539 % \caption{Summary of keys affecting the type and position of the marginal content. (For keys affecting the appearance
540 % of marginal content, see \fullref{Table}{tbl:keys-appearance}.) All keys can be set using \cs{marginaliasetup} or
541 % passed in the optional argument to \cs{marginalia}.}
542 % \label{tbl:keys-type-position}
543 % \leavevmode\strut\kern\hsize
544 % \llap{^^A
545 % \begin{tabular*}{\hsize+30mm}{p{50mm}p{60mm}l}
546 % \toprule
547 % Key name & Value & Default \\
548 % \midrule
549 % \key{type} & \(\set{\val{normal},\val{fixed},\val{optfixed}}\) & \val{normal} \\
550 % \midrule
551 % \key{pos} & \(\set{\val{auto},\val{reverse},\val{left},
552 % \val{right},\val{nearest}}\) & \val{auto} \\
553 % \key{column} & \(\set{\val{auto},\val{one},\val{left},\val{right}}\) & \val{auto} \\
554 % \key{xsep} & Dimension & \val{\cs{marginparsep}} \\
555 % \key{xsep outer} & Dimension & \val{\cs{marginparsep}} \\
556 % \key{xsep inner} & Dimension & \val{\cs{marginparsep}} \\
557 % \key{xsep between} & Dimension & \val{\cs{marginparsep}} \\
558 % \key{xsep recto outer} & Dimension & \val{\cs{marginparsep}} \\
559 % \key{xsep recto inner} & Dimension & \val{\cs{marginparsep}} \\
560 % \key{xsep verso outer} & Dimension & \val{\cs{marginparsep}} \\
561 % \key{xsep verso inner} & Dimension & \val{\cs{marginparsep}} \\
562 % \key{xsep right between} & Dimension & \val{\cs{marginparsep}} \\
563 % \key{xsep left between} & Dimension & \val{\cs{marginparsep}} \\
564 % \midrule
565 % \key{valign} & \(\set{\val{t},\val{b},\val{c},\val{m}}\) & \val{t} \\
566 % \key{yshift} & Dimension & \qty{0}{\point} \\
567 % \key{ysep} & Dimension & \val{\cs{marginparpush}} \\
568 % \key{ysep above below} & Dimension & \val{\cs{marginparpush}} \\
569 % \key{ysep above} & Dimension & \val{\cs{marginparpush}} \\
570 % \key{ysep below} & Dimension & \val{\cs{marginparpush}} \\
571 % \key{ysep page top} & Dimension & [Margin above textblock] \\
572 % \key{ysep page bottom} & Dimension & [Margin below textblock] \\
573 % \key{ysep page top margin} & [None] & --- \\
574 % \key{ysep page bottom margin} & [None] & --- \\
575 % \key{ysep page top bottom margin} & [None] & --- \\
576 % \bottomrule
577 % \end{tabular*}^^A
578 % }
579 % \end{table}
580 %
581 %
582 %
583 % \subsection{Type}
584 %
585 % \DescribeOption{type} The \key{type} of an item of marginal content can be set to one of the following three values:
586 % \begin{vallist}
587 % \item[\val{normal}] The vertical position of the item will be changed automatically if necessary to prevent a clash
588 % with another item of content.
589 % \item[\val{fixed}] The vertical position of the item will \emph{never} be changed automatically from the position
590 % specified by \key{yshift}, even if there is a clash with another item. (The type \val{fixed} was designed for
591 % setting float captions in the margin, since a caption should not move away from the float with which it is
592 % associated.)
593 % \item[\val{optfixed}] The vertical position of the item will \emph{never} be changed automatically from the position
594 % specified by \key{yshift}, even if there is a clash with another item. But an \val{optfixed} item will not appear in
595 % the document if it would clash with a \val{fixed} item. (The type \val{optfixed} was designed for setting running
596 % heads in the margin, which should not appear if they would clash with a figure caption set in the margin.)
597 % \end{vallist}
598 % \default{\val{normal}}
599 %
600 %
601 %
602 % \subsection{Horizontal placement}
603 %
604 % \DescribeOption{pos} The position in which an item of marginal content should be placed. It can be set to one of the
605 % the following four values:
606 % \begin{vallist}
607 % \item[\val{auto}] Place the item in the default position as described in \fullref{Section}{sec:placement}: the outer
608 % margin in single-column mode, and on the opposite side from the other column in two-column mode.
609 % \item[\val{reverse}] Place the item on the opposite side of the text block (in one-column mode) or column (in
610 % two-column mode) from \val{auto}.
611 % \item[\val{left}] The left side of the text block or column.
612 % \item[\val{right}] The right side of the text block of column.
613 % \item[\val{nearest}] The side of the text block or column nearest to which \cs{marginalia} was called.
614 % \end{vallist}%
615 % \default{\val{auto}}
616 %
617 %
618 % \medskip\goodbreak
619 %
620 %
621 % \DescribeOption{column} In two-column mode, \pkg{marginalia} tries to determine to which column an item of marginal
622 % content pertains using the position of the call to \cs{marginalia}. If the call is to the left of the mid-point
623 % between the columns, the item is assumed to pertain to the left column; otherwise, it is assumed to pertain to the
624 % right column. In certain situations, this might lead to undesired placement of the item. In particular, any call to
625 % \cs{marginalia} in a full-width float in two-column mode would be handled as if it were a call from one of the columns
626 % and might thus be set in the wrong place. Similarly, an overfull hbox or a piece of \cs{rlap}-ped text might carry a
627 % call to \cs{marginalia} from the left column text into the area of the page occupied by the right column.
628 %
629 % The key \key{column} can be used to specify which column \pkg{marginalia} should place the item in. It can be set to
630 % one of four values:
631 % \begin{vallist}
632 % \item[\val{auto}] Automatically determine which column an item of marginal content is placed in.
633 % \item[\val{one}] Treat the item as being called from one-column mode.
634 % \item[\val{left}] Treat the item as pertaining to the left column.
635 % \item[\val{right}] Treat the item as pertaining to the right column.
636 % \end{vallist}
637 % The value of \key{column} has no effect in one-column mode. \default{\val{auto}}
638 %
639 %
640 % \medskip\goodbreak
641 %
642 %
643 % \DescribeOption{xsep}
644 % \DescribeOption{xsep outer}
645 % \DescribeOption{xsep inner}
646 % \DescribeOption{xsep between}
647 % \DescribeOption{xsep recto outer}
648 % \DescribeOption{xsep recto inner}
649 % \DescribeOption{xsep verso outer}
650 % \DescribeOption{xsep verso inner}
651 % \DescribeOption{xsep right between}
652 % \DescribeOption{xsep left between}
653 % These keys specify the horizontal separation between an item of marginal content and the text block next
654 % to which it is placed. Which separation is used will depend on where the item is typeset. The terminology is as
655 % in \fullref{Figure}{fig:terminology}.
656 % \begin{vallist}
657 % \item[\key{xsep recto outer}] used for an item in the outer margin of a recto page.
658 % \item[\key{xsep recto inner}] used for an item in the inner margin of a recto page.
659 % \item[\key{xsep verso outer}] used for an item in the outer margin of a verso page.
660 % \item[\key{xsep verso inner}] used for an item in the inner margin of a verso page.
661 % \item[\key{xsep right between}] used for an item set from the right column between the columns.
662 % \item[\key{xsep left between}] used for an item set from the left column between the columns.
663 % \item[\key{xsep outer}] a shorthand for setting the keys \key{xsep recto outer} and \key{xsep verso outer}
664 % simultaneously to the same value.
665 % \item[\key{xsep inner}] a shorthand for setting the keys \key{xsep recto inner} and \key{xsep verso inner}
666 % simultaneously to the same value.
667 % \item[\key{xsep between}] a shorthand for setting the keys \key{xsep right between} and \key{xsep left between}
668 % simultaneously to the same value.
669 % \item[\key{xsep}] a shorthand for setting all of these keys simultaneously.
670 % \end{vallist}
671 % (The shorthands \key{xsep outer} and \key{xsep inner} exist because page geometry is usually symmetrical between recto
672 % and verso pages as regards outer and inner margins. The shorthand \key{xsep between} exists because the space between
673 % columns, if used at all for marginal content, will often be shared equally.) Each of these keys must be set to a valid
674 % dimension. \default{value of \cs{marginparsep} at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
675 %
676 %
677 %
678 % \subsection{Vertical placement}
679 %
680 % \DescribeOption{valign} This key specifies the vertical alignment of the marginal content item, before any
681 % \key{yshift} or automatic adjustment is applied. It can be set to one of the following three values:
682 % \begin{vallist}
683 %
684 % \item[\val{t}] The baseline of the marginal content item is the baseline of the topmost box in its contents. Thus,
685 % if no \key{yshift} is specified and there is no automatic adjustment, the baseline of the topmost box of the
686 % marginal content will be vertically aligned with the line where the call to \cs{marginalia} is located.
687 %
688 % \item[\val{b}] The baseline of the marginal content item is the baseline of the bottommost box in its contents.
689 % Thus, if no \key{yshift} is specified and there is no automatic adjustment, the baseline of the bottommost box of
690 % the marginal content will be vertically aligned with the line where the call to \cs{marginalia} is located.
691 %
692 % \item[\val{c}] The baseline of the marginal content item will be placed centrally between top and bottom of the
693 % contents as a whole.
694 %
695 % \item[\val{m}] The baseline of the marginal content item will be midway between the baselines of the topmost and
696 % bottommost boxes in its contents. Thus, if the marginal content comprises an \emph{odd} number of
697 % \emph{equally-spaced} lines, the baseline of the middle line will be vertically aligned with the line where the call
698 % to \cs{marginalia} is located.
699 %
700 % \end{vallist}
701 % These values are illustrated in \fullref{Figure}{fig:example-valign} If \keyvalue{type}{normal}, then the alignments
702 % described above for \val{t}, \val{b}, \val{m} may not hold because of automatic adjustment. \default{\val{t}}
703 %
704 % \begin{figure}[p]
705 % \lstinputlisting{marginalia-doc-example-valign.tex}
706 % \begin{tikzpicture}
707 % \pgfmathsetmacro{\s}{(\textwidth+.4pt)/210mm}
708 % \pgfmathsetlengthmacro{\w}{\s*210mm}
709 % \pgfmathsetlengthmacro{\h}{\s*110mm}
710 % \path[save path=\outline] (0,0) -| ++(\w,-\h) -| cycle;
711 % \begin{scope}
712 % \clip[use path=\outline];
713 % \node[anchor=north west,inner sep=0] at (0,0) {\includegraphics[scale=\s]{marginalia-doc-example-valign.pdf}};
714 % \end{scope}
715 % \draw[gray,use path=\outline];
716 % \end{tikzpicture}
717 %
718 % \caption{A demonstration of the various values of \key{valign}.}
719 % \label{fig:example-valign}
720 % \end{figure}
721 %
722 % The option \keyvalue{valign}{c} may be useful if the marginal content contains a picture, which would sit on a single
723 % oversized line. Conversely, the option \keyvalue{valign}{m} may be useful if one is wants textual alignment with the
724 % body text.
725 %
726 %
727 % \medskip\goodbreak
728 %
729 %
730 % \DescribeOption{yshift}
731 % The key \key{yshift} is used to shift the default position of the marginal content item up (positive) or
732 % down (negative) from its normal position, which is to have its baseline aligned with the baseline of the callout
733 % position. It must be set to a valid dimension. Note that if \keyvalue{type}{normal}, then the vertical
734 % position may be adjusted from that specified by \key{yshift}. If this is not desired, specify a different \key{type}.
735 % \default{0pt}.
736 %
737 %
738 % \medskip\goodbreak
739 %
740 %
741 % \DescribeOption{ysep}
742 % \DescribeOption{ysep above}
743 % \DescribeOption{ysep below}
744 % \DescribeOption{ysep page top}
745 % \DescribeOption{ysep page bottom}
746 % These keys specify the minimum vertical separation above and below an item of marginal content
747 % (see \fullref{Figure}{fig:ysep-explanation}).
748 % \begin{vallist}
749 % \item[\key{ysep above}] the minimum vertical separation between an item and the one above.
750 % \default{value of \cs{marginparpush} at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
751 % \item[\key{ysep below}] the minimum vertical separation between an item and the one below.
752 % \default{value of \cs{marginparpush} at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
753 % \item[\key{ysep page top}] the minimum vertical separation between an item and top of the page.
754 % \default{margin above main textblock at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
755 % \item[\key{ysep page bottom}] the minimum vertical separation between an item and bottom of the page.
756 % \default{margin below main textblock at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
757 % \item[\key{ysep above below}] is a shorthand for setting both \key{ysep above} and \key{ysep below} simultaneously
758 % to the same value.
759 % \item[\key{ysep}] is a shorthand for setting all of these keys simultaneously to the same value.
760 % \end{vallist}
761 % Each of these keys must be set to a valid dimension.
762 %
763 % \begin{figure}[t]
764 % \centering
765 % \includegraphics{marginalia-doc-ysep-explanation.pdf}
766 % \caption{(Illustration of \key{ysep}) The length \examplelabel{1} is at least the value of \key{ysep below}
767 % specified (locally or globally) for marginal content item \examplelabel{A} and at least the value of \key{ysep
768 % above} specified for item \examplelabel{B}. In this example diagram, \examplelabel{B} has been automatically moved
769 % down from its natural position to maintain the required distance. Similarly, the length \examplelabel{2} is at least
770 % the value of \key{ysep below} specified for \examplelabel{C} and at least the value of \key{ysep above} specified
771 % for \examplelabel{D}, and the length \examplelabel{3} is at least the value of \key{ysep page bottom} specified for
772 % \examplelabel{D}. In this example, to maintain the required distances, \examplelabel{C} and \examplelabel{D} have
773 % been automatically moved (respectively) up and down from their natural positions.}
774 % \label{fig:ysep-explanation}
775 % \end{figure}
776 %
777 % \medskip\goodbreak
778 %
779 % \DescribeOption{ysep page top margin}
780 % \DescribeOption{ysep page bottom margin}
781 % \DescribeOption{ysep page top bottom~margin}
782 % These keys automatically set vertical separation between an item of marginal content and the top and bottom of the
783 % page to match the main textblock.
784 % \begin{vallist}
785 % \item[\key{ysep page top margin}] Automatically set \key{ysep page top} to match the margins above the main
786 % textblock; to be precise, \key{ysep page top} is set to the value of
787 % \(\qty{1}{\inch} + \cs{voffset} + \cs{topmargin} + \cs{headheight} + \cs{headsep}\).
788 % \item[\key{ysep page bottom margin}] Automatically set \key{ysep page bottom} to match the margins above the main
789 % textblock; to be precise, \key{ysep page bottom} is set to the value of
790 % \(\cs{paperheight} - (\qty{1}{\inch} + \cs{voffset} + \cs{topmargin} + \cs{headheight} + \cs{headsep}) - \cs{textheight}\).
791 % \item[\key{ysep page top bottom margin}] Automatically set \key{ysep page top} and \key{ysep page bottom} to match
792 % the margins above and below the main textblock; has the same effect as specifying \key{ysep page top margin} and
793 % \key{ysep page bottom margin} separately.
794 % \end{vallist}
795 % None of these keys takes a value. Note that if the sizes of the top and bottom margins are changed, the values of
796 % \key{ysep page top} and \key{ysep page bottom} do not change automatically, even if these options have been used. The
797 % options can of course be used immediately after the new margins have be set.
798 %
799 %
800 %
801 % \subsection{Appearance}
802 %
803 % \begin{table}[t]
804 % \caption{Summary of keys affecting the appearance of the marginal content. (For keys affecting the type or position
805 % of marginal content, see \fullref{Table}{tbl:keys-type-position}.) All keys can be set using \cs{marginaliasetup} or
806 % passed in the optional argument to \cs{marginalia}.}
807 % \label{tbl:keys-appearance}
808 % \leavevmode\strut\kern\hsize
809 % \llap{^^A
810 % \begin{tabular*}{\hsize+30mm}{p{50mm}p{60mm}l}
811 % \toprule
812 % Key name & Value & Default \\
813 % \midrule
814 % \key{width} & Dimension & \val{\cs{marginparwidth}} \\
815 % \key{width outer} & Dimension & \val{\cs{marginparwidth}} \\
816 % \key{width inner} & Dimension & \val{\cs{marginparwidth}} \\
817 % \key{width between} & Dimension & \val{\cs{marginparwidth}} \\
818 % \key{width recto outer} & Dimension & \val{\cs{marginparwidth}} \\
819 % \key{width recto inner} & Dimension & \val{\cs{marginparwidth}} \\
820 % \key{width verso outer} & Dimension & \val{\cs{marginparwidth}} \\
821 % \key{width verso inner} & Dimension & \val{\cs{marginparwidth}} \\
822 % \key{width right between} & Dimension & \val{\cs{marginparwidth}} \\
823 % \key{width left between} & Dimension & \val{\cs{marginparwidth}} \\
824 % \midrule
825 % \key{style} & \LaTeX\ code & [Empty] \\
826 % \key{style recto outer} & \LaTeX\ code & [Empty] \\
827 % \key{style recto inner} & \LaTeX\ code & [Empty] \\
828 % \key{style verso outer} & \LaTeX\ code & [Empty] \\
829 % \key{style verso inner} & \LaTeX\ code & [Empty] \\
830 % \key{style right between} & \LaTeX\ code & [Empty] \\
831 % \key{style left between} & \LaTeX\ code & [Empty] \\
832 % \midrule
833 % \key{mark} & \LaTeX\ code & [Empty] \\
834 % \key{mark recto outer} & \LaTeX\ code & [Empty] \\
835 % \key{mark recto inner} & \LaTeX\ code & [Empty] \\
836 % \key{mark verso outer} & \LaTeX\ code & [Empty] \\
837 % \key{mark verso inner} & \LaTeX\ code & [Empty] \\
838 % \key{mark right between} & \LaTeX\ code & [Empty] \\
839 % \key{mark left between} & \LaTeX\ code & [Empty] \\
840 % \bottomrule
841 % \end{tabular*}^^A
842 % }
843 % \end{table}
844 %
845 % An item of marginal content that appears in the inner margin might be narrower than one that appears in the outer
846 % margin, and an item appearing in the outer margin of a recto page might be set ragged right, while an item appearing
847 % in the outer margin of a verso page might be set ragged left. And since it is not known where an item will appear
848 % until the page is assembled, the keys in this subsection, dealing with the width and style of an item, have variants
849 % that apply depending on where the item appears on the page.
850 %
851 %
852 % \medskip\goodbreak
853 %
854 %
855 % \DescribeOption{width}
856 % \DescribeOption{width outer}
857 % \DescribeOption{width inner}
858 % \DescribeOption{width between}
859 % \DescribeOption{width recto outer}
860 % \DescribeOption{width recto inner}
861 % \DescribeOption{width verso outer}
862 % \DescribeOption{width verso inner}
863 % \DescribeOption{width right between}
864 % \DescribeOption{width left between}
865 % These keys specify the width of the an item of marginal content (or, more precisely, the \cs{hsize} of the box into
866 % which the item is typeset). Which width is chosen will depend on the where the item is typeset. The terminology is as
867 % in \fullref{Figure}{fig:terminology}.
868 % \begin{vallist}
869 % \item[\key{width recto outer}] used for an item in the outer margin of a recto page.
870 % \item[\key{width recto inner}] used for an item in the inner margin of a recto page.
871 % \item[\key{width verso outer}] used for an item in the outer margin of a verso page.
872 % \item[\key{width verso inner}] used for an item in the inner margin of a verso page.
873 % \item[\key{width right between}] used for an item set from the right column and placed between the columns.
874 % \item[\key{width left between}] used for an item set from the right column and placed between the columns.
875 % \item[\key{width outer}] a shorthand for setting the keys \key{width recto outer} and \key{width verso outer}
876 % simultaneously to the same value.
877 % \item[\key{width inner}] a shorthand for setting the keys \key{width recto inner} and \key{width verso inner}
878 % simultaneously to the same value.
879 % \item[\key{width between}] a shorthand for setting the keys \key{width right between} and \key{width left between}
880 % simultaneously to the same value.
881 % \item[\key{width}] a shorthand for setting all of these keys simultaneously.
882 % \end{vallist}
883 % (The shorthands \key{width outer} and \key{width inner} exist because page geometry is usually symmetrical between
884 % recto and verso pages as regards outer and inner margins. The shorthand \key{width between} exists because the space
885 % between columns, if used at all for marginal content, will often be shared equally.) Each of these keys must be set to
886 % a valid dimension. \default{value of \cs{marginparwidth} at \cs{begin}\texttt{\{}\env{document}\texttt{\}}}
887 %
888 %
889 % \medskip\goodbreak
890 %
891 %
892 % \DescribeOption{style}
893 % \DescribeOption{style recto outer}
894 % \DescribeOption{style recto inner}
895 % \DescribeOption{style verso outer}
896 % \DescribeOption{style verso inner}
897 % \DescribeOption{style right between}
898 % \DescribeOption{style left between}
899 % These keys specify the style with which an item of marginal content is typeset. Which style is chosen will depend on
900 % where the item is typeset. The terminology is as in \fullref{Figure}{fig:terminology}.
901 % \begin{vallist}
902 % \item[\key{style recto outer}] used for an item in the outer margin of a recto page.
903 % \item[\key{style recto inner}] used for an item in the inner margin of a recto page.
904 % \item[\key{style verso outer}] used for an item in the outer margin of a verso page.
905 % \item[\key{style verso inner}] used for an item in the inner margin of a verso page.
906 % \item[\key{style right between}] used for an item set from the right column between the columns.
907 % \item[\key{style left between}] used for an item set from the right column between the columns.
908 % \item[\key{style}] a shorthand for setting all of these keys simultaneously.
909 % \end{vallist}
910 % Each of these keys should be set to \LaTeX\ code that specifies the style. \default{[Empty]}
911 %
912 %
913 %
914 % \medskip\goodbreak
915 %
916 %
917 % \DescribeOption{mark}
918 % \DescribeOption{mark recto outer}
919 % \DescribeOption{mark recto inner}
920 % \DescribeOption{mark verso outer}
921 % \DescribeOption{mark verso inner}
922 % \DescribeOption{mark right between}
923 % \DescribeOption{mark left between}
924 % These keys specify code to typeset something alongside the line where the call to \cs{marginalia} appears, on the same
925 % side on which the marginal content is placed. Roughly speaking, they can be set to any \LaTeX\ code, from a single
926 % symbol to a \TikZ\ picture. Which code is chosen will depend on where the marginal item is typeset. If the marginal
927 % item is placed on the right of the text, the \key{mark} code will be executed in an \cs{rlap} flush with the right end
928 % of the line; if the marginal item is placed on the left of the text, the \key{mark} code will be executed in an
929 % \cs{llap} flush with the left end of the line. These could be used to place arrowheads or more complicated indicators
930 % associating a marginal item with the text; see \fullref{Figure}{fig:example-mark}
931 %
932 % The terminology for the various \key{mark} options is as in
933 % \fullref{Figure}{fig:terminology}.
934 % \begin{vallist}
935 % \item[\key{mark recto outer}] used for an item in the outer margin of a recto page.
936 % \item[\key{mark recto inner}] used for an item in the inner margin of a recto page.
937 % \item[\key{mark verso outer}] used for an item in the outer margin of a verso page.
938 % \item[\key{mark verso inner}] used for an item in the inner margin of a verso page.
939 % \item[\key{mark right between}] used for an item set from the right column between the columns.
940 % \item[\key{mark left between}] used for an item set from the right column between the columns.
941 % \item[\key{mark}] a shorthand for setting all of these keys simultaneously.
942 % \end{vallist}
943 % Each of these keys should be set to \LaTeX\ code. \default{[Empty]}
944 %
945 %
946 %
947 % \begin{figure}[p]
948 % \lstinputlisting{marginalia-doc-example-mark.tex}
949 % \begin{tikzpicture}
950 % \pgfmathsetmacro{\s}{(\textwidth+.4pt)/210mm}
951 % \pgfmathsetlengthmacro{\w}{\s*210mm}
952 % \pgfmathsetlengthmacro{\h}{\s*95mm}
953 % \path[save path=\outline] (0,0) -| ++(\w,-\h) -| cycle;
954 % \begin{scope}
955 % \clip[use path=\outline];
956 % \node[anchor=north west,inner sep=0] at (0,0) {\includegraphics[scale=\s]{marginalia-doc-example-mark.pdf}};
957 % \end{scope}
958 % \draw[gray,use path=\outline];
959 % \end{tikzpicture}
960 % \caption{A demonstration of the \key{mark} option, in particular showing how it can be used to draw a \TikZ\ arrow
961 % from a (moved) note to the relevant line. Notes: (1)~The code in the marginal content is processed \emph{before} the
962 % mark, so the \key{mark} \TikZ\ code refers to a coordinate defined in the marginal content, not the other way
963 % around. (2)~The code must be compiled \emph{three} times to give this output, because the \texttt{arrowstart}
964 % coordinate is not placed correctly until after \pkg{marginalia} has placed the second marginal note on the second
965 % run. Thus one more run is necessary for \TikZ\ to draw the arrow correctly.}
966 % \label{fig:example-mark}
967 % \end{figure}
968 %
969 %
970 %
971 % \section{Placement}
972 % \label{sec:placement}
973 %
974 % The placement of an item of marginal content depends on where the call to \cs{marginalia} appears in the finished
975 % document. Both horizontal and vertical placement can be complicated.
976 %
977 %
978 %
979 % \subsection{Horizontal placement}
980 %
981 % To understand the horizontal placement, first recall some terminology: a recto page is an odd-numbered page in
982 % two-sided mode, or any page in one-sided mode; a verso page is an even-numbered page in two-sided mode. The
983 % description in the paragraphs that follow is summarized in \fullref{Figure}{fig:terminology}.
984 %
985 % \begin{figure}[t]
986 % \centering
987 % \begin{tikzpicture}[
988 % x={.45*\textwidth},
989 % y={sqrt(2)*.45*\textwidth},
990 % ]
991 %
992 % \begin{scope}[
993 % every node/.style ={
994 % node font=\footnotesize\scshape,
995 % align=center,
996 % }
997 % ]
998 % \begin{scope}
999 % \clip[decorate,decoration={snake,amplitude=1mm,segment length=5mm}] (-1,0) -- (1,0) -- (1,.5) -| cycle;
1000 % \begin{scope}[fill=lightgray]
1001 % \fill (-.7,-.35) rectangle (-.2,.35);
1002 % \node at (-.45,.175) {one\\column};
1003 % \fill (.7,-.35) rectangle (.2,.35);
1004 % \node at (.45,.175) {one\\column};
1005 % \end{scope}
1006 % \end{scope}
1007 %
1008 % \begin{scope}
1009 % \clip[decorate,decoration={snake,amplitude=1mm,segment length=5mm}] (-1,0) -- (1,0) -- (1,-.5) -| cycle;
1010 % \begin{scope}[fill=lightgray]
1011 % \fill (-.85,-.35) rectangle (-.65,.35);
1012 % \node at (-.75,-.175) {left\\column};
1013 % \fill (-.35,-.35) rectangle (-.15,.35);
1014 % \node at (-.25,-.175) {right\\column};
1015 % \fill (.35,-.35) rectangle (.15,.35);
1016 % \node at (.25,-.175) {left\\column};
1017 % \fill (.85,-.35) rectangle (.65,.35);
1018 % \node at (.75,-.175) {right\\column};
1019 % \end{scope}
1020 % \end{scope}
1021 %
1022 % \end{scope}
1023 %
1024 % \pgfresetboundingbox
1025 %
1026 % \draw[white,line width=1pt,decorate,decoration={snake,amplitude=1mm,segment length=5mm}] (-1,0) -- (1,0);
1027 %
1028 % \draw (0,-.5) -- (0,.5);
1029 % \draw (-1,-.5) rectangle (1,.5);
1030 %
1031 % \begin{scope}[
1032 % every node/.style={
1033 % node font=\footnotesize\ttfamily,
1034 % inner xsep=3pt,
1035 % }
1036 % ]
1037 % \node[anchor=north west,align=left] at (.7,.35) {auto\\right};
1038 % \node[anchor=north east,align=right] at (.2,.35) {reverse\\left};
1039 %
1040 % \node[anchor=south east,align=right] at (.15,-.35) {auto\\left};
1041 % \node[anchor=north,shift={(.03,0)},align=left] at (.35,-.35) {reverse\\right};
1042 % \node[anchor=north,shift={(-.03,0)},align=right] at (.65,-.35) {reverse\\left};
1043 % \node[anchor=south west,align=left] at (.85,-.35) {auto\\\strut\smash{right}};
1044 %
1045 % \node[anchor=north east,align=right] at (-.7,.35) {auto\\left};
1046 % \node[anchor=north west,align=left] at (-.2,.35) {reverse\\right};
1047 %
1048 % \node[anchor=south east,align=right] at (-.85,-.35) {auto\\left};
1049 % \node[anchor=north,shift={(.03,0)},align=left] at (-.65,-.35) {reverse\\right};
1050 % \node[anchor=north,shift={(-.03,0)},align=right] at (-.35,-.35) {reverse\\left};
1051 % \node[anchor=south west,align=left] at (-.15,-.35) {auto\\right};
1052 %
1053 % \end{scope}
1054 %
1055 % \begin{scope}[
1056 % every node/.style={
1057 % node font=\small\ttfamily,
1058 % inner xsep=6pt,
1059 % }
1060 % ]
1061 % \node[rotate=90,anchor=north] at (-1,0) {verso outer};
1062 % \node[rotate=90,anchor=north east,align=right] at (-.65,0) {left\\[-1pt]between};
1063 % \node[rotate=-90,anchor=north west,align=left] at (-.35,0) {right\\[-1pt]between};
1064 % \node[rotate=-90,anchor=north] at (0,0) {verso inner};
1065 % \node[rotate=90,anchor=north] at (0,0) {recto inner};
1066 % \node[rotate=90,anchor=north east,align=right] at (.35,0) {left\\[-1pt]between};
1067 % \node[rotate=-90,anchor=north west,align=left] at (.65,0) {right\\[-1pt]between};
1068 % \node[rotate=-90,anchor=north] at (1,0) {recto outer};
1069 % \end{scope}
1070 %
1071 % \end{tikzpicture}
1072 % \caption{Summary of the positioning of marginal content using \key{pos}, and terminology used in \key{width} and
1073 % \key{style} keys, on recto and verso pages, in both one-column and two-column mode.}
1074 % \label{fig:terminology}
1075 % \end{figure}
1076 %
1077 %
1078 % In one-column mode, marginal content is placed by default in the outer margin: right on recto pages, left on verso
1079 % pages. If \keyvalue{pos}{reverse} is applied, it is placed in the inner margin: left on recto pages, right on verso
1080 % pages.
1081 %
1082 % In two-column mode, the default placement is next to the column in which the call to \cs{marginalia} appears, on the
1083 % side opposite to the other column. Thus, if the call to \cs{marginalia} was in the left column, the marginal content
1084 % item is placed by default on the left: on a recto page, the inner margin, on a verso page, the outer margin. If
1085 % \keyvalue{pos}{reverse} is applied, it is placed between the two columns, adjacent to the left column. If the call to
1086 % \cs{marginalia} was in the right column, the item is placed by default on the right: on a recto page, the
1087 % outer margin, on a verso page, the inner margin. If \keyvalue{pos}{reverse} is applied, it is placed between the two
1088 % columns, adjacent to the right column.
1089 %
1090 % \keyvalue{pos}{left} specifies that the item is to be placed on the left of the text block or column
1091 % containing the call to \cs{marginalia}.
1092 %
1093 % \keyvalue{pos}{right} similarly specifies that the item is to be placed on the right of the text block or column
1094 % containing the call to \cs{marginalia}.
1095 %
1096 % \pkg{marginalia} determines in which column the call to \cs{marginalia} was made using its horizontal position. As
1097 % discussed in the description of key \key{column}, there are situations where this can go wrong and which
1098 % necessitate a manual specification of a particular column.
1099 %
1100 %
1101 %
1102 % \subsection{Vertical placement}
1103 % \label{subsec:vertical-placement}
1104 %
1105 % \pkg{marginalia} tries by default to place the each item of marginal content with its baseline shifted by the value of
1106 % \key{yshift} (by default, \qty{0}{\point}) from the baseline where \cs{marginalia} was called. The actual vertical
1107 % placement is calculated by the procedure described below, carried out for the items appearing in a particular
1108 % horizontal location. (As shown in \fullref{Figure}{fig:terminology}, in one-column mode the possible locations are in
1109 % outer and inner margins; in two-column mode the possible locationd are the outer and inner margins and on the left and
1110 % right sides of the space between the columns.) A \emph{clash} exists when two items are closer than specified by
1111 % \key{ysep below} for the upper item or \key{ysep above} for the lower item, whichever is greater.
1112 %
1113 % For the items in each horizontal location, the procedure is as follows:
1114 % \begin{enumerate}
1115 % \item Place the items appearing in a given horizontal location on the page into a list.
1116 % \item Set the vertical shift of each item to the one specified by \key{yshift}.
1117 % \item For each \keyvalue{type}{optfixed} item, if it clashes with any \keyvalue{type}{fixed} item, delete it from
1118 % the list of items that appear on the page.
1119 % \item Sort the list by the position of the call to \cs{marginalia}, top-to-bottom, left-to-right, breaking ties
1120 % by the order of calls. (Because of floats, footnotes, etc., the sorted order of the list is not necessarily
1121 % the same as the order of appearance of \cs{marginalia} commands in the source code.)
1122 % \item Pass through the list of items in sorted order. For each \keyvalue{type}{normal} item, if necessary shift it
1123 % in a negative (downward) direction so that it
1124 % \begin{enumerate*}[label={(\arabic*)}]
1125 % \item does not reach closer to the top of the page than specified by \texttt{ysep page top}, and
1126 % \item does not clash with the previous (above) item.
1127 % \end{enumerate*}
1128 % (After this stage, it is possible for an assigned vertical shift to push a \keyvalue{type}{normal} item off
1129 % the bottom of the page.)
1130 % \item Pass through the list of items in the reverse of the sorted order. For each \keyvalue{type}{normal} item, if
1131 % necessary shift it in a positive (upward) direction so that it
1132 % \begin{enumerate*}[label={(\arabic*)}]
1133 % \item does not reach closer to the bottom of the page than specified by \texttt{ysep page bottom}, and
1134 % \item does not clash with the next (below) item.
1135 % \end{enumerate*}
1136 % \end{enumerate}
1137 % During this process, it may be found that it is impossible to prevent clashes or items reaching beyond the limits
1138 % (e.g. fixed items clash with each other; a fixed item conflicts with \texttt{ysep page top} or \texttt{ysep page
1139 % bottom}, or there are simply too many items of marginal content to fit (in which case, the top of some of them will be
1140 % above the limit specified by \texttt{ysep page top} or will clash with fixed items)). In these cases, warnings are
1141 % issued at the end of the Lua\LaTeX\ run.
1142 %
1143 %
1144 %
1145 % \section{Usage notes}
1146 % \label{sec:usage}
1147 %
1148 % \pkg{marginalia} requires a minimum of two Lua\LaTeX\ runs, and often more, to place items of marginal content
1149 % correctly. On the first pass, information about items, including their vertical size, is written to the \file{.aux}
1150 % file, and this information is used to position them correctly on the next run. However, because \key{width} and
1151 % \key{style} have variants dependent on the margin in which the item is placed, an item may only be typeset at the
1152 % correct size on this second run. Thus the vertical size of the item may have changed and so the information written to
1153 % the \file{.aux} file on the previous run may be out of date. In this case a third run may be needed for correct
1154 % placement.
1155 %
1156 % More runs may be needed if the position of the call to \cs{marginalia} changes between runs. (For example, if
1157 % \cs{marginalia} is used to set numbered sidenotes with per-page numbering, the number may change between runs and the
1158 % small difference in widths can change line breaks and page/column breaks.) Provided that the main text stabilizes, the
1159 % placement of items using \cs{marginalia} should be correct two runs later.
1160 %
1161 % At the end of the Lua\LaTeX\ run, \pkg{marginalia} reports any problems encountered in the vertical placement of items
1162 % (as decribed at the end of \fullref{Subsection}{subsec:vertical-placement}). These problems are based on calculations
1163 % made on the basis of information from the previous written to the \file{.aux} file on the previous run, and may not
1164 % arise if item positions or sizes (i.e. height or depth) have changed. \pkg{marginalia} also reports any changes in
1165 % positions or sizes compared to the previous run.
1166 %
1167 % In these reports, a page number refers to a visible page number if it is prefixed with `\texttt{p}'; it otherwise
1168 % refers to the absolute page number of the output.
1169 %
1170 %
1171 %
1172 % \section{Incompatibilities}
1173 %
1174 % Using \pkg{marginalia} alongside \cs{marginpar} or packages like \pkg{mparhak}, \pkg{marginnote}, \pkg{marginfix}, or
1175 % \pkg{marginfit} should not produce any errors, but \pkg{marginalia} will ignore marginal content not created using
1176 % \cs{marginalia}; for example, an item of marginal content created using \cs{marginalia} might overlap with one created
1177 % using \cs{marginpar}.
1178 %
1179 %
1180 %
1181 % \section{Limitations}
1182 % \label{sec:limitations}
1183 %
1184 % As noted in the introduction, \pkg{marginalia} was originally written to typeset a particular kind of book. It thus
1185 % has several limitations. Three of these are:
1186 % \begin{description}
1187 % \item[Lua\LaTeX only] Most of the code for deciding the placement of items of marginal content is written in Lua.
1188 % In principle, it could be replaced with a pure \LaTeX\ solution.
1189 % \item[No support for `moving past' fixed items] The adjustment of vertical positions will never cause a
1190 % \keyvalue{type}{normal} item to be shifted past a \keyvalue{type}{fixed} one, even when there is space on
1191 % the other side. It may be desirable to have this available as an option.
1192 % \item[No support for nested content items] Nesting might be desirable for typesetting editions of manuscripts
1193 % which sometimes contain marginal glosses, and then glosses upon those glosses.
1194 % \end{description}
1195 %
1196 % The lack of any built-in facility for producing (for example) numbered sidenotes is a conscious design choice. This is
1197 % properly the concern of a command that merely uses \cs{marginalia} to place the notes correctly.
1198 %
1199 %
1200 %
1201 % ^^A\bibliography{\jobname}
1202 % ^^A\bibliographystyle{alphaabbrv}
1203 %
1204 % \begin{thebibliography}{Cai24}
1205 %
1206 % \bibitem[Bri04]{bringhurst_elements}
1207 % R.~Bringhurst.
1208 % \newblock \emph{The Elements of Typographic Style}.
1209 % \newblock Hartley {\&} Marks, version 3.0, 2004.
1210 %
1211 % \bibitem[Cai24]{cain_formandnumber_ebook_large}
1212 % A.~J. Cain.
1213 % \newblock \emph{Form \& Number: A History of Mathematical Beauty}.
1214 % \newblock Lisbon, 2024.
1215 % \newblock {\scshape url:}
1216 % \href{https://archive.org/details/cain_formandnumber_ebook_large}{\nolinkurl{https://archive.org/details/cain_formandnumber_ebook_large}}.
1217 %
1218 % \bibitem[Cai25]{c_marginalia}
1219 % A.~J. Cain
1220 % \newblock `\texttt{marginalia} at work: Running heads, float captions, citations, and small figures in the margins'.
1221 % \newblock \textit{TUGboat}. The Communications of the \TeX\ Users Group. 46, no.1 (2025), pp.49--53.
1222 % \newblock {\scshape doi:}
1223 % \href{https://dx.doi.org/10.47397/tb/46-1/tb142cain-marginalia}{\nolinkurl{10.47397/tb/46-1/tb142cain-marginalia}}
1224 %
1225 % \end{thebibliography}
1226 %
1227 %
1228 %
1229 % \end{documentation}
1230 %
1231 %
1232 %
1233 % \iffalse
1234 %<*example-getting-started>
1235 \documentclass[11pt,a4paper]{article}
1236
1237 \usepackage{marginalia}
1238
1239 \begin{document}
1240
1241 Here is some body text.\marginalia{Here is a marginal note.} Some more
1242 body text.\marginalia[style=\footnotesize\itshape\raggedright]{Here is another
1243 marginal note, set in smaller text and italics, whose position has been been
1244 adjusted automatically.}
1245
1246 \vspace{20mm}
1247
1248 Some final body text after a space.\marginalia[pos=left, valign=b,
1249 style=\sffamily\raggedleft, width=35mm]{This note is placed on the left side
1250 of the page, wider, in sans serif, ragged left, and bottom-aligned.}
1251
1252 \end{document}
1253 %</example-getting-started>
1254 %
1255 %
1256 %
1257 %<*example-valign>
1258 \documentclass[11pt,a4paper]{article}
1259
1260 \usepackage{marginalia}
1261 \marginaliasetup{
1262 ysep page top=0mm,
1263 style recto outer=\raggedright\footnotesize,
1264 style recto inner=\raggedleft\footnotesize,
1265 width=30mm,
1266 }
1267
1268 \begin{document}
1269
1270 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod%
1271 \marginalia[valign=t]{{\large\ttfamily valign=t}\\The baseline of this marginal note
1272 is the baseline of the its top line.}%
1273 \marginalia[valign=b,pos=reverse]{{\large\ttfamily valign=b}\\The baseline of this
1274 marginal note is the baseline of its bottom line.}
1275
1276 \vspace{30mm}
1277
1278 Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris
1279 nisi%
1280 \marginalia[valign=c,pos=reverse]{{\large\ttfamily valign=c}\\The baseline of this
1281 marginal note is midway between the top and bottom of its content.}%
1282 \marginalia[valign=m]{{\large\ttfamily valign=m}\\The baseline of this marginal note
1283 is midway between the baseline of its top line and the baseline of its bottom line.}
1284
1285 \end{document}
1286 %</example-valign>
1287 %
1288 %
1289 %
1290 %<*example-mark>
1291 \documentclass[11pt,a4paper]{article}
1292
1293 \usepackage{marginalia}
1294 \marginaliasetup{
1295 xsep=5mm, style=\raggedright\sffamily,
1296 }
1297
1298 \usepackage{tikz}
1299 \newcommand\drawarrow{%
1300 \tikz[remember picture,overlay]
1301 \draw[->,thick] (arrowstart) -- ++(-1.5mm,0) |- (.5mm,.5ex);%
1302 }
1303 \newcommand\savearrowstart{%
1304 \tikz[remember picture,overlay] \coordinate (arrowstart) at (-.5mm,.5ex);%
1305 }
1306
1307 \begin{document}
1308
1309 Lorem ipsum dolor sit amet,%
1310 \marginalia[mark={~$\triangleleft$}]{A marginal note. (Extra text to push down the
1311 note below.)} consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
1312 labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
1313 ullamco laboris nisi ut aliquip ex ea commodo consequat.%
1314 \marginalia[mark={\drawarrow}]{\savearrowstart Another marginal note.}
1315 Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
1316 fugiat nulla pariatur.
1317
1318 \end{document}
1319 %</example-mark>
1320 % \fi
1321 %
1322 %
1323 %
1324 % \clearpage
1325 % \begin{implementation}
1326 %
1327 %
1328 %
1329 % \section{Implementation (\LaTeX\ package)}
1330 %
1331 % \begin{macrocode}
1332 %<*package>
1333 %<@@=marginalia>
1334 % \end{macrocode}
1335 %
1336 %
1337 %
1338 % \subsection{Initial set-up}
1339 %
1340 % Package identification/version information.
1341 % \begin{macrocode}
1342 \NeedsTeXFormat{LaTeX2e}[2020-02-02]
1343 \ProvidesExplPackage{marginalia}{2026-01-02}{0.83.20}
1344 {Non-floating marginal content for LuaLaTeX}
1345 % \end{macrocode}
1346 % Check that Lua\TeX\ is in use.
1347 % \begin{macrocode}
1348 \sys_if_engine_luatex:F
1349 {
1350 \msg_new:nnn{marginalia}{lualatex_required}
1351 {LuaLaTeX~required.~Package~loading~will~abort.}
1352 \msg_critical:nn{marginalia}{lualatex_required}
1353 }
1354 % \end{macrocode}
1355 %
1356 %
1357 %
1358 % \subsection{Tagging set-up}
1359 %
1360 % If \LaTeX\ has tagging support, set up sockets if necessary and define \cs{@@_tagging_socket:n} to be
1361 % \cs{UseTaggingSocket}.
1362 % \begin{macrocode}
1363 \@ifundefined{UseTaggingSocket}
1364 {
1365 \cs_new:Npn \@@_tagging_socket:n #1 {}
1366 }
1367 {
1368 \str_if_exist:cF { l__socket_tagsupport/marginpar/begin_plug_str }
1369 {
1370 \socket_new:nn {tagsupport/marginpar/begin}{0}
1371 \socket_new:nn {tagsupport/marginpar/end}{0}
1372 }
1373 \str_if_exist:cF { l__socket_tagsupport/para/restore_plug_str }
1374 {
1375 \socket_new:nn {tagsupport/para/restore}{0}
1376 }
1377 \cs_new:Npn \@@_tagging_socket:n #1
1378 {
1379 \UseTaggingSocket{#1}
1380 }
1381 }
1382 % \end{macrocode}
1383 %
1384 %
1385 %
1386 % \subsection{Auxiliary macro for dimension setting}
1387 %
1388 % \begin{macro}{
1389 % \@@_set_dim:Nn,
1390 % }
1391 % Set the dimension variable passed as the first parameter to value specified in second parameter at
1392 % \texttt{begindocument} if used in the preamble, or immediately (since \texttt{begindocument} is a one-time hook) in
1393 % the document.
1394 % \begin{macrocode}
1395 \cs_new:Nn \@@_set_dim:Nn
1396 {
1397 \hook_gput_code:nnn { begindocument } { marginalia/dim }
1398 {
1399 \dim_set:Nn #1 { #2 }
1400 }
1401 }
1402 % \end{macrocode}
1403 % \end{macro}
1404 %
1405 %
1406 %
1407 % \subsection{Auxiliary macros for setting options}
1408 %
1409 % \begin{macro}{\@@_setup_preamble:n}
1410 % Macro used to set the configuration in the preamble. This only has effect at the outer group level: inside a group,
1411 % options should be confined to that group, so adding the options that use the \texttt{begindocument} hook (via
1412 % \cs{@@_set_dim:Nn}) should have no effect. And since \cs{marginalia} cannot be used in the preamble, setting other
1413 % options inside a group in the preamble is pointless.
1414 % \begin{macrocode}
1415 \cs_new:Npn \@@_setup_preamble:n #1
1416 {
1417 \int_if_zero:nT{ \currentgrouplevel }
1418 {
1419 \keys_set:nn{marginalia}{ #1 }
1420 }
1421 }
1422 % \end{macrocode}
1423 % \end{macro}
1424 %
1425 % \begin{macro}{\@@_setup_body:n}
1426 % Macro used to set the configuration in the document body.
1427 % \begin{macrocode}
1428 \cs_new:Npn \@@_setup_body:n #1
1429 {
1430 \keys_set:nn{marginalia}{ #1 }
1431 }
1432 % \end{macrocode}
1433 % \end{macro}
1434 %
1435 % \begin{macro}{\@@_setup:n}
1436 % The macro \cs{@@_setup:n} is defined to be \cs{@@_setup_preamble:n} initially and is redefined to
1437 % \cs{@@_setup_body:n} at \texttt{begindocument/end}.
1438 % \begin{macrocode}
1439 \cs_set_eq:NN\@@_setup:n\@@_setup_preamble:n
1440 % \end{macrocode}
1441 % \begin{macrocode}
1442 \hook_gput_code:nnn{ begindocument/end }{ marginalia/setup }
1443 {
1444 \cs_set_eq:NN\@@_setup:n\@@_setup_body:n
1445 }
1446 % \end{macrocode}
1447 % \end{macro}
1448 %
1449 %
1450 %
1451 % \subsection{Options}
1452 %
1453 % Set up the key--value options and the variables in which the settings will be stored.
1454 %
1455 %
1456 %
1457 % \subsubsection{Type}
1458 %
1459 % \begin{macro}{
1460 % \l_@@_type_int,
1461 % }
1462 % A key to store the type of the marginal content item. The setting is held in an integer variable:
1463 % \(1 = \key{normal}\), \(2 = \key{fixed}\), \(3 = \key{optfixed}\).
1464 % \begin{macrocode}
1465 \int_new:N\l_@@_type_int
1466 \keys_define:nn { marginalia }
1467 {
1468 type .choices:nn = {normal,fixed,optfixed}{
1469 \int_set:Nn\l_@@_type_int{\l_keys_choice_int}
1470 },
1471 type .initial:n = normal,
1472 }
1473 % \end{macrocode}
1474 % \end{macro}
1475 %
1476 %
1477 %
1478 % \subsubsection{Horizontal placement}
1479 %
1480 % \begin{macro}{
1481 % \l_@@_pos_int,
1482 % }
1483 % A key to store the specified position of the marginal content item. The setting is held in an integer variable:
1484 % \(1 = \key{auto}\), (the outer margin in one-column mode; left margin in left column, right margin in right column
1485 % in two-column mode) \(2 = \key{reverse}\) (inner margin in one-column mode; between the columns in two-column mode),
1486 % \(3 = \key{left}\), \(4 = \key{right}\), \(5 = \key{nearest}\).
1487 % \begin{macrocode}
1488 \int_new:N\l_@@_pos_int
1489 \keys_define:nn { marginalia }
1490 {
1491 pos .choices:nn = {auto,reverse,left,right,nearest}{
1492 \int_set:Nn\l_@@_pos_int{\l_keys_choice_int}
1493 },
1494 pos .initial:n = auto
1495 }
1496 % \end{macrocode}
1497 % \end{macro}
1498 %
1499 % \begin{macro}{
1500 % \l_@@_column_int,
1501 % }
1502 % A key to force the marginal content item to be treated in one-column mode or as being set from the left or right
1503 % column. The setting is held in an integer variable: \(-1 = \key{auto}\) (automatic), \(0 = \key{one}\) (one-column
1504 % mode), \(1 = \key{left}\) (left column) \(2 = \key{right}\) (right column).
1505 % \begin{macrocode}
1506 \int_new:N\l_@@_column_int
1507 \keys_define:nn { marginalia }
1508 {
1509 column .choices:nn = {auto,one,left,right}{
1510 \int_set:Nn\l_@@_column_int{\l_keys_choice_int-2}
1511 },
1512 column .initial:n = auto,
1513 }
1514 % \end{macrocode}
1515 % \end{macro}
1516 %
1517 % \begin{macro}{
1518 % \l_@@_xsep_recto_outer_dim,
1519 % \l_@@_xsep_recto_inner_dim,
1520 % \l_@@_xsep_verso_outer_dim,
1521 % \l_@@_xsep_verso_inner_dim,
1522 % \l_@@_xsep_right_between_dim,
1523 % \l_@@_xsep_left_between_dim,
1524 % }
1525 % Dimension keys to hold the separation between the marginal content item and the main text, which can be dependent on
1526 % where it appears on the page.
1527 % \begin{macrocode}
1528 \dim_new:N\l_@@_xsep_recto_outer_dim
1529 \dim_new:N\l_@@_xsep_recto_inner_dim
1530 \dim_new:N\l_@@_xsep_verso_outer_dim
1531 \dim_new:N\l_@@_xsep_verso_inner_dim
1532 \dim_new:N\l_@@_xsep_right_between_dim
1533 \dim_new:N\l_@@_xsep_left_between_dim
1534 \keys_define:nn { marginalia }
1535 {
1536 xsep~recto~outer .code:n
1537 = \@@_set_dim:Nn\l_@@_xsep_recto_outer_dim{#1},
1538 xsep~recto~inner .code:n
1539 = \@@_set_dim:Nn\l_@@_xsep_recto_inner_dim{#1},
1540 xsep~verso~outer .code:n
1541 = \@@_set_dim:Nn\l_@@_xsep_verso_outer_dim{#1},
1542 xsep~verso~inner .code:n
1543 = \@@_set_dim:Nn\l_@@_xsep_verso_inner_dim{#1},
1544 xsep~right~between .code:n
1545 = \@@_set_dim:Nn\l_@@_xsep_right_between_dim{#1},
1546 xsep~left~between .code:n
1547 = \@@_set_dim:Nn\l_@@_xsep_left_between_dim{#1},
1548 xsep .code:n = {
1549 \keys_set:nn{ marginalia }{
1550 xsep~recto~outer=#1,
1551 xsep~recto~inner=#1,
1552 xsep~verso~outer=#1,
1553 xsep~verso~inner=#1,
1554 xsep~right~between=#1,
1555 xsep~left~between=#1,
1556 }
1557 },
1558 xsep~outer .code:n = {
1559 \keys_set:nn{ marginalia }{
1560 xsep~recto~outer=#1,
1561 xsep~verso~outer=#1,
1562 }
1563 },
1564 xsep~inner .code:n = {
1565 \keys_set:nn{ marginalia }{
1566 xsep~recto~inner=#1,
1567 xsep~verso~inner=#1,
1568 }
1569 },
1570 xsep~between .code:n = {
1571 \keys_set:nn{ marginalia }{
1572 xsep~right~between=#1,
1573 xsep~left~between=#1,
1574 }
1575 },
1576 xsep .initial:n = \marginparsep,
1577 }
1578 % \end{macrocode}
1579 % \end{macro}
1580 %
1581 %
1582 %
1583 % \subsubsection{Vertical placement}
1584 %
1585 % \begin{macro}{
1586 % \l_@@_valign_int,
1587 % }
1588 % A key to store the vertical alignment of the marginal content item. The setting is held in a integer variable:
1589 % \(1 = \key{t}\) (aligned at the baseline of the topmost line of the item), \(2 = \key{b}\) (aligned at the baseline
1590 % of the bottommost line of the item).
1591 % \begin{macrocode}
1592 \int_new:N\l_@@_valign_int
1593 \keys_define:nn { marginalia }
1594 {
1595 valign .choices:nn = {t,b,c,m}{
1596 \int_set_eq:NN\l_@@_valign_int\l_keys_choice_int
1597 },
1598 valign .initial:n = t,
1599 }
1600 % \end{macrocode}
1601 % \end{macro}
1602 %
1603 % \begin{macro}{
1604 % \l_@@_default_yshift_dim,
1605 % }
1606 % Dimension key to hold the default vertical shift of the marginal content item from its natural position.
1607 % \begin{macrocode}
1608 \keys_define:nn { marginalia }
1609 {
1610 yshift .dim_set:N = \l_@@_default_yshift_dim,
1611 yshift .initial:n = 0pt,
1612 }
1613 % \end{macrocode}
1614 % \end{macro}
1615 %
1616 % \begin{macro}{
1617 % \@@_margin_top:,
1618 % \@@_margin_bottom:
1619 % }
1620 % These macros are simply the calculations necessary for the space above and below the main textblock. They are simply
1621 % a convenience to avoid specifying the calcuation twice in the definition of the \key{ysep} keys.
1622 % \begin{macrocode}
1623 \cs_new:Npn \@@_margin_top:
1624 {
1625 1in + \voffset + \topmargin + \headheight + \headsep
1626 }
1627 \cs_new:Npn \@@_margin_bottom:
1628 {
1629 \pageheight - 1in - \voffset - \topmargin - \headheight - \headsep
1630 - \textheight
1631 }
1632 % \end{macrocode}
1633 % \end{macro}
1634 %
1635 % \begin{macro}{
1636 % \l_@@_ysep_above_dim,
1637 % \l_@@_ysep_below_dim,
1638 % \l_@@_ysep_page_top_dim,
1639 % \l_@@_ysep_page_bottom_dim,
1640 % }
1641 % Dimension keys to hold the the minimum vertical spacing between a marginal content item and (respectively) the item
1642 % above, the item below, the page top, and the page bottom.
1643 % \begin{macrocode}
1644 \dim_new:N\l_@@_ysep_above_dim
1645 \dim_new:N\l_@@_ysep_below_dim
1646 \dim_new:N\l_@@_ysep_page_top_dim
1647 \dim_new:N\l_@@_ysep_page_bottom_dim
1648 \keys_define:nn { marginalia }
1649 {
1650 ysep~above .code:n
1651 = \@@_set_dim:Nn\l_@@_ysep_above_dim{#1},
1652 ysep~below .code:n
1653 = \@@_set_dim:Nn\l_@@_ysep_below_dim{#1},
1654 ysep~page~top .code:n
1655 = \@@_set_dim:Nn\l_@@_ysep_page_top_dim{#1},
1656 ysep~page~bottom .code:n
1657 = \@@_set_dim:Nn\l_@@_ysep_page_bottom_dim{#1},
1658 ysep~above~below .code:n = {
1659 \keys_set:nn{ marginalia }{
1660 ysep~below=#1,
1661 ysep~above=#1,
1662 }
1663 },
1664 ysep .code:n = {
1665 \keys_set:nn{ marginalia }{
1666 ysep~below=#1,
1667 ysep~above=#1,
1668 ysep~page~top=#1,
1669 ysep~page~bottom=#1,
1670 }
1671 },
1672 ysep~page~top~margin .code:n = {
1673 \keys_set:nn{ marginalia }{
1674 ysep~page~top
1675 = \@@_margin_top:
1676 }
1677 },
1678 ysep~page~bottom~margin .code:n = {
1679 \keys_set:nn{ marginalia }{
1680 ysep~page~bottom
1681 = \@@_margin_bottom:
1682 }
1683 },
1684 ysep~page~top~bottom~margin .code:n = {
1685 \keys_set:nn{ marginalia }{
1686 ysep~page~top~margin,
1687 ysep~page~bottom~margin,
1688 }
1689 },
1690 ysep~above~below .initial:n = \marginparpush,
1691 ysep~page~top .initial:n = \@@_margin_top:,
1692 ysep~page~bottom .initial:n = \@@_margin_bottom:,
1693 }
1694 % \end{macrocode}
1695 % \end{macro}
1696 %
1697 %
1698 %
1699 % \subsubsection{Appearance}
1700 %
1701 % \begin{macro}{
1702 % \l_@@_width_recto_outer_dim,
1703 % \l_@@_width_recto_inner_dim,
1704 % \l_@@_width_verso_outer_dim,
1705 % \l_@@_width_verso_inner_dim,
1706 % \l_@@_width_right_between_dim,
1707 % \l_@@_width_left_between_dim,
1708 % }
1709 % Dimension keys to hold the width of the marginal content item, which can be dependent on where it appears on the
1710 % page.
1711 % \begin{macrocode}
1712 \dim_new:N\l_@@_width_recto_outer_dim
1713 \dim_new:N\l_@@_width_recto_inner_dim
1714 \dim_new:N\l_@@_width_verso_outer_dim
1715 \dim_new:N\l_@@_width_verso_inner_dim
1716 \dim_new:N\l_@@_width_right_between_dim
1717 \dim_new:N\l_@@_width_left_between_dim
1718 \keys_define:nn { marginalia }
1719 {
1720 width~recto~outer .code:n
1721 = \@@_set_dim:Nn\l_@@_width_recto_outer_dim{#1},
1722 width~recto~inner .code:n
1723 = \@@_set_dim:Nn\l_@@_width_recto_inner_dim{#1},
1724 width~verso~outer .code:n
1725 = \@@_set_dim:Nn\l_@@_width_verso_outer_dim{#1},
1726 width~verso~inner .code:n
1727 = \@@_set_dim:Nn\l_@@_width_verso_inner_dim{#1},
1728 width~right~between .code:n
1729 = \@@_set_dim:Nn\l_@@_width_right_between_dim{#1},
1730 width~left~between .code:n
1731 = \@@_set_dim:Nn\l_@@_width_left_between_dim{#1},
1732 width .code:n = {
1733 \keys_set:nn{ marginalia }{
1734 width~recto~outer=#1,
1735 width~recto~inner=#1,
1736 width~verso~outer=#1,
1737 width~verso~inner=#1,
1738 width~right~between=#1,
1739 width~left~between=#1,
1740 }
1741 },
1742 width~outer .code:n = {
1743 \keys_set:nn{ marginalia }{
1744 width~recto~outer=#1,
1745 width~verso~outer=#1,
1746 }
1747 },
1748 width~inner .code:n = {
1749 \keys_set:nn{ marginalia }{
1750 width~recto~inner=#1,
1751 width~verso~inner=#1,
1752 }
1753 },
1754 width~between .code:n = {
1755 \keys_set:nn{ marginalia }{
1756 width~right~between=#1,
1757 width~left~between=#1,
1758 }
1759 },
1760 width .initial:n = \marginparwidth,
1761 }
1762 % \end{macrocode}
1763 % \end{macro}
1764 %
1765 % \begin{macro}{
1766 % \l_@@_style_recto_outer_tl,
1767 % \l_@@_style_recto_inner_tl,
1768 % \l_@@_style_verso_outer_tl,
1769 % \l_@@_style_verso_inner_tl,
1770 % \l_@@_style_right_between_tl,
1771 % \l_@@_style_left_between_tl,
1772 % }
1773 % Token list keys to hold the style with which a marginal content item is typeset, which can be dependent on where it
1774 % appears on the page.
1775 % \begin{macrocode}
1776 \keys_define:nn { marginalia }
1777 {
1778 style~recto~outer .tl_set:N = \l_@@_style_recto_outer_tl,
1779 style~recto~inner .tl_set:N = \l_@@_style_recto_inner_tl,
1780 style~verso~outer .tl_set:N = \l_@@_style_verso_outer_tl,
1781 style~verso~inner .tl_set:N = \l_@@_style_verso_inner_tl,
1782 style~right~between .tl_set:N = \l_@@_style_right_between_tl,
1783 style~left~between .tl_set:N = \l_@@_style_left_between_tl,
1784 style .code:n = {
1785 \keys_set:nn{ marginalia }{
1786 style~recto~outer=#1,
1787 style~recto~inner=#1,
1788 style~verso~outer=#1,
1789 style~verso~inner=#1,
1790 style~right~between=#1,
1791 style~left~between=#1,
1792 }
1793 },
1794 style .initial:n = {},
1795 }
1796 % \end{macrocode}
1797 % \end{macro}
1798 %
1799 % \begin{macro}{
1800 % \l_@@_mark_recto_outer_tl,
1801 % \l_@@_mark_recto_inner_tl,
1802 % \l_@@_mark_verso_outer_tl,
1803 % \l_@@_mark_verso_inner_tl,
1804 % \l_@@_mark_right_between_tl,
1805 % \l_@@_mark_left_between_tl,
1806 % }
1807 % Token list keys to hold code for a mark to be placed adjacent to the line where the call to\cs{marginalia} is
1808 % located, on the same side as the marginal content item.
1809 % \begin{macrocode}
1810 \keys_define:nn { marginalia }
1811 {
1812 mark~recto~outer .tl_set:N = \l_@@_mark_recto_outer_tl,
1813 mark~recto~inner .tl_set:N = \l_@@_mark_recto_inner_tl,
1814 mark~verso~outer .tl_set:N = \l_@@_mark_verso_outer_tl,
1815 mark~verso~inner .tl_set:N = \l_@@_mark_verso_inner_tl,
1816 mark~right~between .tl_set:N = \l_@@_mark_right_between_tl,
1817 mark~left~between .tl_set:N = \l_@@_mark_left_between_tl,
1818 mark .code:n = {
1819 \keys_set:nn{ marginalia }{
1820 mark~recto~outer=#1,
1821 mark~recto~inner=#1,
1822 mark~verso~outer=#1,
1823 mark~verso~inner=#1,
1824 mark~right~between=#1,
1825 mark~left~between=#1,
1826 }
1827 },
1828 mark .initial:n = {},
1829 }
1830 % \end{macrocode}
1831 % \end{macro}
1832 %
1833 %
1834 %
1835 % \subsection{Lua backend and interface}
1836 %
1837 % Load the Lua backend.
1838 % \begin{macrocode}
1839 \lua_now:n{
1840 marginalia = require('marginalia')
1841 }
1842 % \end{macrocode}
1843 %
1844 % The following 9 macros interface between \LaTeX\ and Lua code. Each control sequence \cs[no-index]{@@_lua_XYZ}
1845 % simply calls the corresponding Lua function \luafunc{marginalia.XYZ}.
1846 % \begin{macro}{
1847 % \@@_lua_store_default_page_data:,
1848 % \@@_lua_store_page_data:n,
1849 % \@@_lua_check_page_data:n,
1850 % \@@_lua_store_item_data:n,
1851 % \@@_lua_check_item_data:n,
1852 % \@@_lua_compute_items:,
1853 % \@@_lua_write_problem_report:,
1854 % \@@_lua_write_item_change_report:,
1855 % }
1856 % The first 8 macros do not require expansion of parameters: they either have none, or process data not containing
1857 % control sequences (read from the \file{.aux} file); hence \cs{lua_now:n} is used.
1858 % \begin{macrocode}
1859 \cs_new:Npn\@@_lua_store_default_page_data:
1860 {
1861 \lua_now:n{ marginalia.store_default_page_data() }
1862 }
1863 \cs_new:Npn\@@_lua_store_page_data:n #1
1864 {
1865 \lua_now:n{ marginalia.store_page_data('#1') }
1866 }
1867 \cs_new:Npn\@@_lua_check_page_data:n #1
1868 {
1869 \lua_now:n{ marginalia.check_page_data('#1') }
1870 }
1871 \cs_new:Npn\@@_lua_write_page_change_report:
1872 {
1873 \lua_now:n{ marginalia.write_page_change_report() }
1874 }
1875 \cs_new:Npn\@@_lua_store_item_data:n #1
1876 {
1877 \lua_now:n{ marginalia.store_item_data('#1') }
1878 }
1879 \cs_new:Npn\@@_lua_check_item_data:n #1
1880 {
1881 \lua_now:n{ marginalia.check_item_data('#1') }
1882 }
1883 \cs_new:Npn\@@_lua_compute_items:
1884 {
1885 \lua_now:n{ marginalia.compute_items() }
1886 }
1887 \cs_new:Npn\@@_lua_write_problem_report:
1888 {
1889 \lua_now:n{ marginalia.write_problem_report() }
1890 }
1891 \cs_new:Npn\@@_lua_write_item_change_report:
1892 {
1893 \lua_now:n{ marginalia.write_item_change_report() }
1894 }
1895 % \end{macrocode}
1896 % \end{macro}
1897 %
1898 % \begin{macro}{
1899 % \@@_lua_load_item_data:n,
1900 % }
1901 % The last macro will receive a control sequence parameter and so requires expansion; hence
1902 % \cs{lua_now:e} is used.
1903 % \begin{macrocode}
1904 \cs_new:Npn\@@_lua_load_item_data:n #1
1905 {
1906 \lua_now:e{ marginalia.load_item_data('#1') }
1907 }
1908 % \end{macrocode}
1909 % \end{macro}
1910 %
1911 %
1912 %
1913 % \subsection{Processing data from the \texorpdfstring{\file{.aux}}{.aux} file}
1914 %
1915 % \begin{macro}[int]{
1916 % \marginalia@pagedata,
1917 % }
1918 % This command is used to store version information in the \file{.aux} file. It currently does nothing, but may be
1919 % used in future to avoid errors if changes are made in the format of the data written to the \file{.aux} file.
1920 % \begin{macrocode}
1921 \cs_new:Npn \marginalia@version #1
1922 {}
1923 % \end{macrocode}
1924 % \end{macro}
1925 %
1926 % \begin{macro}[int]{
1927 % \marginalia@pagedata,
1928 % }
1929 % This command is used to store page data in the \file{.aux} file.
1930 % \begin{macrocode}
1931 \cs_new:Npn \marginalia@pagedata #1
1932 {
1933 \@@_process_page_data:n{#1}
1934 }
1935 % \end{macrocode}
1936 % Initially \cs{@@_process_page_data:n} is set to \cs{@@_lua_store_page_data:n}. Thus, when the \file{.aux} file is
1937 % read, \cs{marginalia@pagedata} will pass the page data to the Lua backend to be stored.
1938 % \begin{macrocode}
1939 \cs_set_eq:NN
1940 \@@_process_page_data:n
1941 \@@_lua_store_page_data:n
1942 % \end{macrocode}
1943 % \end{macro}
1944 %
1945 % \begin{macro}[int]{
1946 % \marginalia@itemdata,
1947 % }
1948 % This command is used to store data for each marginal content item in the \file{.aux} file.
1949 % \begin{macrocode}
1950 \cs_new:Npn \marginalia@itemdata #1
1951 {
1952 \@@_process_item_data:n{#1}
1953 }
1954 % \end{macrocode}
1955 % \end{macro}
1956 % Initially \cs{@@_process_item_data:n} is set to \cs{@@_lua_store_item_data:n}. Thus, when the \file{.aux} file is
1957 % read, \cs{marginalia@itemdata} will pass the item data to the Lua backend to be stored.
1958 % \begin{macrocode}
1959 \cs_set_eq:NN
1960 \@@_process_item_data:n
1961 \@@_lua_store_item_data:n
1962 % \end{macrocode}
1963 % At the \texttt{begindocument} hook, the \file{.aux} file has been read and closed. The Lua backend now stores the
1964 % geometry and computes the vertical shift for each item. Then the handle for the main \file{.aux} file is stored for
1965 % use in this package.
1966 % \begin{macrocode}
1967 \hook_gput_code:nnn{ begindocument }{ marginalia/prepare }{
1968 \@@_lua_store_default_page_data:
1969 \@@_lua_compute_items:
1970 \cs_set_eq:NN\l_@@_aux_iow\@mainaux
1971 }
1972 % \end{macrocode}
1973 % The \texttt{enddocument/afterlastpage} hook is before the \file{.aux} file is read back, so this is where
1974 % \cs{@@_process_page_data:n} and \cs{@@_process_item_data:n} are set, respectively, to \cs{@@_lua_check_page_data:n}
1975 % and \cs{@@_lua_check_item_data:n}. Thus, when the \file{.aux} file is read back, \cs{marginalia@pagedata} and
1976 % \cs{marginalia@itemdata} will pass data to the Lua backend to be checked for changes.
1977 % \begin{macrocode}
1978 \hook_gput_code:nnn{enddocument/afterlastpage}{ marginalia/check }{
1979 \cs_set_eq:NN
1980 \@@_process_page_data:n
1981 \@@_lua_check_page_data:n
1982 \cs_set_eq:NN
1983 \@@_process_item_data:n
1984 \@@_lua_check_item_data:n
1985 }
1986 % \end{macrocode}
1987 % \begin{macro}{\@@_write_reports:}
1988 % All the reports of changes and/or problems are assembled in the Lua backend. This macro will write the reports as
1989 % package warnings, using the following three messages, to which the Lua-assembled reports are passed as parameters:
1990 % \begin{macrocode}
1991 \msg_new:nnn{marginalia}{placement_problem}
1992 { Problems~in~placement.~#1 }
1993 \msg_new:nnn{marginalia}{item_change}
1994 { Changes~in~item~data.~Rerun~to~get~correct~placement.~#1 }
1995 \msg_new:nnn{marginalia}{page_change}
1996 { Changes~in~page~data.~Rerun~to~get~correct~placement.~#1 }
1997 \cs_new:Npn\@@_write_reports:
1998 {
1999 \group_begin:
2000 \tl_set:Ne\l_tmpa_tl{\@@_lua_write_problem_report:}
2001 \tl_if_blank:VF\l_tmpa_tl
2002 {
2003 \msg_warning:nne{marginalia}{placement_problem}{\tl_use:N\l_tmpa_tl}
2004 }
2005 \tl_set:Ne\l_tmpa_tl{\@@_lua_write_item_change_report:}
2006 \tl_if_blank:VF\l_tmpa_tl
2007 {
2008 \msg_warning:nne{marginalia}{item_change}{\tl_use:N\l_tmpa_tl}
2009 }
2010 \tl_set:Ne\l_tmpa_tl{\@@_lua_write_page_change_report:}
2011 \tl_if_blank:VF\l_tmpa_tl
2012 {
2013 \msg_warning:nne{marginalia}{page_change}{\tl_use:N\l_tmpa_tl}
2014 }
2015 \group_end:
2016 }
2017 % \end{macrocode}
2018 % \end{macro}
2019 % Use the \texttt{enddocument/info} hook to write the reports of changes and/or problems.
2020 % \begin{macrocode}
2021 \hook_gput_code:nnn{ enddocument/info }{ marginalia/report } {
2022 \@@_write_reports:
2023 }
2024 % \end{macrocode}
2025 %
2026 %
2027 %
2028 % \subsection{Writing page data to the \texorpdfstring{\file{.aux}}{.aux} file}
2029 %
2030 % \begin{macro}{\@@_write_version:}
2031 % This command will be used to write the package version to the \file{.aux} file.
2032 % \begin{macrocode}
2033 \cs_new:Npn\@@_write_version:
2034 {
2035 \iow_now:Ne\l_@@_aux_iow{
2036 \token_to_str:N\marginalia@version{
2037 \use:c{[email protected]}
2038 }
2039 }
2040 }
2041 % \end{macrocode}
2042 % \end{macro}
2043 %
2044 % To compute the positions of marginal content items, certain page layout data is required. And since all the
2045 % computation takes place at the beginning of the document, it is necessary to write this information to the \file{.aux}
2046 % file.
2047 %
2048 % \begin{macro}{\g_@@_pagedatano_int}
2049 % Global integer variable to index page data items written to the \file{.aux} file.
2050 % \begin{macrocode}
2051 \int_new:N\g_@@_pagedatano_int
2052 % \end{macrocode}
2053 % \end{macro}
2054 %
2055 % \begin{macro}{\@@_write_page_data:}
2056 % This command will be used to write the current page data to the \file{.aux} file. It is initially defined to do
2057 % nothing, so that the use of \cs{marginalianewgeometry} in the preamble does not cause errors (because the
2058 % \file{.aux} file is not available for writing until \texttt{begindocument/end}).
2059 % \begin{macrocode}
2060 \cs_set_eq:NN\@@_write_page_data:\prg_do_nothing:
2061 \cs_new:Npn\@@_write_page_data_real:
2062 {
2063 \int_gincr:N\g_@@_pagedatano_int
2064 \legacy_if:nTF{@twocolumn}
2065 { \int_set:Nn\l_tmpa_int{2} }
2066 { \int_set:Nn\l_tmpa_int{1} }
2067 \iow_now:Ne\l_@@_aux_iow{
2068 \token_to_str:N\marginalia@pagedata{
2069 pagedatano=\int_value:w\g_@@_pagedatano_int,
2070 abspageno=\int_eval:n{\g_shipout_readonly_int+1},
2071 hoffset=\int_value:w\hoffset,
2072 voffset=\int_value:w\voffset,
2073 pageheight=\int_value:w\pageheight,
2074 oddsidemargin=\int_value:w\oddsidemargin,
2075 evensidemargin=\int_value:w\evensidemargin,
2076 textwidth=\int_value:w\textwidth,
2077 columncount=\int_value:w\l_tmpa_int,
2078 columnwidth=\int_value:w\columnwidth,
2079 columnsep=\int_value:w\columnsep,
2080 twoside=\bool_to_str:n{\legacy_if_p:n{@twoside}},
2081 }
2082 }
2083 }
2084 % \end{macrocode}
2085 % At the \texttt{begindocument/end} hook, the \file{.aux} file has been opened for writing, and so the macro
2086 % \cs{@@_write_page_data:} is enabled and the initial page data is written out.
2087 % \begin{macrocode}
2088 \hook_gput_code:nnn{ begindocument/end }{ marginalia/initial }
2089 {
2090 \@@_write_version:
2091 \cs_set_eq:NN
2092 \@@_write_page_data:
2093 \@@_write_page_data_real:
2094 \@@_write_page_data:
2095 }
2096 % \end{macrocode}
2097 % \end{macro}
2098 %
2099 %
2100 %
2101 % \subsection{Marginal content item processing}
2102 %
2103 % \subsubsection{Variables}
2104 %
2105 % \paragraph{Variables set by \LaTeX.}
2106 %
2107 % \begin{macro}{\g_@@_itemno_int}
2108 % Global integer variable to index marginal content items.
2109 % \begin{macrocode}
2110 \int_new:N\g_@@_itemno_int
2111 % \end{macrocode}
2112 % \end{macro}
2113 %
2114 % \begin{macro}{\l_@@_item_box}
2115 % Box variable to hold the typeset marginal content item.
2116 % \begin{macrocode}
2117 \box_new:N\l_@@_item_box
2118 % \end{macrocode}
2119 % \end{macro}
2120 %
2121 % \begin{macro}{
2122 % \l_@@_item_height_dim,
2123 % \l_@@_item_depth_dim,
2124 % }
2125 % Dimension variables to hold the height and depth of the typeset margin content item.
2126 % \begin{macrocode}
2127 \dim_new:N\l_@@_item_height_dim
2128 \dim_new:N\l_@@_item_depth_dim
2129 % \end{macrocode}
2130 % \end{macro}
2131 %
2132 %
2133 %
2134 % \paragraph{Variables set by Lua.}
2135 %
2136 % The following variables will be set by the Lua backend via \texttt{tex.count} and \texttt{tex.dimen} when
2137 % \cs{@@_lua_load_item_data:n} is called.
2138 %
2139 % \begin{macro}{\l_@@_page_int}
2140 % Integer variable for the page on which the marginal content item appears. This variable will be
2141 % made available via \cs{marginaliapage} within the \meta{content} of \cs{marginalia}.
2142 % \begin{macrocode}
2143 \int_new:N\l_@@_page_int
2144 % \end{macrocode}
2145 % \end{macro}
2146 %
2147 % \begin{macro}{\l_@@_column_computed_int}
2148 % Integer variable for the column next to which the marginal content item appears. This variable will be
2149 % will be made available via \cs{marginaliacolumn} within the \meta{content} of \cs{marginalia}.
2150 % \begin{macrocode}
2151 \int_new:N\l_@@_column_computed_int
2152 % \end{macrocode}
2153 % \end{macro}
2154 %
2155 % \begin{macro}{
2156 % \l_@@_xshift_computed_dim,
2157 % \l_@@_yshift_computed_dim,
2158 % }
2159 % Dimension variables to hold the differences in \(x\) and \(y\) coordinates between the call to \cs{marginalia} and
2160 % the position where the marginal content item should appear.
2161 % \begin{macrocode}
2162 \dim_new:N\l_@@_xshift_computed_dim
2163 \dim_new:N\l_@@_yshift_computed_dim
2164 % \end{macrocode}
2165 % \end{macro}
2166 %
2167 %
2168 % \begin{macro}{\l_@@_side_computed_int}
2169 % Integer variable to indicate the side of the text block or column on which the marginal content item should be
2170 % placed: \(0 = \textrm{right}\) and \(1 = \textrm{left}\).
2171 % \begin{macrocode}
2172 \int_new:N\l_@@_side_computed_int
2173 % \end{macrocode}
2174 % (This variable could be a boolean, but an integer is used because there is no canonical access to booleans from
2175 % Lua.)
2176 % \end{macro}
2177 %
2178 % \begin{macro}{\l_@@_marginno_computed_int}
2179 % Integer variable to indicate in which margin the content will be be placed, to enable quick selection of width and
2180 % style: \(0 = \textrm{recto outer}\), \(1 = \textrm{recto inner}\), \(2 = \textrm{verso outer}\), \(3 = \textrm{verso
2181 % inner}\), \(4 = \textrm{right between}\), \(5 = \textrm{left between}\).
2182 % \begin{macrocode}
2183 \int_new:N\l_@@_marginno_computed_int
2184 % \end{macrocode}
2185 % \end{macro}
2186 %
2187 % \begin{macro}{\l_@@_enabled_computed_int}
2188 % Integer variable to indicate whether the marginal content item is enabled: \(0 = \textrm{disabled}\),
2189 % \(1 = \textrm{enabled}\).
2190 % \begin{macrocode}
2191 \int_new:N\l_@@_enabled_computed_int
2192 % \end{macrocode}
2193 % (This variable could be a boolean, but an integer is used because there is no canonical access to booleans from
2194 % Lua.)
2195 % \end{macro}
2196 %
2197 %
2198 %
2199 % \subsubsection{Core macro}
2200 %
2201 % \begin{macro}{\@@_process_item:nn}
2202 % This macro does most of the work in setting the marginal content item. The first parameter is \meta{options}, the
2203 % second is \meta{content}.
2204 % \begin{macrocode}
2205 \cs_new:Npn\@@_process_item:nn #1#2
2206 {
2207 % \end{macrocode}
2208 % First, increment the index, then enter a group where all the action will happen.
2209 % \begin{macrocode}
2210 \int_gincr:N\g_@@_itemno_int
2211 \group_begin:
2212 % \end{macrocode}
2213 % Process \meta{options}. These settings apply locally inside the group.
2214 % \begin{macrocode}
2215 \keys_set:nn{marginalia}{ #1 }
2216 % \end{macrocode}
2217 % Get item data from the Lua backend: the integer variables \cs{l_@@_page_int}, \cs{l_@@_column_computed_int},
2218 % \cs{l_@@_side_computed_int}, \cs{l_@@_enabled_computed_int}, and the dimension variables
2219 % \cs{l_@@_xshift_computed_dim}, and \cs{l_@@_yshift_computed_dim} are set by Lua via \texttt{tex.count} and
2220 % \texttt{tex.dimen}. If no data is available (if, for instance, no data has been stored from a previous run), default
2221 % values will be set by Lua. On later runs, the Lua backend will supply the values computed from the data written to
2222 % the \file{.aux} file on the previous run.
2223 % \begin{macrocode}
2224 \@@_lua_load_item_data:n
2225 { \int_value:w\g_@@_itemno_int }
2226 % \end{macrocode}
2227 % Choose the correct auxiliary function for typesetting, depending on which mode \TeX\ is in.
2228 % \begin{macrocode}
2229 \mode_if_math:TF
2230 {
2231 \cs_set_eq:NN
2232 \@@_typeset:n
2233 \@@_typeset_mmode:n
2234 }
2235 {
2236 \legacy_if:nT{@inlabel}
2237 { \leavevmode }
2238 \mode_if_horizontal:TF
2239 {
2240 \cs_set_eq:NN
2241 \@@_typeset:n
2242 \@@_typeset_hmode:n
2243 }
2244 {
2245 \cs_set_eq:NN
2246 \@@_typeset:n
2247 \@@_typeset_vmode:n
2248 }
2249 }
2250 % \end{macrocode}
2251 % Choose the correct box in which to typeset the item for the desired vertical alignment, which has been stored in
2252 % \cs{l_@@_valign_int}.
2253 % \begin{macrocode}
2254 \int_case:nn{\l_@@_valign_int}
2255 {
2256 {1}{ \cs_set_eq:NN\@@_item_box_set:Nn\vbox_set_top:Nn }
2257 {2}{ \cs_set_eq:NN\@@_item_box_set:Nn\vbox_set:Nn }
2258 {3}{ \cs_set_eq:NN\@@_item_box_set:Nn\vbox_set_center:Nn }
2259 {4}{ \cs_set_eq:NN\@@_item_box_set:Nn\vbox_set_midway:Nn }
2260 }
2261 % \end{macrocode}
2262 % Choose the correct horizontal separation, width, style, and mark for the item.
2263 % \begin{macrocode}
2264 \@@_set_xsep_width_style_mark:
2265 % \end{macrocode}
2266 % Typeset the \meta{content} into \cs{l_@@_item_box}. Use \cs{@parboxrestore} for brevity, even though \cs{hsize} and
2267 % \cs{linewidth} are subsequently set to \cs{l_@@_width_dim}. Make available \cs{marginaliapage} and
2268 % \cs{marginaliacolumn}.
2269 % \begin{macrocode}
2270 \@@_tagging_socket:n {marginpar/begin}
2271 \@@_item_box_set:Nn\l_@@_item_box{
2272 \@parboxrestore
2273 \@@_tagging_socket:n {para/restore}
2274 \normalfont\normalsize
2275
2276 \tl_use:N\l_@@_style_tl
2277 \dim_set_eq:NN\hsize\l_@@_width_dim
2278 \dim_set_eq:NN\linewidth\hsize
2279
2280 \cs_set_eq:NN\marginaliapage\l_@@_page_int
2281 \cs_set_eq:NN\marginaliacolumn\l_@@_column_computed_int
2282
2283 \group_begin:
2284 \ignorespaces
2285 #2
2286 \par
2287 \group_end:
2288 }
2289 \@@_tagging_socket:n{marginpar/end}
2290 % \end{macrocode}
2291 % Measure \cs{l_@@_item_box}.
2292 % \begin{macrocode}
2293 \dim_set:Nn\l_@@_item_height_dim
2294 {\box_ht:N\l_@@_item_box}
2295 \dim_set:Nn\l_@@_item_depth_dim
2296 {\box_dp:N\l_@@_item_box}
2297 % \end{macrocode}
2298 % Everything is now ready to place the item on the page and write the necessary data to the \file{.aux} file. Use the
2299 % chosen auxiliary function for typesetting, and immediately use \cs{savepos} to store the callout position.
2300 % \begin{macrocode}
2301 \@@_typeset:n{
2302 \savepos
2303 % \end{macrocode}
2304 % Write the item data to the \file{.aux} file. All tokens that will change for future items, and which are currently
2305 % meaningful, are expanded now; the remainder will be expanded at shipout time, when \emph{they} are meaningful.
2306 % \begin{macrocode}
2307 \iow_shipout_e:Ne\l_@@_aux_iow{
2308 \token_to_str:N\marginalia@itemdata{
2309 itemno=\int_value:w\g_@@_itemno_int,
2310 abspageno=\exp_not:N\int_eval:n{\g_shipout_readonly_int},
2311 pageno=\exp_not:N\int_value:w\c@page,
2312 type=\str_use:N\int_value:w\l_@@_type_int,
2313 xpos=\exp_not:N\int_value:w\lastxpos,
2314 ypos=\exp_not:N\int_value:w\lastypos,
2315 height=\int_value:w\l_@@_item_height_dim,
2316 depth=\int_value:w\l_@@_item_depth_dim,
2317 pos=\int_value:w\l_@@_pos_int,
2318 column=\int_value:w\l_@@_column_int,
2319 yshift=\int_value:w\l_@@_default_yshift_dim,
2320 ysep~above=\int_value:w\l_@@_ysep_above_dim,
2321 ysep~below=\int_value:w\l_@@_ysep_below_dim,
2322 ysep~page~top=\int_value:w\l_@@_ysep_page_top_dim,
2323 ysep~page~bottom=\int_value:w\l_@@_ysep_page_bottom_dim,
2324 }
2325 }
2326 % \end{macrocode}
2327 % Finally, if the item is enabled, typeset it onto the page: shift the item by
2328 % \[
2329 % \abs[\big]{\cs{l_@@_xshift_computed_dim}} + \abs[\big]{\cs{l_@@_xsep_dim}}
2330 % \]
2331 % to the right or left (depending on \cs{l_@@_side_computed_int} in the appropriate overlap \cs{hbox}, then use
2332 % \cs{@@_place_item_box:} for the vertical placement.
2333 % \begin{macrocode}
2334 \int_if_zero:nF{\l_@@_enabled_computed_int}
2335 {
2336 \int_if_zero:nTF{\l_@@_side_computed_int}
2337 {
2338 \hbox_overlap_right:n{
2339 \kern\l_@@_xshift_computed_dim
2340 \tl_if_empty:NF\l_@@_mark_tl
2341 {
2342 \hbox_overlap_right:n{ \tl_use:N\l_@@_mark_tl }
2343 }
2344 \kern\l_@@_xsep_dim
2345 \@@_place_item_box:
2346 }
2347 }
2348 {
2349 \hbox_overlap_left:n{
2350 \@@_place_item_box:
2351 \kern\l_@@_xsep_dim
2352 \tl_if_empty:NF\l_@@_mark_tl
2353 {
2354 \hbox_overlap_left:n{ \tl_use:N\l_@@_mark_tl }
2355 }
2356 \kern-\l_@@_xshift_computed_dim
2357 }
2358 }
2359 }
2360 }
2361 % \end{macrocode}
2362 % Close the group started near the beginning of \cs{@@_process_item:nn}.
2363 % \begin{macrocode}
2364 \group_end:
2365 }
2366 % \end{macrocode}
2367 % \end{macro}
2368 %
2369 %
2370 %
2371 % \subsubsection{Horizontal separation, width, style, mark selection}
2372 %
2373 % \begin{macro}{\@@_set_xsep_width_style_mark:}
2374 % Set \cs{l_@@_xsep_dim}, \cs{l_@@_width_dim}, and \cs{l_@@_style_tl}, based on \cs{l_@@_marginno_computed_int}.
2375 % \begin{macrocode}
2376 \cs_new:Npn\@@_set_xsep_width_style_mark:
2377 {
2378 \int_case:nn{\l_@@_marginno_computed_int}
2379 {
2380 {0}
2381 {
2382 \cs_set_eq:NN\l_@@_xsep_dim
2383 \l_@@_xsep_recto_outer_dim
2384 \cs_set_eq:NN\l_@@_width_dim
2385 \l_@@_width_recto_outer_dim
2386 \cs_set_eq:NN\l_@@_style_tl
2387 \l_@@_style_recto_outer_tl
2388 \cs_set_eq:NN\l_@@_mark_tl
2389 \l_@@_mark_recto_outer_tl
2390 }
2391 {1}
2392 {
2393 \cs_set_eq:NN\l_@@_xsep_dim
2394 \l_@@_xsep_recto_inner_dim
2395 \cs_set_eq:NN\l_@@_width_dim
2396 \l_@@_width_recto_inner_dim
2397 \cs_set_eq:NN\l_@@_style_tl
2398 \l_@@_style_recto_inner_tl
2399 \cs_set_eq:NN\l_@@_mark_tl
2400 \l_@@_mark_recto_inner_tl
2401 }
2402 {2}
2403 {
2404 \cs_set_eq:NN\l_@@_xsep_dim
2405 \l_@@_xsep_verso_outer_dim
2406 \cs_set_eq:NN\l_@@_width_dim
2407 \l_@@_width_verso_outer_dim
2408 \cs_set_eq:NN\l_@@_style_tl
2409 \l_@@_style_verso_outer_tl
2410 \cs_set_eq:NN\l_@@_mark_tl
2411 \l_@@_mark_verso_outer_tl
2412 }
2413 {3}
2414 {
2415 \cs_set_eq:NN\l_@@_xsep_dim
2416 \l_@@_xsep_verso_inner_dim
2417 \cs_set_eq:NN\l_@@_width_dim
2418 \l_@@_width_verso_inner_dim
2419 \cs_set_eq:NN\l_@@_style_tl
2420 \l_@@_style_verso_inner_tl
2421 \cs_set_eq:NN\l_@@_mark_tl
2422 \l_@@_mark_verso_inner_tl
2423 }
2424 {4}
2425 {
2426 \cs_set_eq:NN\l_@@_xsep_dim
2427 \l_@@_xsep_right_between_dim
2428 \cs_set_eq:NN\l_@@_width_dim
2429 \l_@@_width_right_between_dim
2430 \cs_set_eq:NN\l_@@_style_tl
2431 \l_@@_style_right_between_tl
2432 \cs_set_eq:NN\l_@@_mark_tl
2433 \l_@@_mark_right_between_tl
2434 }
2435 {5}
2436 {
2437 \cs_set_eq:NN\l_@@_xsep_dim
2438 \l_@@_xsep_left_between_dim
2439 \cs_set_eq:NN\l_@@_width_dim
2440 \l_@@_width_left_between_dim
2441 \cs_set_eq:NN\l_@@_style_tl
2442 \l_@@_style_left_between_tl
2443 \cs_set_eq:NN\l_@@_mark_tl
2444 \l_@@_mark_left_between_tl
2445 }
2446 }
2447 }
2448 % \end{macrocode}
2449 % \end{macro}
2450 %
2451 %
2452 %
2453 % \subsubsection{Auxiliary box macros}
2454 %
2455 % \begin{macro}{\@@_vbox_set_center:Nn}
2456 % Typesets the second parameter at natural height and stores the result inside a box register supplied as the first
2457 % parameter, with the baseline being mid-way between the top and bottom of the box.
2458 % \begin{macrocode}
2459 \cs_new:Npn \vbox_set_center:Nn #1#2
2460 {
2461 \vbox_set_top:Nn #1{#2}
2462 \dim_set:Nn \l_tmpa_dim{ \box_ht:N#1 + \box_dp:N#1 }
2463 \box_set_ht:Nn #1 { .5\l_tmpa_dim }
2464 \box_set_dp:Nn #1 { .5\l_tmpa_dim }
2465 }
2466 % \end{macrocode}
2467 % \end{macro}
2468 %
2469 %
2470 %
2471 % \begin{macro}{\@@_vbox_set_midway:Nn}
2472 % Typesets the second parameter at natural height and stores the result inside a box register supplied as the first
2473 % parameter, with the baseline being mid-way between the top and bottom baselines of the items in the box.
2474 % \begin{macrocode}
2475 \cs_new:Npn \vbox_set_midway:Nn #1#2
2476 {
2477 % \end{macrocode}
2478 % The distance between the topmost and bottommost baselines is equal to the difference in depths between the results
2479 % of typesetting using a \cs{vtop} and a \cs{vbox}. So typeset into both (suspending tagging for the latter),
2480 % calculate the difference, and adjust the height and depth of the first.
2481 % \begin{macrocode}
2482 \vbox_set_top:Nn #1{#2}
2483 \tag_suspend:n{ marginalia }
2484 \vbox_set:Nn \l_tmpa_box {#2}
2485 \tag_resume:n{ marginalia }
2486 \dim_set:Nn \l_tmpa_dim{ \box_dp:N#1 - \box_dp:N\l_tmpa_box}
2487 \box_set_ht:Nn #1 { \box_ht:N#1 + .5\l_tmpa_dim }
2488 \box_set_dp:Nn #1 { \box_dp:N#1 - .5\l_tmpa_dim }
2489 }
2490 % \end{macrocode}
2491 % \end{macro}
2492 %
2493 %
2494 %
2495 % \subsubsection{Placement macros}
2496 %
2497 % \begin{macro}{\@@_place_item_box:}
2498 % Place the item that has been set in \cs{l_@@_item_box}, vertically shifted by \cs{l_@@_yshift_computed_dim} and
2499 % \cs{smash}ed to avoid altering vertical spacing in the main text.
2500 % \begin{macrocode}
2501 \cs_new:Npn\@@_place_item_box:
2502 {
2503 \smash
2504 {
2505 \box_move_up:nn{\l_@@_yshift_computed_dim}
2506 {
2507 \box_use:N\l_@@_item_box
2508 }
2509 }
2510 }
2511 % \end{macrocode}
2512 % \end{macro}
2513 %
2514 %
2515 %
2516 % \begin{macro}{
2517 % \@@_typeset_mmode:n,
2518 % \@@_typeset_hmmode:n,
2519 % \@@_typeset_vmode:n,
2520 % }
2521 % These three macros handle typsetting in math mode, horizontal mode, and vertical mode. Nothing special needs to be
2522 % done in math mode. In horizontal mode, \cs{@bsphack}\ldots\cs{@esphack} avoids double spacing. In vertical mode,
2523 % \cs{if@nobreak} is saved, a new paragraph is started, the item is typeset, the paragraph is ended, a vertical skip
2524 % of \(-\cs{baselineskip}\) is added, which should `hide' that invisible paragraph, and \cs{if@nobreak} is restored to
2525 % the saved value.
2526 % \begin{macrocode}
2527 \cs_new:Npn\@@_typeset_mmode:n #1
2528 {
2529 #1
2530 }
2531 \cs_new:Npn\@@_typeset_hmode:n #1
2532 {
2533 \@bsphack
2534 #1
2535 \@esphack
2536 }
2537 \bool_new:N\l_@@_nobreak_bool
2538 \cs_new:Npn\@@_typeset_vmode:n #1
2539 {
2540 \bool_set:Nn\l_@@_nobreak_bool{ \legacy_if_p:n{@nobreak} }
2541 \nobreak\noindent #1\par
2542 \skip_vertical:n{-\baselineskip}
2543 \legacy_if_gset:nn{ @nobreak }{ \l_@@_nobreak_bool }
2544 }
2545 % \end{macrocode}
2546 % \end{macro}
2547 %
2548 %
2549 %
2550 % \subsection{User commands}
2551 %
2552 % Finally, set up the commands for the user.
2553 %
2554 % \begin{macro}{\marginalia}
2555 % This is the main user command for creating a marginal content item. This macro does nothing but hand off to
2556 % \cs{@@_process_item:nn}.
2557 % \begin{macrocode}
2558 \NewDocumentCommand{\marginalia}{ O{} +m }
2559 {
2560 \@@_process_item:nn{#1}{#2}
2561 }
2562 % \end{macrocode}
2563 % \end{macro}
2564 %
2565 %
2566 %
2567 % \begin{macro}{\marginaliasetup}
2568 % The user command to set the configuration. This macro does nothing but hand off to
2569 % \cs{@@_setup:n}.
2570 % \begin{macrocode}
2571 \NewDocumentCommand{\marginaliasetup}{ m }
2572 {
2573 \@@_setup:n{ #1 }
2574 }
2575 % \end{macrocode}
2576 % \end{macro}
2577 %
2578 %
2579 %
2580 % \begin{macro}{\marginalianewgeometry}
2581 % The user command to signal that the page geometry has been changed.
2582 % \begin{macrocode}
2583 \NewDocumentCommand{\marginalianewgeometry}{}
2584 {
2585 \@@_write_page_data:
2586 }
2587 % \end{macrocode}
2588 % \end{macro}
2589 %
2590 %
2591 %
2592 % \begin{macrocode}
2593 %</package>
2594 % \end{macrocode}
2595 %
2596 %
2597 %
2598 % \section{Implementation (Lua backend)}
2599 %
2600 % \begin{macrocode}
2601 %<*lua>
2602 % \end{macrocode}
2603 %
2604 %
2605 %
2606 % \subsection{Global variables}
2607 %
2608 % Global tables for page_data and item_data.
2609 % \begin{macrocode}
2610 local PAGE_DATA_MAIN_TABLE = {}
2611 local ITEM_DATA_MAIN_TABLE = {}
2612 % \end{macrocode}
2613 % Global tables for compiling reports.
2614 % \begin{macrocode}
2615 local PROBLEM_REPORT_TABLE = {}
2616 local PAGE_CHANGE_REPORT_TABLE = {}
2617 local ITEM_CHANGE_REPORT_TABLE = {}
2618 % \end{macrocode}
2619 % Global configuration for reports.
2620 % \begin{macrocode}
2621 local PROBLEM_REPORT_MAX_LENGTH = 40
2622 local PAGE_CHANGE_REPORT_MAX_LENGTH = 10
2623 local ITEM_CHANGE_REPORT_MAX_LENGTH = 10
2624 % \end{macrocode}
2625 %
2626 %
2627 %
2628 % \subsection{Constants}
2629 %
2630 % Type constants. These match the possible values for the type key.
2631 % \begin{macrocode}
2632 local TYPE_NORMAL = 1
2633 local TYPE_FIXED = 2
2634 local TYPE_OPTFIXED = 3
2635 % \end{macrocode}
2636 % Position constants. These match the possible values for the pos key.
2637 % \begin{macrocode}
2638 local POS_AUTO = 1
2639 local POS_REVERSE = 2
2640 local POS_LEFT = 3
2641 local POS_RIGHT = 4
2642 local POS_NEAREST = 5
2643 % \end{macrocode}
2644 %
2645 %
2646 %
2647 % \subsection{Keys for tables}
2648 %
2649 % The strings listed in this subsection are constants used to index the tables. Also listed are the types of
2650 % values that are indexed by each key. Note that values listed below as "dimensions" are actually integers, giving the
2651 % dimension in \TeX\ scaled points (sp)
2652 %
2653 %
2654 %
2655 % \subsubsection{Keys for both page and item data tables}
2656 %
2657 % Integer: Absolute page number in output file (not on-page number), used in both page_data and item_data tables
2658 % \begin{macrocode}
2659 local KEY_ABSPAGENO = 'abspageno'
2660 % \end{macrocode}
2661 % Boolean: Used to mark page_data or item_data as checked when the .aux file is read back at the end of the document
2662 % \begin{macrocode}
2663 local KEY_CHECKED = 'checked'
2664 % \end{macrocode}
2665 %
2666 %
2667 %
2668 % \subsubsection{Keys for page data tables, layout etc.}
2669 %
2670 % Integer: Used only to distinguish instances of data written to .aux file
2671 % \begin{macrocode}
2672 local KEY_PAGEDATANO = 'pagedatano'
2673 % \end{macrocode}
2674 % Dimensions: Value of next two will always be equivalent of \qty{1}{\inch}, but it is simpler to keep all geometry
2675 % data together.
2676 % \begin{macrocode}
2677 local KEY_HOFFSETORIGIN = 'hoffsetorigin'
2678 local KEY_VOFFSETORIGIN = 'voffsetorigin'
2679 % \end{macrocode}
2680 % Dimensions: corresponding to obvious \LaTeX\ dimensions
2681 % \begin{macrocode}
2682 local KEY_HOFFSET = 'hoffset'
2683 local KEY_VOFFSET = 'voffset'
2684 local KEY_PAGEHEIGHT = 'pageheight'
2685 local KEY_ODDSIDEMARGIN = 'oddsidemargin'
2686 local KEY_EVENSIDEMARGIN = 'evensidemargin'
2687 local KEY_TEXTWIDTH = 'textwidth'
2688 local KEY_COLUMNWIDTH = 'columnwidth'
2689 local KEY_COLUMNSEP = 'columnsep'
2690 % \end{macrocode}
2691 % Integer: either \(1\) or \(2\), depending on whether \LaTeX\ was in one- or two-column mode
2692 % \begin{macrocode}
2693 local KEY_COLUMNCOUNT = 'columncount'
2694 % \end{macrocode}
2695 % Boolean: true iff \LaTeX\ is in twoside mode
2696 % \begin{macrocode}
2697 local KEY_TWOSIDE = 'twoside'
2698 % \end{macrocode}
2699 %
2700 %
2701 %
2702 % \subsubsection{Keys for item data tables}
2703 %
2704 % Integer: Used to identify data with item
2705 % \begin{macrocode}
2706 local KEY_ITEMNO = 'itemno'
2707 % \end{macrocode}
2708 % Integer: On-page number
2709 % \begin{macrocode}
2710 local KEY_PAGENO = 'pageno'
2711 % \end{macrocode}
2712 % Dimensions: \(x\) and \(y\) positions of call to \cs{marginalia}
2713 % \begin{macrocode}
2714 local KEY_XPOS = 'xpos'
2715 local KEY_YPOS = 'ypos'
2716 % \end{macrocode}
2717 % Dimensions: Height and depth of typeset item
2718 % \begin{macrocode}
2719 local KEY_HEIGHT = 'height'
2720 local KEY_DEPTH = 'depth'
2721 % \end{macrocode}
2722 % Integer: Specified type, following \luavar{TYPE_*}
2723 % \begin{macrocode}
2724 local KEY_TYPE = 'type'
2725 % \end{macrocode}
2726 % Integer: corresponds to value of \key{pos} key: \(0 = \texttt{auto}\), \(1 = \texttt{reverse}\), \(2 = \texttt{left}\),
2727 % \(3 = \texttt{right}\), \(4 = \texttt{nearest}\)
2728 % \begin{macrocode}
2729 local KEY_POS = 'pos'
2730 % \end{macrocode}
2731 % Integer: corresponds to value of \key{column} key: \(-1 = \texttt{auto}\), \(0 = \texttt{one}\), \(1 = \texttt{left}\),
2732 % \(2 = \texttt{right}\)
2733 % \begin{macrocode}
2734 local KEY_COLUMN = 'column'
2735 % \end{macrocode}
2736 % Dimension: specified vertical shift
2737 % \begin{macrocode}
2738 local KEY_YSHIFT = 'yshift'
2739 % \end{macrocode}
2740 % Dimensions: specified vertical separations
2741 % \begin{macrocode}
2742 local KEY_YSEP_ABOVE = 'ysep above'
2743 local KEY_YSEP_BELOW = 'ysep below'
2744 local KEY_YSEP_PAGE_TOP = 'ysep page top'
2745 local KEY_YSEP_PAGE_BOTTOM = 'ysep page bottom'
2746 % \end{macrocode}
2747 %
2748 % \medskip\noindent
2749 % The preceding keys refer to values that will be supplied from \LaTeX. The remaining values will be computed in Lua
2750 % and passed back to \LaTeX.
2751 %
2752 % \medskip\noindent
2753 % Integer: column in which the call to \cs{marginalia} was located: \(0 = \textrm{one-column}\),
2754 % \(1 = \textrm{left}\), \(2 = \textrm{right}\)
2755 % \begin{macrocode}
2756 local KEY_COLNO_COMPUTED = 'colno computed'
2757 % \end{macrocode}
2758 % Dimension: Horizontal shift between the call to \cs{marginalia} and the margin in which the item should be located
2759 % \begin{macrocode}
2760 local KEY_XSHIFT_COMPUTED = 'xshift computed'
2761 % \end{macrocode}
2762 % Dimension: Computed vertical shift
2763 % \begin{macrocode}
2764 local KEY_YSHIFT_COMPUTED = 'yshift computed'
2765 % \end{macrocode}
2766 % Integer: Side of text on which the item will appear: \(0 = \textrm{right}\), \(1 = \textrm{left}\)
2767 % \begin{macrocode}
2768 local KEY_SIDE_COMPUTED = 'side computed'
2769 % \end{macrocode}
2770 % Integer: Number of margin in which the item will appear, \(0 = \textrm{recto outer}\), \(1 = \textrm{recto inner}\),
2771 % \(2 = \textrm{verso outer}\), \(3 = \textrm{verso inner}\), \(4 = \textrm{ right between}\),
2772 % \(5 = \textrm{left between}\)
2773 % \begin{macrocode}
2774 local KEY_MARGINNO_COMPUTED = 'marginno computed'
2775 % \end{macrocode}
2776 % Boolean: Whether the item will actually appear on the page
2777 % \begin{macrocode}
2778 local KEY_ENABLED_COMPUTED = 'enabled computed'
2779 % \end{macrocode}
2780 %
2781 %
2782 %
2783 % \subsection{Utility functions}
2784 %
2785 % \begin{macro}[int]{list_filter}
2786 % Take a list \luavar{t} and remove from it any elements for which the function
2787 % \luavar{f} does not return true. (The index \luavar{j} is always the destination index to which a `keep' element
2788 % is moved.)\sidenote{Code adapted from \url{https://stackoverflow.com/a/53038524/8990243}.}
2789 % \begin{macrocode}
2790 local function list_filter(t, f)
2791 local j = 1
2792 local n = #t
2793
2794 for i=1,n do
2795 if (f(t[i])) then
2796 if (i ~= j) then
2797 t[j] = t[i]
2798 t[i] = nil
2799 end
2800 j = j + 1
2801 else
2802 t[i] = nil
2803 end
2804 end
2805
2806 end
2807 % \end{macrocode}
2808 % \end{macro}
2809 %
2810 % \begin{macro}[int]{list_filter}
2811 % Return boolean true iff \luavar{s} is exactly the string `\luavar{true}'.
2812 % \begin{macrocode}
2813 local function toboolean(s)
2814 return s == "true"
2815 end
2816 % \end{macrocode}
2817 % \end{macro}
2818 %
2819 % \begin{macro}[int]{get_data_page_number}
2820 % Take a item or page data and return a human-readable string indicating the page to which the data pertains.
2821 % \begin{macrocode}
2822 local function get_data_page_number(data)
2823 local pageno = data[KEY_PAGENO]
2824 if pageno ~= nil then
2825 return 'p' .. pageno .. ' (' .. data[KEY_ABSPAGENO] .. ')'
2826 else
2827 return data[KEY_ABSPAGENO]
2828 end
2829 end
2830 % \end{macrocode}
2831 % \end{macro}
2832 %
2833 %
2834 %
2835 % \subsection{Generic page/item data functions}
2836 %
2837 % \begin{macro}[int]{parse_data}
2838 % Parse \luavar{keyvalue_string} and return the corresponding data as a table. The \luavar{keyvalue_string} is
2839 % expected to be of precisely the kind written to the \file{.aux} file as the parameter of \cs{marginalia@pagedata} or
2840 % \cs{marginalia@notedata}.
2841 %
2842 % Ignore any keys in \luavar{keyvalue_string} that are not listed in \luavar{conversion_table}. Fill in any missing
2843 % value with values from \luavar{defaults_table}.
2844 %
2845 % \luavar{conversion_table} is indexed by possible keys, with values equal to functions to convert the corresponding
2846 % value string to the value that should appear in the returned table.
2847 %
2848 % \luavar{defaults_table} is indexed by keys that \emph{will} appear in the returned table, using the corresponding
2849 % value unless it was given in \luavar{keyvalue_string} and the key appeared in \luavar{conversion_table}.
2850 % \begin{macrocode}
2851 local function parse_data(keyvalue_string,conversion_table,defaults_table)
2852
2853 local key
2854 local value
2855 local result = {}
2856
2857 for s in string.gmatch(keyvalue_string,'([^,]+)') do
2858
2859 key,value = string.match(s,'^(.+)=(.+)$')
2860 local conv = conversion_table[key]
2861 if conv ~= nil then
2862 result[key] = conv(value)
2863 end
2864
2865 end
2866
2867 for key,value in pairs(defaults_table) do
2868 if not(result[key] ~= nil) then
2869 result[key] = value
2870 end
2871 end
2872
2873 return result
2874
2875 end
2876 % \end{macrocode}
2877 % \end{macro}
2878 %
2879 % \begin{macro}[int]{check_data}
2880 % Check \luavar{keyvalue_string} against stored data. If it is new or has changed, append a report to
2881 % \luavar{report_table}. Set the \luavar{KEY_CHECKED} of the data item to true.
2882 %
2883 % The \luavar{keyvalue_string} is processed using \luavar{conversion_table} and \luavar{defaults_table} as per the
2884 % \luavar{parse_data} function. The resulting table is compared to the table in \luavar{data_table} with the same value
2885 % whose key is \luavar{data_table_key}. The tables are compared using the fields indexed by keys in
2886 % \luavar{conversion_table}.
2887 % \begin{macrocode}
2888 local function check_data(keyvalue_string,conversion_table,defaults_table,
2889 data_table,data_table_key_field,report_table)
2890
2891 local new_data = parse_data(keyvalue_string,
2892 conversion_table,defaults_table)
2893
2894 local data_table_key = new_data[data_table_key_field]
2895
2896 local stored_data = data_table[data_table_key]
2897 if stored_data == nil then
2898 table.insert(
2899 report_table,
2900 get_data_page_number(new_data) .. ' New'
2901 )
2902 else
2903 local change_report = ''
2904 for k,_ in pairs(conversion_table) do
2905 if stored_data[k] ~= new_data[k] then
2906 change_report = change_report
2907 .. ' ' .. k .. ':' ..
2908 tostring(stored_data[k]) .. '->' .. tostring(new_data[k])
2909 end
2910 end
2911 if change_report ~= '' then
2912 table.insert(
2913 report_table,
2914 get_data_page_number(new_data) .. ' ' .. change_report
2915 )
2916 end
2917 stored_data[KEY_CHECKED] = true
2918 end
2919
2920 end
2921 % \end{macrocode}
2922 % \end{macro}
2923 %
2924 % \begin{macro}[int]{check_removed_data}
2925 % Check whether data have been removed from \luavar{data_table}, which corresponds to some entry having the value
2926 % of \luavar{KEY_CHECKED} being false. In this case, append a report to \luavar{report_table}.
2927 % \begin{macrocode}
2928 local function check_removed_data(data_table,report_table)
2929 for _,data in pairs(data_table) do
2930 if not data[KEY_CHECKED] then
2931 table.insert(
2932 report_table,
2933 ' Removed'
2934 )
2935 break
2936 end
2937 end
2938 end
2939 % \end{macrocode}
2940 % \end{macro}
2941 %
2942 %
2943 %
2944 % \subsection{Processing of page data from \texorpdfstring{\file{.aux}}{.aux} file}
2945 %
2946 % Conversion and default tables.
2947 % \begin{macrocode}
2948 local PAGE_DATA_CONVERSION_TABLE = {
2949 [KEY_PAGEDATANO] = tonumber,
2950 [KEY_ABSPAGENO] = tonumber,
2951 [KEY_HOFFSETORIGIN] = tonumber,
2952 [KEY_VOFFSETORIGIN] = tonumber,
2953 [KEY_HOFFSET] = tonumber,
2954 [KEY_VOFFSET] = tonumber,
2955 [KEY_PAGEHEIGHT] = tonumber,
2956 [KEY_ODDSIDEMARGIN] = tonumber,
2957 [KEY_EVENSIDEMARGIN] = tonumber,
2958 [KEY_COLUMNCOUNT] = tonumber,
2959 [KEY_COLUMNWIDTH] = tonumber,
2960 [KEY_COLUMNSEP] = tonumber,
2961 [KEY_TEXTWIDTH] = tonumber,
2962 [KEY_TWOSIDE] = toboolean,
2963 }
2964 local PAGE_DATA_DEFAULT_TABLE = {
2965 [KEY_PAGEDATANO] = 0,
2966 [KEY_ABSPAGENO] = 0,
2967 [KEY_HOFFSETORIGIN] = tex.sp('1in'),
2968 [KEY_VOFFSETORIGIN] = tex.sp('1in'),
2969 [KEY_HOFFSET] = tex.dimen['hoffset'],
2970 [KEY_VOFFSET] = tex.dimen['voffset'],
2971 [KEY_PAGEHEIGHT] = tex.dimen['pageheight'],
2972 [KEY_ODDSIDEMARGIN] = tex.dimen['oddsidemargin'],
2973 [KEY_EVENSIDEMARGIN] = tex.dimen['evensidemargin'],
2974 [KEY_TEXTWIDTH] = tex.dimen['textwidth'],
2975 [KEY_COLUMNWIDTH] = tex.dimen['columnwidth'],
2976 [KEY_COLUMNSEP] = tex.dimen['columnsep'],
2977 [KEY_COLUMNCOUNT] = 1,
2978 [KEY_TWOSIDE] = false,
2979 [KEY_CHECKED] = false,
2980 }
2981 % \end{macrocode}
2982 %
2983 % \begin{macro}[int]{store_page_data}
2984 % Store page data supplied by \luavar{keyvalue_string} in \luavar{PAGE_DATA_MAIN_TABLE}.
2985 % \begin{macrocode}
2986 local function store_page_data(keyvalue_string)
2987
2988 local page_data = parse_data(keyvalue_string,
2989 PAGE_DATA_CONVERSION_TABLE,
2990 PAGE_DATA_DEFAULT_TABLE)
2991
2992 PAGE_DATA_MAIN_TABLE[page_data[KEY_PAGEDATANO]] = page_data
2993
2994 end
2995 % \end{macrocode}
2996 % \end{macro}
2997 %
2998 % \begin{macro}[int]{store_default_page_data}
2999 % Store default page data in \luavar{PAGE_DATA_MAIN_TABLE}, so that there is some data to work with when
3000 % computing item positions, even on a first run, when no page data has been written to the \file{.aux} file.
3001 % \begin{macrocode}
3002 local function store_default_page_data()
3003
3004 default_page_data = parse_data('',
3005 PAGE_DATA_CONVERSION_TABLE,
3006 PAGE_DATA_DEFAULT_TABLE)
3007
3008 default_page_data[KEY_ABSPAGENO] = 1
3009 default_page_data[KEY_CHECKED] = true
3010
3011 PAGE_DATA_MAIN_TABLE[0] = default_page_data
3012
3013 end
3014 % \end{macrocode}
3015 % \end{macro}
3016 %
3017 % \begin{macro}[int]{check_page_data}
3018 % Check whether page_data supplied by keyvalue_string differs from that in \luavar{PAGE_DATA_MAIN_TABLE}, appending
3019 % reports to \luavar{PAGE_CHANGE_REPORT_TABLE} if so.
3020 % \begin{macrocode}
3021 local function check_page_data(keyvalue_string)
3022
3023 check_data(keyvalue_string,
3024 PAGE_DATA_CONVERSION_TABLE,PAGE_DATA_DEFAULT_TABLE,
3025 PAGE_DATA_MAIN_TABLE,KEY_PAGEDATANO,
3026 PAGE_CHANGE_REPORT_TABLE)
3027
3028 end
3029 % \end{macrocode}
3030 % \end{macro}
3031 %
3032 %
3033 %
3034 % \subsection{Processing of item data from \texorpdfstring{\file{.aux}}{.aux} file}
3035 %
3036 % Conversion and default tables.
3037 % \begin{macrocode}
3038 local ITEM_DATA_CONVERSIONS = {
3039 [KEY_ITEMNO] = tonumber,
3040 [KEY_ABSPAGENO] = tonumber,
3041 [KEY_PAGENO] = tonumber,
3042 [KEY_XPOS] = tonumber,
3043 [KEY_YPOS] = tonumber,
3044 [KEY_HEIGHT] = tonumber,
3045 [KEY_DEPTH] = tonumber,
3046 [KEY_TYPE] = tonumber,
3047 [KEY_POS] = tonumber,
3048 [KEY_COLUMN] = tonumber,
3049 [KEY_YSHIFT] = tonumber,
3050 [KEY_YSEP_ABOVE] = tonumber,
3051 [KEY_YSEP_BELOW] = tonumber,
3052 [KEY_YSEP_PAGE_TOP] = tonumber,
3053 [KEY_YSEP_PAGE_BOTTOM] = tonumber,
3054 [KEY_CHECKED] = toboolean,
3055 }
3056 local ITEM_DATA_DEFAULTS = {
3057 [KEY_ITEMNO] = 0,
3058 [KEY_ABSPAGENO] = 1,
3059 [KEY_PAGENO] = 1,
3060 [KEY_XPOS] = 0,
3061 [KEY_YPOS] = 0,
3062 [KEY_HEIGHT] = 0,
3063 [KEY_DEPTH] = 0,
3064 [KEY_TYPE] = 0,
3065 [KEY_POS] = 0,
3066 [KEY_COLUMN] = -1,
3067 [KEY_YSHIFT] = 0,
3068 [KEY_YSEP_ABOVE] = tex.dimen['marginparpush'],
3069 [KEY_YSEP_BELOW] = tex.dimen['marginparpush'],
3070 [KEY_YSEP_PAGE_TOP] = tex.dimen['marginparpush'],
3071 [KEY_YSEP_PAGE_BOTTOM] = tex.dimen['marginparpush'],
3072 [KEY_COLNO_COMPUTED] = 0,
3073 [KEY_XSHIFT_COMPUTED] = 0,
3074 [KEY_YSHIFT_COMPUTED] = 0,
3075 [KEY_SIDE_COMPUTED] = 0,
3076 [KEY_MARGINNO_COMPUTED] = 0,
3077 [KEY_ENABLED_COMPUTED] = true,
3078 [KEY_CHECKED] = false,
3079 }
3080 % \end{macrocode}
3081 % \luavar{ITEM_DATA_DEFAULTS} is also used by \luafunc{load_item_data} when no stored item data is found in
3082 % \luavar{ITEM_DATA_MAIN_TABLE}.
3083
3084 % \begin{macro}[int]{store_item_data}
3085 % Store item_data supplied by \luavar{keyvalue_string} in \luavar{ITEM_DATA_MAIN_TABLE}.
3086 % \begin{macrocode}
3087 local function store_item_data(keyvalue_string)
3088
3089 local item = parse_data(keyvalue_string,
3090 ITEM_DATA_CONVERSIONS,
3091 ITEM_DATA_DEFAULTS)
3092
3093 ITEM_DATA_MAIN_TABLE[item[KEY_ITEMNO]] = item
3094
3095 end
3096 % \end{macrocode}
3097 % \end{macro}
3098 %
3099 % \begin{macro}[int]{check_item_data}
3100 % Check whether item_data supplied by \luavar{keyvalue_string} differs from that in \luavar{ITEM_DATA_MAIN_TABLE},
3101 % appending reports to \luavar{ITEM_CHANGE_REPORT_TABLE} if so.
3102 % \begin{macrocode}
3103 local function check_item_data(keyvalue_string)
3104
3105 check_data(keyvalue_string,
3106 ITEM_DATA_CONVERSIONS,ITEM_DATA_DEFAULTS,
3107 ITEM_DATA_MAIN_TABLE,KEY_ITEMNO,
3108 ITEM_CHANGE_REPORT_TABLE)
3109
3110 end
3111 % \end{macrocode}
3112 % \end{macro}
3113 %
3114 %
3115 %
3116 % \subsection{Writing reports}
3117 %
3118 % \begin{macro}[int]{write_report}
3119 % Write the data contained in \luavar{report_table} to \TeX\ in a format suitable for a package warning. The written
3120 % text will contain at most \luavar{max_length} items.
3121 % \begin{macrocode}
3122 local function write_report(report_table,max_length,noun)
3123
3124 if #report_table > 0 then
3125 local report_text
3126 local report_length
3127
3128 if #report_table <= max_length then
3129 report_length = #report_table
3130 report_text = ' Here are the ' .. noun .. ':\n'
3131 else
3132 report_length = max_length
3133 report_text = ' Here are the first ' .. report_length .. ' ' .. noun .. ':\n'
3134 end
3135
3136 for i=1,report_length do
3137 report_text = report_text .. report_table[i]
3138 if i < report_length then
3139 report_text = report_text .. '\n'
3140 end
3141 end
3142
3143 tex.print(report_text)
3144 end
3145
3146 end
3147 % \end{macrocode}
3148 % \end{macro}
3149 %
3150 % \begin{macro}[int]{write_problem_report}
3151 % Write a report about placement problems to \TeX\ in a format suitable for a package warning.
3152 % \begin{macrocode}
3153 local function write_problem_report()
3154
3155 write_report(PROBLEM_REPORT_TABLE,PROBLEM_REPORT_MAX_LENGTH,'problems')
3156
3157 end
3158 % \end{macrocode}
3159 % \end{macro}
3160 %
3161 % \begin{macro}[int]{write_item_change_report}
3162 % Write a report about changes in item data to \TeX\ in a format suitable for a package warning.
3163 % \begin{macrocode}
3164 local function write_item_change_report()
3165
3166 check_removed_data(ITEM_DATA_MAIN_TABLE,ITEM_CHANGE_REPORT_TABLE)
3167 write_report(ITEM_CHANGE_REPORT_TABLE,ITEM_CHANGE_REPORT_MAX_LENGTH,'changes')
3168
3169 end
3170 % \end{macrocode}
3171 % \end{macro}
3172 %
3173 % \begin{macro}[int]{write_page_change_report}
3174 % Write a report about changes in page data to \TeX\ in a format suitable for a package warning.
3175 % \begin{macrocode}
3176 local function write_page_change_report()
3177
3178 check_removed_data(PAGE_DATA_MAIN_TABLE,PAGE_CHANGE_REPORT_TABLE)
3179 write_report(PAGE_CHANGE_REPORT_TABLE,PAGE_CHANGE_REPORT_MAX_LENGTH,'changes')
3180
3181 end
3182 % \end{macrocode}
3183 % \end{macro}
3184 %
3185 %
3186 %
3187 % \subsection{Computing horizontal positions}
3188 %
3189 % It is necessary to determine whether an item should be placed on the right or left of the text block, and in which
3190 % column it lies. The following lookup tables are used.
3191 %
3192 % The value found in \luavar{RIGHTSIDE_LOOKUP_TABLE} is either \luavar{true} (right) or \luavar{false} (left). It is
3193 % indexed by whether the item is on a recto page (\luavar{true}/\luavar{false}), whether it pertains to single-column
3194 % text, the left column, or the right colum (\luavar{0}/\luavar{1}/\luavar{2}), and the value of \key{pos} being
3195 % either \val{auto} or \val{reverse}.
3196 % \begin{macrocode}
3197 local RIGHTSIDE_LOOKUP_TABLE = {
3198 [true] = {
3199 [0] = {
3200 [POS_AUTO] = true,
3201 [POS_REVERSE] = false,
3202 },
3203 [1] = {
3204 [POS_AUTO] = false,
3205 [POS_REVERSE] = true,
3206 },
3207 [2] = {
3208 [POS_AUTO] = true,
3209 [POS_REVERSE] = false,
3210 },
3211 },
3212 [false] = {
3213 [0] = {
3214 [POS_AUTO] = false,
3215 [POS_REVERSE] = true,
3216 },
3217 [1] = {
3218 [POS_AUTO] = true,
3219 [POS_REVERSE] = false,
3220 },
3221 [2] = {
3222 [POS_AUTO] = false,
3223 [POS_REVERSE] = true,
3224 },
3225 },
3226 }
3227 % \end{macrocode}
3228 % The value found in \luavar{MARGINNO_LOOKUP_TABLE} ranges from \luavar{0} to \luavar{5} (see
3229 % \luavar{KEY_MARGINNO_COMPUTED} for the meaning of these values). It is indexed by whether the item is on a recto
3230 % page (\luavar{true}/\luavar{false}), whether it pertains to single-column text, the left column, or the right colum
3231 % (\luavar{0}/\luavar{1}/\luavar{2}), and whether it is to be placed on the right of the text block
3232 % (\luavar{true}/\luavar{false}).
3233 % \begin{macrocode}
3234 local MARGINNO_LOOKUP_TABLE = {
3235 [true] = {
3236 [0] = {
3237 [false] = 1,
3238 [true] = 0,
3239 },
3240 [1] = {
3241 [false] = 1,
3242 [true] = 5,
3243 },
3244 [2] = {
3245 [false] = 4,
3246 [true] = 0,
3247 },
3248 },
3249 [false] = {
3250 [0] = {
3251 [false] = 2,
3252 [true] = 3,
3253 },
3254 [1] = {
3255 [false] = 2,
3256 [true] = 5,
3257 },
3258 [2] = {
3259 [false] = 4,
3260 [true] = 3,
3261 },
3262 },
3263 }
3264 % \end{macrocode}
3265 %
3266 % \begin{macro}[int]{compute_items_horizontal}
3267 % For every \luavar{item_data} in \luavar{item_data_list}, compute the fields relevant to horizontal positioning,
3268 % namely \luavar{KEY_COLNO_COMPUTED}, \luavar{KEY_XSHIFT_COMPUTED}, \luavar{KEY_SIDE_COMPUTED}, based on the layout
3269 % information in page_data. Every item described in \luavar{item_data_list} is assumed to be on the same page.
3270 % \begin{macrocode}
3271 local function compute_items_horizontal(item_data_list,page_data)
3272 % \end{macrocode}
3273 % Immediately return if \luavar{item_data_list} is empty, to avoid edge cases.
3274 % \begin{macrocode}
3275 if #item_data_list == 0 then
3276 return
3277 end
3278 % \end{macrocode}
3279 % Information used frequently and which is the same for every item.
3280 % \begin{macrocode}
3281 local pageno = item_data_list[1][KEY_PAGENO]
3282 local twoside = page_data[KEY_TWOSIDE]
3283 local recto = ((pageno % 2) == 1) or (not twoside)
3284 local columncount = page_data[KEY_COLUMNCOUNT]
3285 % \end{macrocode}
3286 % Tables to contain the \(x\)-coordinates of left edge, right edge, and middle of the current text, whether a single
3287 % column (index 0), the left column (index 1), or the right column (index 2).
3288 % \begin{macrocode}
3289 local x_textleft = {}
3290 local x_textright = {}
3291 local x_textmiddle = {}
3292 % \end{macrocode}
3293 % First, compute necessary dimensions for single-column text, since most of these calculations would be used anyway
3294 % for two-column text. The terms used in calculating \luavar{x_textleft[0]} respectively take one to the origin of
3295 % \cs{hoffset}, to the origin of \cs{oddsidemargin} and \cs{evensidemargin}, and to the left-hand side of the text
3296 % block.
3297 % \begin{macrocode}
3298 if recto then
3299 x_textleft[0] = (
3300 page_data[KEY_HOFFSETORIGIN]
3301 + page_data[KEY_HOFFSET]
3302 + page_data[KEY_ODDSIDEMARGIN]
3303 )
3304 x_textright[0] = (
3305 x_textleft[0]
3306 + page_data[KEY_TEXTWIDTH]
3307 )
3308 else
3309 x_textleft[0] = (
3310 page_data[KEY_HOFFSETORIGIN]
3311 + page_data[KEY_HOFFSET]
3312 + page_data[KEY_EVENSIDEMARGIN]
3313 )
3314 x_textright[0] = (
3315 x_textleft[0]
3316 + page_data[KEY_TEXTWIDTH]
3317 )
3318 end
3319 x_textmiddle[0] = (x_textleft[0] + x_textright[0])/2
3320
3321
3322 if columncount == 1 then
3323 % \end{macrocode}
3324 % If the page is one-column, the field \luavar{KEY_COLNO_COMPUTED} can be set immediately for every item_data.
3325 % \begin{macrocode}
3326 for i=1,#item_data_list do
3327 item_data_list[i][KEY_COLNO_COMPUTED] = 0
3328 end
3329 else
3330 % \end{macrocode}
3331 % If the page is two-column, calculate the \(x\)-coordinates of the left and right edges and the mid-point of each
3332 % column.
3333 % \begin{macrocode}
3334 x_textleft[1] = x_textleft[0]
3335 x_textright[1] = (
3336 x_textleft[1]
3337 + page_data[KEY_COLUMNWIDTH]
3338 )
3339 x_textmiddle[1] = (x_textleft[1] + x_textright[1])/2
3340
3341 x_textleft[2] = (
3342 x_textright[1]
3343 + page_data[KEY_COLUMNSEP]
3344 )
3345 x_textright[2] = (
3346 x_textleft[2]
3347 + page_data[KEY_COLUMNWIDTH]
3348 )
3349 x_textmiddle[2] = (x_textleft[2] + x_textright[2])/2
3350
3351 % \end{macrocode}
3352 % Calculate the cut-off (mid-way between the columns) that distinguishes items from left and right columns.
3353 % \begin{macrocode}
3354 local left_column_x_limit = (
3355 x_textright[1]
3356 + .5*page_data[KEY_COLUMNSEP]
3357 )
3358 % \end{macrocode}
3359 % Now set the field \luavar{KEY_COLNO_COMPUTED} for each item.
3360 % \begin{macrocode}
3361 for i=1,#item_data_list do
3362 local item_data = item_data_list[i]
3363
3364 if item_data[KEY_COLUMN] >= 0 then
3365 item_data[KEY_COLNO_COMPUTED] = item_data[KEY_COLUMN]
3366 else
3367 if item_data[KEY_XPOS] <= left_column_x_limit then
3368 item_data[KEY_COLNO_COMPUTED] = 1
3369 else
3370 item_data[KEY_COLNO_COMPUTED] = 2
3371 end
3372 end
3373 end
3374
3375 end
3376 % \end{macrocode}
3377 % For every item_data in item_data_list, compute and set the fields \luavar{KEY_SIDE_COMPUTED},
3378 % \luavar{KEY_XSHIFT_COMPUTED}, and \luavar{KEY_MARGINNO_COMPUTED}.
3379 % \begin{macrocode}
3380 for i=1,#item_data_list do
3381 local item = item_data_list[i]
3382
3383 local pos = item[KEY_POS]
3384 local colnocomputed = item[KEY_COLNO_COMPUTED]
3385
3386 if pos == POS_LEFT then
3387 rightside = false
3388 elseif pos == POS_RIGHT then
3389 rightside = true
3390 elseif pos == POS_NEAREST then
3391 rightside = (item[KEY_XPOS] >= x_textmiddle[colnocomputed])
3392 else
3393 % \end{macrocode}
3394 % \luavar{pos} must be POS_AUTO or POS_REVERSE
3395 % \begin{macrocode}
3396 rightside = RIGHTSIDE_LOOKUP_TABLE[recto][colnocomputed][pos]
3397 end
3398
3399 local marginno = MARGINNO_LOOKUP_TABLE[recto][colnocomputed][rightside]
3400
3401 if rightside then
3402 item[KEY_SIDE_COMPUTED] = 0
3403 item[KEY_XSHIFT_COMPUTED] = -item[KEY_XPOS]
3404 + x_textright[colnocomputed]
3405 else
3406 item[KEY_SIDE_COMPUTED] = 1
3407 item[KEY_XSHIFT_COMPUTED] = -item[KEY_XPOS]
3408 + x_textleft[colnocomputed]
3409 end
3410 item[KEY_MARGINNO_COMPUTED] = marginno
3411
3412 end
3413
3414 end
3415 % \end{macrocode}
3416 % \end{macro}
3417 %
3418 % \begin{macro}[int]{get_y_item_top}
3419 % Return the \(y\)-coordinate of the top of the item described by \luavar{item_data}.
3420 % \begin{macrocode}
3421 local function get_y_item_top(item_data)
3422 return item_data[KEY_YPOS]
3423 + item_data[KEY_YSHIFT_COMPUTED]
3424 + item_data[KEY_HEIGHT]
3425 end
3426 % \end{macrocode}
3427 % \end{macro}
3428 %
3429 % \begin{macro}[int]{get_y_item_bottom}
3430 % Return the \(y\)-coordinate of the bottom of the item described by \luavar{item_data}.
3431 % \begin{macrocode}
3432 local function get_y_item_bottom(item_data)
3433 return item_data[KEY_YPOS]
3434 - item_data[KEY_DEPTH]
3435 + item_data[KEY_YSHIFT_COMPUTED]
3436 end
3437 % \end{macrocode}
3438 % \end{macro}
3439 %
3440 % \begin{macro}[int]{get_ysep_list}
3441 % Calculate the separation to be used between adjacent marginal content items as described in
3442 % \luavar{item_data_list}. The list is assumed to be sorted so that items are in the order they should appear on the
3443 % page, top to bottom.
3444 %
3445 % The idea is that we have the following arrangement for \(i = 1,\ldots,\luavar{\#item_data_list}\):
3446 % {
3447 % \null~~~~~~\(\vdots\)\\
3448 % \null~~~~\luavar{item_data_list[i]}\\
3449 % \null~~~~~~\luavar{ysep_list[i]}\\
3450 % \null~~~~\luavar{item_data_list[i+1]}\\
3451 % \null~~~~~~\(\vdots\)\\
3452 % }
3453 % Also set \luavar{ysep_list[0]} and \luavar{ysep_list[\#item_data_list]} to 0, to avoid checking when these values
3454 % are accessed (although they are not used).
3455 % \begin{macrocode}
3456 local function get_ysep_list(item_data_list)
3457
3458 local ysep_list = {}
3459
3460 ysep_list[0] = 0
3461 for i=1,#item_data_list-1 do
3462 ysep_list[i] = math.max(
3463 item_data_list[i][KEY_YSEP_BELOW],
3464 item_data_list[i+1][KEY_YSEP_ABOVE]
3465 )
3466 end
3467 ysep_list[#item_data_list] = 0
3468
3469 return ysep_list
3470
3471 end
3472 % \end{macrocode}
3473 % \end{macro}
3474 %
3475 %
3476 %
3477 % \subsection{Computing vertical positions}
3478 %
3479 %
3480 %
3481 % \subsubsection{Computing \val{optfixed} enabled}
3482 %
3483 % \begin{macro}[int]{compute_items_vertical_optfixed_enabled}
3484 % For every \luavar{item_data} in \luavar{item_data_list} describing an item of type \luavar{TYPE_OPTFIXED}, check
3485 % for a clash with an item of type \luavar{TYPE_FIXED}. If so, set \luavar{item_data[KEY_ENABLED_COMPUTED]} to
3486 % \luavar{false}. Every item described in \luavar{item_data_list} is assumed to be on the same page and to have
3487 % \luavar{KEY_YSHIFT} set to the default.
3488 % \begin{macrocode}
3489 local function compute_items_vertical_optfixed_enabled(item_data_list)
3490
3491 local optfixed_item_data_list = {}
3492 local fixed_item_data_list = {}
3493
3494 for _,item_data in pairs(item_data_list) do
3495 if item_data[KEY_TYPE] == TYPE_OPTFIXED then
3496 optfixed_item_data_list[#optfixed_item_data_list+1] = item_data
3497 elseif item_data[KEY_TYPE] == TYPE_FIXED then
3498 fixed_item_data_list[#fixed_item_data_list+1] = item_data
3499 end
3500 end
3501
3502 for _,optfixed_item_data in pairs(optfixed_item_data_list) do
3503 local optfixed_y_item_top = get_y_item_top(optfixed_item_data)
3504 local optfixed_y_item_bottom = get_y_item_bottom(optfixed_item_data)
3505
3506 for _,fixed_item_data in pairs(fixed_item_data_list) do
3507 local fixed_y_item_top = get_y_item_top(fixed_item_data)
3508 local fixed_y_item_bottom = get_y_item_bottom(fixed_item_data)
3509
3510 if (
3511 (
3512 (fixed_y_item_bottom - optfixed_y_item_top)
3513 <
3514 math.max(
3515 fixed_item_data[KEY_YSEP_BELOW],
3516 optfixed_item_data[KEY_YSEP_ABOVE]
3517 )
3518 )
3519 and
3520 (
3521 (optfixed_y_item_bottom - fixed_y_item_top)
3522 <
3523 math.max(
3524 optfixed_item_data[KEY_YSEP_BELOW],
3525 fixed_item_data[KEY_YSEP_ABOVE]
3526 )
3527 )
3528 ) then
3529 optfixed_item_data[KEY_ENABLED_COMPUTED] = false
3530 break
3531 end
3532 end
3533 end
3534
3535 end
3536 % \end{macrocode}
3537 % \end{macro}
3538 %
3539 %
3540 %
3541 % \subsubsection{Computing vertical adjustment}
3542 %
3543 % \begin{macro}[int]{compute_items_vertical_adjustment}
3544 % For every \luavar{item_data} in \luavar{item_data_list}, compute the field relevant to vertical positioning,
3545 % namely \luavar{KEY_YSHIFT_COMPUTED}, based on the layout information in \luavar{page_data}. Every item described
3546 % in \luavar{item_data_list} is assumed to be on the same page and to have \luavar{KEY_YSHIFT} set to the default,
3547 % and the list is assumed to be sorted so that items are in the order they should appear on the page, top to bottom.
3548 % \begin{macrocode}
3549 local function compute_items_vertical_adjustment(item_data_list,page_data)
3550 % \end{macrocode}
3551 % Immediately return if \luavar{item_data_list} is empty, to avoid edge cases
3552 % \begin{macrocode}
3553 if #item_data_list == 0 then
3554 return
3555 end
3556
3557 local ysep_list = get_ysep_list(item_data_list)
3558 % \end{macrocode}
3559 % \textit{First pass of computation (downward).} \luavar{y_limit_above} will always be the highest \(y\)-coordinate
3560 % at which the top of next item below can appear.
3561 % \begin{macrocode}
3562 local y_limit_above = (
3563 page_data[KEY_VOFFSET]
3564 + page_data[KEY_PAGEHEIGHT]
3565 - item_data_list[1][KEY_YSEP_PAGE_TOP]
3566 )
3567
3568 for i=1,#item_data_list do
3569 local item_data = item_data_list[i]
3570
3571 local y_item_top = get_y_item_top(item_data)
3572
3573 if y_item_top > y_limit_above then
3574 if item_data[KEY_TYPE] == TYPE_NORMAL then
3575 item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT_COMPUTED]
3576 + (y_limit_above - y_item_top)
3577 end
3578 end
3579
3580 y_limit_above = get_y_item_bottom(item_data) - ysep_list[i]
3581 end
3582 % \end{macrocode}
3583 % \textit{Second pass of computation (upward)}. \luavar{y_limit_below} will always be the lowest \(y\)-coordinate at
3584 % which the bottom of next item above can appear.
3585 % \begin{macrocode}
3586 local y_limit_below = (
3587 page_data[KEY_VOFFSET]
3588 + item_data_list[#item_data_list][KEY_YSEP_PAGE_BOTTOM]
3589 )
3590
3591 for i=#item_data_list,1,-1 do
3592 local item_data = item_data_list[i]
3593
3594 local y_item_bottom = get_y_item_bottom(item_data)
3595
3596 if y_item_bottom < y_limit_below then
3597 if item_data[KEY_TYPE] == TYPE_NORMAL then
3598 item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT_COMPUTED]
3599 + (y_limit_below - y_item_bottom)
3600 end
3601 end
3602
3603 y_limit_below = get_y_item_top(item_data) + ysep_list[i-1]
3604 end
3605
3606 end
3607 % \end{macrocode}
3608 % \end{macro}
3609 %
3610 %
3611 %
3612 % \subsubsection{Checking vertical adjustment}
3613 %
3614 % Messages to use when checking results of vertical adjustment.
3615 % \begin{macrocode}
3616 local ITEM_PASSED_YSEP_PAGE_TOP_MESSAGES = {
3617 [TYPE_NORMAL] = 'Moveable item > ysep page top',
3618 [TYPE_FIXED] = 'Topmost fixed item > ysep page top',
3619 [TYPE_OPTFIXED] = 'Topmost optfixed item > ysep page top',
3620 }
3621 local ITEM_CLASH_MESSAGES = {
3622 [TYPE_NORMAL] = {
3623 [TYPE_NORMAL] = 'moveable items'
3624 .. ' (this shouldn\'t happen)',
3625 [TYPE_FIXED] = 'moveable item above fixed item',
3626 [TYPE_OPTFIXED] = 'moveable item above optfixed item',
3627 },
3628 [TYPE_FIXED] = {
3629 [TYPE_NORMAL] = 'moveable item below fixed item',
3630 [TYPE_FIXED] = 'fixed items',
3631 [TYPE_OPTFIXED] = 'fixed item above optfixed item '
3632 .. '(this shouldn\'t happen)',
3633 },
3634 [TYPE_OPTFIXED] = {
3635 [TYPE_NORMAL] = 'moveable items below optfixed item',
3636 [TYPE_FIXED] = 'fixed item below optfixed item '
3637 .. '(this shouldn\'t happen)',
3638 [TYPE_OPTFIXED] = 'optfixed items '
3639 .. '(this shouldn\'t happen)',
3640 },
3641 }
3642 local ITEM_PASSED_YSEP_PAGE_BOTTOM_MESSAGE = {
3643 [TYPE_NORMAL] = 'Moveable item < ysep page bottom',
3644 [TYPE_FIXED] = 'Bottommost fixed item < ysep page bottom',
3645 [TYPE_OPTFIXED] = 'Bottommost optfixed item < ysep page bottom',
3646 }
3647 % \end{macrocode}
3648 %
3649 % \begin{macro}[int]{check_items_vertical}
3650 % For the items described by the item_data in \luavar{item_data_list}, check whether any clash or fail to obey
3651 % \key{ysep page top} or \key{ysep page bottom}. If so, write messages to \luavar{PROBLEM_REPORT_TABLE}.
3652 % \begin{macrocode}
3653 local function check_items_vertical(item_data_list,page_data)
3654 % \end{macrocode}
3655 % Immediately return if item_data_list is empty, to avoid edge cases
3656 % \begin{macrocode}
3657 if (#item_data_list) == 0 then
3658 return
3659 end
3660
3661 local ysep_list = get_ysep_list(item_data_list)
3662
3663 local item_data
3664
3665 % \end{macrocode}
3666 % If any item fails to obey \key{ysep page top}, the first one in the list does.
3667 % \begin{macrocode}
3668 item_data = item_data_list[1]
3669 if (
3670 get_y_item_top(item_data) > page_data[KEY_VOFFSET]
3671 + page_data[KEY_PAGEHEIGHT]
3672 - item_data[KEY_YSEP_PAGE_TOP]
3673 ) then
3674 table.insert(
3675 PROBLEM_REPORT_TABLE,
3676 get_data_page_number(item_data)
3677 .. ' ' .. ITEM_PASSED_YSEP_PAGE_TOP_MESSAGES[item_data[KEY_TYPE]]
3678 )
3679 end
3680
3681 for i=2,#item_data_list do
3682 local item_data = item_data_list[i]
3683 local prev_item_data = item_data_list[i-1]
3684 if (
3685 get_y_item_top(item_data) > get_y_item_bottom(prev_item_data)
3686 - ysep_list[i-1]
3687 ) then
3688 table.insert(
3689 PROBLEM_REPORT_TABLE,
3690 get_data_page_number(item_data)
3691 .. ' Clash: ' ..
3692 ITEM_CLASH_MESSAGES[prev_item_data[KEY_TYPE]][item_data[KEY_TYPE]]
3693 )
3694 end
3695 end
3696 % \end{macrocode}
3697 % If any item fails to obey \key{ysep page bottom}, the last one in the list does.
3698 % \begin{macrocode}
3699 item_data = item_data_list[#item_data_list]
3700 if (
3701 get_y_item_bottom(item_data) < page_data[KEY_VOFFSET]
3702 + item_data[KEY_YSEP_PAGE_BOTTOM]
3703 ) then
3704 table.insert(
3705 PROBLEM_REPORT_TABLE,
3706 get_data_page_number(item_data)
3707 .. ' ' .. ITEM_PASSED_YSEP_PAGE_BOTTOM_MESSAGE[item_data[KEY_TYPE]]
3708 )
3709 end
3710
3711 end
3712 % \end{macrocode}
3713 % \end{macro}
3714 %
3715 %
3716 %
3717 % \subsubsection{Core vertical position computation}
3718 %
3719 % \begin{macro}[int]{compute_items_vertical}
3720 % For every \luavar{item_data} in \luavar{item_data_list}, compute the field relevant to vertical positioning,
3721 % namely \luavar{KEY_YSHIFT_COMPUTED}, based on the layout information in \luavar{page_data}. This may involve
3722 % setting the field \luavar{KEY_ENABLED_COMPUTED} to false. In such a case, the relevant item_data is removed from
3723 % \luavar{item_data_list}.
3724 % \begin{macrocode}
3725 local function compute_items_vertical(item_data_list,page_data)
3726 % \end{macrocode}
3727 % Set \luavar{KEY_YSHIFT_COMPUTED} of each \luavar{item_data} to the user-supplied value.
3728 % \begin{macrocode}
3729 for i=1,#item_data_list do
3730 local item_data = item_data_list[i]
3731
3732 item_data[KEY_YSHIFT_COMPUTED] = item_data[KEY_YSHIFT]
3733 end
3734 % \end{macrocode}
3735 % Decide which items of type \luavar{ITEM_DATA_OPTFIXED} are to be disabled.
3736 % \begin{macrocode}
3737 compute_items_vertical_optfixed_enabled(item_data_list)
3738 % \end{macrocode}
3739 % Strip any \luavar{item_data} with \luavar{KEY_ENABLED_COMPUTED} set to false from \luavar{item_data_list}.
3740 % \begin{macrocode}
3741 list_filter(item_data_list,function(item_data)
3742 return item_data[KEY_ENABLED_COMPUTED]
3743 end)
3744 % \end{macrocode}
3745 % Sort \luavar{item_data_list} according to the stored position from top to bottom and left to right on the page,
3746 % resolving ties using \luavar{KEY_ITEMNO}.
3747 % \begin{macrocode}
3748 table.sort(
3749 item_data_list,
3750 function(left,right)
3751 local y_diff = left[KEY_YPOS] - right[KEY_YPOS]
3752
3753 if y_diff > 0 then
3754 return true
3755 elseif y_diff < 0 then
3756 return false
3757 end
3758
3759 local x_diff = left[KEY_XPOS] - right[KEY_XPOS]
3760
3761 if x_diff < 0 then
3762 return true
3763 elseif x_diff > 0 then
3764 return false
3765 end
3766
3767 return (left[KEY_ITEMNO] < right[KEY_ITEMNO])
3768 end
3769 )
3770
3771 compute_items_vertical_adjustment(item_data_list,page_data)
3772
3773 check_items_vertical(item_data_list,page_data)
3774
3775 end
3776 % \end{macrocode}
3777 % \end{macro}
3778 %
3779 % \begin{macro}[int]{compute_items}
3780 % For every item represented in \luavar{ITEM_DATA_MAIN_TABLE}, use the \luavar{page_data} stored in
3781 % \luavar{PAGE_DATA_MAIN_TABLE} to compute the item_data values necessary to place the item correctly on the page,
3782 % namely those indexed by: \luavar{KEY_COLNO_COMPUTED}, \luavar{KEY_XSHIFT_COMPUTED}, \luavar{KEY_YSHIFT_COMPUTED},
3783 % \luavar{KEY_SIDE_COMPUTED}, \luavar{KEY_ENABLED_COMPUTED}.
3784 % \begin{macrocode}
3785 local function compute_items()
3786 % \end{macrocode}
3787 % Compute the maximum abspageno, which will be the last page of the document on which a item appears.
3788 % \begin{macrocode}
3789 local max_abspageno = 0
3790
3791 for k,v in pairs(ITEM_DATA_MAIN_TABLE) do
3792 max_abspageno = math.max(v[KEY_ABSPAGENO],max_abspageno)
3793 end
3794 % \end{macrocode}
3795 % \luavar{per_abspage_item_data_list} will be a list indexed by absolute page numbers. Each entry will be a
3796 % list (possibly empty) of \luavar{item_data} describing the items that appear on the corresponding page.
3797 % \begin{macrocode}
3798 local per_abspage_item_data_list = {}
3799 % \end{macrocode}
3800 % Prepare \luavar{per_abspage_item_data_list} by making each entry an empty list, then fill it from
3801 % \luavar{ITEM_DATA_MAIN_TABLE}.
3802 % \begin{macrocode}
3803 for i=1,max_abspageno do
3804 per_abspage_item_data_list[i] = {}
3805 end
3806 for _,item_data in pairs(ITEM_DATA_MAIN_TABLE) do
3807 local temp_table = per_abspage_item_data_list[item_data[KEY_ABSPAGENO]]
3808 temp_table[#temp_table+1] = item_data
3809 end
3810 % \end{macrocode}
3811 % \luavar{per_abspage_item_data_list} will be a list indexed by abssolute page numbers. Each entry will be a
3812 % \luavar{page_data} describing the corresponding page. Usually multiple entries will be the same
3813 % \luavar{page_data}: in the loop, \luavar{pagedatano} will be the index of the last entry in
3814 % \luavar{PAGE_DATA_MAIN_TABLE} with \luavar{KEY_ABSPAGENO} value less than or equal to \luavar{abspageno}. (There
3815 % may be several such entries in \luavar{PAGE_DATA_MAIN_TABLE} because \cs{marginalianewgeometry} may have been
3816 % called multiple times on the same page.) Note that \luavar{PAGE_DATA_MAIN_TABLE[0]} is available even if there was
3817 % no data in the \file{.aux} file, because the defaults were stored by \luafunc{store_default_page_data}.
3818 % \begin{macrocode}
3819 local per_abspage_page_data_list = {}
3820 % \end{macrocode}
3821 % \begin{macrocode}
3822 local pagedatano = 0
3823 for abspageno = 1,max_abspageno do
3824 % \end{macrocode}
3825 % \begin{macrocode}
3826 while (
3827 PAGE_DATA_MAIN_TABLE[pagedatano+1] ~= nil
3828 and
3829 PAGE_DATA_MAIN_TABLE[pagedatano+1][KEY_ABSPAGENO] == abspageno
3830 ) do
3831 pagedatano = pagedatano+1
3832 end
3833 per_abspage_page_data_list[abspageno] = PAGE_DATA_MAIN_TABLE[pagedatano]
3834 end
3835 % \end{macrocode}
3836 % Iterate through all pages and perform the necessary computations.
3837 % \begin{macrocode}
3838 for abspageno=1,#per_abspage_item_data_list do
3839 local current_page_data = per_abspage_page_data_list[abspageno]
3840 local current_page_item_data_list = per_abspage_item_data_list[abspageno]
3841 % \end{macrocode}
3842 % First, compute the horizontal positions, which includes sorting items into columns in two-column mode.
3843 % \begin{macrocode}
3844 compute_items_horizontal(current_page_item_data_list,current_page_data)
3845 % \end{macrocode}
3846 % Sort the items into sublists corresponding to the margins in which they are located.
3847 % \begin{macrocode}
3848 local current_page_item_data_sublists = {}
3849
3850 for i=0,5 do
3851 current_page_item_data_sublists[i] = {}
3852 end
3853
3854 for _,item_data in pairs(current_page_item_data_list) do
3855 table.insert(
3856 current_page_item_data_sublists[item_data[KEY_MARGINNO_COMPUTED]],
3857 item_data
3858 )
3859 end
3860 % \end{macrocode}
3861 % Compute vertical positons for each sublist.
3862 % \begin{macrocode}
3863 for i=0,5 do
3864 compute_items_vertical(
3865 current_page_item_data_sublists[i],
3866 current_page_data
3867 )
3868 end
3869 end
3870 end
3871 % \end{macrocode}
3872 % \end{macro}
3873 %
3874 %
3875 %
3876 % \subsection{Passing item_data back to \LaTeX}
3877 %
3878 % \begin{macro}[int]{load_item_data}
3879 % Set the relevant \LaTeX\ counter and dimension variables to the values computed for \luavar{itemno}.
3880 % \begin{macrocode}
3881 local function load_item_data(itemno)
3882
3883 item = ITEM_DATA_MAIN_TABLE[tonumber(itemno)]
3884 if item == nil then
3885 item = ITEM_DATA_DEFAULTS
3886 end
3887
3888 tex.count['l__marginalia_page_int'] = item[KEY_PAGENO]
3889 tex.count['l__marginalia_column_computed_int'] = item[KEY_COLNO_COMPUTED]
3890 tex.dimen['l__marginalia_xshift_computed_dim'] = item[KEY_XSHIFT_COMPUTED]
3891 tex.dimen['l__marginalia_yshift_computed_dim'] = item[KEY_YSHIFT_COMPUTED]
3892 tex.count['l__marginalia_side_computed_int'] = item[KEY_SIDE_COMPUTED]
3893 tex.count['l__marginalia_marginno_computed_int']
3894 = item[KEY_MARGINNO_COMPUTED]
3895 if item[KEY_ENABLED_COMPUTED] then
3896 tex.count['l__marginalia_enabled_computed_int'] = 1
3897 else
3898 tex.count['l__marginalia_enabled_computed_int'] = 0
3899 end
3900
3901 end
3902 % \end{macrocode}
3903 % \end{macro}
3904 %
3905 %
3906 %
3907 % \subsection{Export public functions}
3908 %
3909 % Finally, make available the functions that will be called from \LaTeX\ using \cs{lua_now:n} and \cs{lua_now:e}.
3910 % \begin{macrocode}
3911 return {
3912 store_default_page_data = store_default_page_data,
3913 store_page_data = store_page_data,
3914 check_page_data = check_page_data,
3915
3916 store_item_data = store_item_data,
3917 check_item_data = check_item_data,
3918
3919 compute_items = compute_items,
3920
3921 load_item_data = load_item_data,
3922
3923 write_problem_report = write_problem_report,
3924
3925 write_page_change_report = write_page_change_report,
3926 write_item_change_report = write_item_change_report,
3927 }
3928 % \end{macrocode}
3929 %
3930 %
3931 %
3932 % \begin{macrocode}
3933 %</lua>
3934 % \end{macrocode}
3935
3936 %
3937 %
3938 %
3939 % \clearpage
3940 % \end{implementation}
3941 %
3942 %
3943 %
3944 % \iffalse
3945 %<*metadriver>
3946 \input{marginalia.dtx}
3947 %</metadriver>
3948 % \fi