% leannote.sty -- margin notes that name the Lean declaration behind a statement. \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{leannote}[2026/11/20 Lean margin notes] \RequirePackage{xcolor,url,needspace,zref-savepos,zref-abspage,hyperref} % % INTERFACE % % \leannote{}{}{}{} % A note in the right margin: a lead phrase for , then , then the file name % and line, linked to #L at the pinned revision \leanrevurl. % Used inside a paragraph it attaches to the line where it occurs. Directly above % \begin{theorem} (lemma, ...) it attaches to the heading line of the statement; first % thing inside a theorem-like environment, likewise. Above a \paragraph heading or an % ordinary paragraph it attaches to that paragraph's first line. % is one key; it prints a short lead phrase before : % same Same route. (the Lean proof follows the printed proof) % formal Formalized. (same statement; routes not compared) % route Different route. (same statement, Lean proves it another way) % narrower Narrower in Lean. (Lean proves less than the printed statement) % different Different form in Lean. (Lean proves a statement of another form, whose % equivalence with the printed one is argued in the text) % none (no lead phrase) % : terse prose; write each Lean name as \leandecl{Name}. Names break only after "." % or "_". \leanref{}{}{} is a further Lean name with its own link. % \ifleanrail set \leanrailfalse to suppress all notes. % % Layout: the note is a zero-size box placed on its line at the right edge of the text block % plus \marginparsep, using positions saved by zref-savepos (several runs). Its first baseline % sits on the baseline of that line, unless the previous note on the same page reaches that low: % then it starts one line plus \leannotegap below it. A note that would run below the text block is moved up, % pushing earlier notes on the page up if needed; one that still does not fit is reported in the % log ("leannote: note N runs ... below the text block"). % Face: text serif, upright, gray, \scriptsize, ragged right; lead phrase italic; Lean names in % Inconsolata (zi4), darker gray. \makeatletter \newif\ifleanrail\leanrailtrue \newdimen\leannotefootroom \leannotefootroom=24pt \newcommand*\leanrevurl{https://github.com/macgregor-sudo/tight-39-code/blob/46f01c135aacea9003435e7f1dc99b4a69568e83/} \newdimen\leannotegap \leannotegap=3pt % extra space between stacked notes \newcommand*\leannotefont{\fontfamily{zi4}\fontseries{m}\fontshape{n}\selectfont} \newcommand*\leannotecolor{\color[gray]{0.33}} \newcommand*\leannotestyle{% \everypar{}\normalfont\scriptsize\upshape \setlength\baselineskip{9.2pt}% \parindent\z@\leftskip\z@\rightskip\z@\@plus 2.5em\relax \parfillskip\z@\@plus 1fil\relax\spaceskip\fontdimen2\font\xspaceskip\z@ \hyphenpenalty 4000\exhyphenpenalty 4000\pretolerance\m@ne\tolerance 9000\relax \emergencystretch 1em\hbadness 10000\relax} \DeclareUrlCommand\leandecl{% \def\UrlFont{\leannotefont\color[gray]{0.18}}% \def\UrlBreaks{\do\.\do\_}\def\UrlBigBreaks{}\def\UrlNoBreaks{}% \def\UrlSpecials{}\Urlmuskip=0mu plus 1mu\relax} \newcommand\leanref[3]{\href{\leanrevurl#1\#L#2}{\leandecl{#3}}} % kind keys -> lead phrases \@namedef{PFln@lead@same}{Same route.} \@namedef{PFln@lead@formal}{Formalized.} \@namedef{PFln@lead@route}{Different route.} \@namedef{PFln@lead@narrower}{Narrower in Lean.} \@namedef{PFln@lead@different}{Different form in Lean.} \@namedef{PFln@lead@none}{} \newcommand*\PFln@lead[1]{% \ifcsname PFln@lead@#1\endcsname \edef\PFln@tmp{\csname PFln@lead@#1\endcsname}% \ifx\PFln@tmp\@empty\else{\itshape\PFln@tmp}\space\fi \else\PackageWarning{leannote}{Unknown note kind `#1'}\fi} % Printed file name: without the directory "Proofs/", breakable after "." and before capitals. \def\PFln@strip#1/#2\@nil{#2} \newcommand\PFln@file[1]{{\leannotefont\color[gray]{0.18}% \expandafter\PFln@scan\romannumeral-`0\PFln@strip#1\@nil\@nil}} \def\PFln@scan#1{% \ifx#1\@nil\else \ifnum\lccode`#1>\z@\ifnum\lccode`#1=`#1\else\discretionary{}{}{}\fi\fi #1% \ifx#1.\discretionary{}{}{}\fi \expandafter\PFln@scan \fi} % \Needspace (needspace.sty) with a log line when it breaks the page. \newcommand\PFln@needspace[1]{% \ifhmode\par\fi \@tempskipb\lastskip\vskip-\@tempskipb\penalty-100 \begingroup\setlength\dimen@{#1}\advance\dimen@\@tempskipb \dimen@ii\pagegoal\advance\dimen@ii-\pagetotal \ifdim\dimen@>\dimen@ii \PackageInfo{leannote}{page break before note: needs \the\dimen@, has \the\dimen@ii}% \ifdim\dimen@ii>\z@\vfil\fi\break \else\vskip\@tempskipb\fi \endgroup} % ---- mechanism ---------------------------------------------------------------------- \newsavebox\PFln@box \newcount\PFln@cnt \newif\ifPFln@armed \newcommand\PFln@build[1]{% \global\setbox\PFln@box\vtop{\let\par\@@par\@newlistfalse\@inlabelfalse \hsize\marginparwidth\leannotestyle\leavevmode{\leannotecolor#1}\par}} \newcommand\leannote[4]{\ifleanrail \PFln@build{\PFln@lead{#1}#2 \href{\leanrevurl#3\#L#4}{\PFln@file{#3}, line~#4}}% \expandafter\PFln@dispatch\fi} \newcommand\PFln@dispatch{% \if@newlist % first thing in a theorem-like environment: the heading line is the line of the note. \leavevmode\PFln@place \else\ifvmode \if@nobreak\PackageInfo{leannote}{note after a heading: no room requested}% \else\PFln@needspace{\dimexpr\dp\PFln@box+2\baselineskip\relax}\fi \global\PFln@armedtrue \expandafter\PFln@look \else \PFln@place \fi\fi} \newcommand\PFln@look{\@ifnextchar\begin{}{\@ifnextchar\paragraph\PFln@para\PFln@leave}} \newcommand\PFln@para[2]{#1{#2}\leavevmode\PFln@fire\ignorespaces} \newcommand\PFln@leave{\leavevmode\PFln@fire} \newcommand\PFln@fire{\ifPFln@armed\global\PFln@armedfalse\PFln@place\fi} % amsthm builds every theorem-like head through \deferred@thm@head; put the note first in it. \let\PFln@orig@dth\deferred@thm@head \def\deferred@thm@head#1{\PFln@orig@dth{\PFln@fire#1}} % Stacking state of the previous note: its page, the y of its bottom (sp, from page foot) and % the upward push it received from the aux file. % A note is moved down below the previous note on its page, and moved up when it would run % below the text block; if it cannot move up far enough, it asks the previous note on the page % to move up on the next run (\PFlnpush in the aux file). \newcount\PFln@prevpage \PFln@prevpage=-1 \newdimen\PFln@prevbottom \newdimen\PFln@prevfree \newdimen\PFln@y \newdimen\PFln@T \newdimen\PFln@lim \newdimen\PFln@foot \newdimen\PFln@top \newdimen\PFln@push \newdimen\PFln@bot \newdimen\PFln@free \newif\ifPFln@same \def\PFlnpush#1#2{\expandafter\gdef\csname PFln@push@#1\endcsname{#2}} % \PFln@fit{}: first baseline \PFln@T and bottom \PFln@bot of the current note (box 0) % for a given upward push, within \PFln@lim, lifted if it would run below \PFln@foot. \newcommand\PFln@fit[1]{% \PFln@T\PFln@y\advance\PFln@T#1\relax \ifdim\PFln@T>\PFln@lim\PFln@T\PFln@lim\fi \PFln@bot\PFln@T\advance\PFln@bot-\dp\z@ \ifdim\PFln@bot<\PFln@foot \advance\PFln@T\PFln@foot\advance\PFln@T-\PFln@bot \ifdim\PFln@T>\PFln@lim\PFln@T\PFln@lim\fi \PFln@bot\PFln@T\advance\PFln@bot-\dp\z@ \fi} \newcommand\PFln@place{% \global\advance\PFln@cnt\@ne \zsavepos{PFln\the\PFln@cnt}% \setbox\z@\hbox{\copy\PFln@box}% \PFln@y=\zposy{PFln\the\PFln@cnt}sp\relax \count@=\zref@extractdefault{PFln\the\PFln@cnt}{abspage}{-2}\relax \ifnum\count@=\PFln@prevpage\PFln@sametrue\else\PFln@samefalse\fi \expandafter\ifx\csname PFln@push@\the\PFln@cnt\endcsname\relax\PFln@push\z@ \else\PFln@push=\csname PFln@push@\the\PFln@cnt\endcsname\relax\fi % text block, measured from the bottom of the page \PFln@top\paperheight\advance\PFln@top-1in\advance\PFln@top-\voffset \advance\PFln@top-\topmargin\advance\PFln@top-\headheight\advance\PFln@top-\headsep \PFln@foot\PFln@top\advance\PFln@foot-\textheight \advance\PFln@foot-\leannotefootroom % a note may run this far into the bottom margin \advance\PFln@top-7pt % highest admissible first baseline \PFln@lim\PFln@top \ifPFln@same\dimen@\PFln@prevbottom\advance\dimen@-\leannotegap\advance\dimen@-9.2pt \ifdim\dimen@<\PFln@lim\PFln@lim\dimen@\fi\fi % bottom without any push (what the next note may ask to raise), then the real position \PFln@fit\z@ \PFln@free\PFln@bot \PFln@fit\PFln@push % lowest admissible first baseline: bottom on the foot, or as high as the next note asked \dimen@\PFln@foot\advance\dimen@\dp\z@ \ifdim\PFln@push>\z@\dimen@ii\PFln@y\advance\dimen@ii\PFln@push \ifdim\dimen@ii>\dimen@\dimen@\dimen@ii\fi\fi \ifPFln@same \advance\dimen@\leannotegap\advance\dimen@9.2pt\advance\dimen@-\PFln@prevfree \ifdim\dimen@<\z@\dimen@\z@\fi \ifdim\PFln@y=\z@\dimen@\z@\fi % position not yet known \ifdim\dimen@>\z@ \immediate\write\@mainaux{\string\PFlnpush{\the\numexpr\PFln@cnt-1\relax}{\the\dimen@}}\fi % compare with the push read from the aux file for the previous note \expandafter\ifx\csname PFln@push@\the\numexpr\PFln@cnt-1\relax\endcsname\relax\dimen@ii\z@ \else\dimen@ii=\csname PFln@push@\the\numexpr\PFln@cnt-1\relax\endcsname\relax\fi \advance\dimen@ii-\dimen@ \ifdim\dimen@ii<\z@\dimen@ii-\dimen@ii\fi \ifdim\dimen@ii>.5pt\global\PFln@reruntrue\fi \fi \ifnum\count@>\z@\ifdim\PFln@bot<\dimexpr\PFln@foot-1pt\relax \PackageWarning{leannote}{note \the\PFln@cnt\space runs \the\dimexpr\PFln@foot-\PFln@bot\relax\space below the text block}\fi\fi \global\PFln@prevpage\count@ \global\PFln@prevbottom\PFln@bot \global\PFln@prevfree\PFln@free \dimen@\PFln@y\advance\dimen@-\PFln@T % downward shift \ifdim\dimen@=\z@\else\PackageInfo{leannote}{note \the\PFln@cnt\space moved down \the\dimen@}\fi \setbox\z@\hbox{\lower\dimen@\box\z@}\ht\z@\z@\dp\z@\z@ \rlap{\kern\dimexpr1in+\hoffset+\oddsidemargin+\textwidth+\marginparsep -\zposx{PFln\the\PFln@cnt}sp\relax\box\z@}} \newif\ifPFln@rerun \AtEndDocument{\ifPFln@rerun\PackageWarningNoLine{leannote}{Margin notes may have moved. Rerun to get them right}\fi} \AtBeginDocument{\zref@addprop{savepos}{abspage}} \makeatother \endinput