# Position margin notes relative to left margin in plain TeX

I want to add margin notes in the Tufte style to my Plain TeX document. (I'm using eplain, if that matters.) I know that if I were using LaTeX I could just use tufte-latex, but as a thorough contrarian I enjoy coding these things myself.

I'm almost there but my current macro will place text relative to the current location when I invoke it, instead of the margin. In my MWE, the note is placed smack dab in the middle of things. But if you move the invocation of \sidenote around, the side note may appear closer to the left margin. How can I reposition my sidenote text relative to the left (or right) margin, instead of the current location? (I used the left margin in my MWE.)

The thing I really do like about my macro is that the baselines of the first line of the side note and the main body text to which it refers align. I want to keep that. And any other suggestions about my macro are welcome.

MWE:

\input eplain
\leftmargin=2in

\font\sidenotefont=      pxi at8.5pt

\long\def\sidenote#1{\llap{\smash{\vtop{%
\parindent=0pt\hsize=1.5in\rightskip=.87in\parfillskip=0pt\leftskip=-1in plus1fil%
\baselineskip=10pt\sidenotefont #1}}}}

When we are working at a difficult task and strive after a good thing, we are
fighting a righteous battle, the direct reward of which is that we are kept from
\sidenote{This note has been brought to you by the letter pi and
the number e. Please make a note of it. The reason for its existence is the
great lengths I went to do things.}much evil. As we advance in life it becomes more and more difficult, but in
fighting the difficulties the inmost strength of the heart is developed. Indeed
life is a have to defend and protect ourselves, and with a cheerful and brace
spirit we must battle; we plan and calculate in order to make progress.

\bye

-
Would an \insert-based solution work for you? i.e., output routine. –  morbusg Dec 8 '12 at 10:04
@morbusg, I was looking at \insert yesterday. I was concerned that I wouldn't be able to easily get the alignment right; sidenotes must be beside the text the're commenting on. Do you know if this can be done? –  EfForEffort Dec 8 '12 at 12:58
Ah, yes, it gets very difficult then; I know because I have tried (and failed). I think \pagetotal could somehow be used for those calculations, but the problems (again: at least for me) arise when there are more than one sidenote on a page. –  morbusg Dec 8 '12 at 20:36

If lines don't have unusual depth, this could be a starting point:

\input eplain
\leftmargin=2in

\font\mainfont=pxr at 10pt
\font\sidenotefont=pxi at 8.5pt

\long\def\sidenote#1{%
\parindent=0pt
\hsize=1.7in
\parfillskip=0pt
\leftskip=0pt plus1fil
\baselineskip=10pt\sidenotefont\vglue-\ht\strutbox #1}}\kern1em}}}

\mainfont

When we are working at a difficult task and strive after a good thing, we
are fighting a righteous battle, the direct reward of which is that we are
kept from \sidenote{This note has been brought to you by the letter pi and
the number e. Please make a note of it. The reason for its existence is the
great lengths I went to do things.}much evil. As we advance in life it becomes
more and more difficult, but in fighting the difficulties the inmost strength
of the heart is developed. Indeed life is a have to defend and protect ourselves,
and with a cheerful and brace spirit we must battle; we plan and calculate in
order to make progress.

\bye


The key is \vadjust that adds the material to the enclosing vertical list.

-
This is great, thanks! Now to learn what strutbox is and how I can adjust it for different font sizes.... –  EfForEffort Dec 8 '12 at 14:37
@D.Bueno: You might want to look at exercise 14.28 of the TeXbook (and it's answer in the Appendix A). The solution is very similar to egreg's proposal and Knuth's explanations are always nice, so it's a good read. –  Mafra Dec 8 '12 at 23:08
@D.Bueno: For that, you could use LaTeX's definition of the strutbox; it uses .7\baselineskip for height, and .3\baselineskip for depth, instead of “hard-coded” values as in plain. Also you might want to set \parskip=0pt to avoid possible “drifting” of baselines between the main column content and the sidenote column (plain's default is 0pt plus 1pt). –  morbusg Dec 11 '12 at 6:26
@morbusg: Thanks! These are good suggestions. –  EfForEffort Dec 11 '12 at 19:59

After reading the appropriate exercise in the TeXBook (14.28), here is my \strut-based solution. Thanks to @Mafra for suggesting the exercise.

\def\strutdepth{\dp\strutbox}
\def\simplesidenote#1{%
\def\lsidenote#1{\vtop to \strutdepth{%
\baselineskip\strutdepth%
\vss\llap{\vtop to 0pt{%
\leftskip=0pt plus1fill \rightskip=8pt%
\parfillskip=0pt \parindent=0pt%
\hsize=1.7in%
\eightpoint\it #1}}\null}}


Note that \eightpoint is just a macro to adjust the font size.

-
Heh, I think I suggested exercise 14.28 :-) –  Mafra Dec 12 '12 at 23:22
lol. Indeed, you did. Sorry about that! I've corrected it in the answer. –  EfForEffort Dec 12 '12 at 23:30