I'm trying to write a command that I can use to set up right-aligned left margin notes easily, and not be worried about the flow of content. Basically I would like the margin note to be on the same line as the next block of text in the content. Here's what I have so far:
\documentclass{article}
\usepackage[utf8]{inputenc}
\DeclareRobustCommand{\mymarginpar}[1]{%
\mbox{}\marginpar[\raggedleft#1]{\raggedright#1}}
\begin{document}
\reversemarginpar
\mymarginpar{\textit{Consider}}
\begin{verbatim}
a = 1/5
while True: {
print(a)
a = (1+a) / 2
}
\end{verbatim}
\mymarginpar{\textit{Prints}}
\begin{verbatim}
1/5
3/5
4/5
9/10
\end{verbatim}
\end{document}
Which produces:
Whereas I would like:
(Assuming I aligned perfectly with my eye)
Thanks!
Best Answer
As egreg points out at control vertical space before and after verbatim environment?, the
verbatim
environment adds space at the top and bottom of the list, and he shows how to turn that off. In addition, because a new line is started, I also had to\vspace{-\baselineskip}
to undo the new line. Finally, I added extra space between your two examples with a\bigskip
.If your verbatim lists don't have to break across page boundaries, here is another approach that stuffs the verbatim material into a
verbbox
, and then the box can be used just like any other LaTeX box. Because the box has the baseline at the bottom, one must shift the box downward by all of its height except for the height of a\strutbox
, in order to make the top line of the verbatim align with the marginpar. I do the downward shift with the\belowbaseline
macro of thestackengine
package.I don't show the output because it is, essentially, identical to the above.