5

I am having an issue inserting marginal notes while using the ltxdoc document class. I see that the default layout is the following:

ltxdoc layout

From what I understand, the margin notes are set up this way because the \DescribeMacro command places the macro name as a marginal note. I would like to utilize the right margins for inserting my own marginal notes. I tried using

\reversemarginpar\marginpar{Some text to be placed in the right margin}

but this did not do what I wanted. I have also tried using the marginnote package to no avail...

\marginnote{Still not on the right}

How can I place text in the right margin? Here is an MWE:

\documentclass{ltxdoc}

\usepackage{lipsum}
\usepackage{marginnote}
\newcommand{\rmarginpar}[1]{\reversemarginpar\marginpar{#1}}

\begin{document}
\lipsum[1-2]
\marginpar{This marginal note is on the left. This behavior is expected based on
    the page layout.}
\rmarginpar{This marginal note is also on the left, but I want it to appear on the
    right of the text.
    Even worse, this marginal note overlaps the first note.}
\marginnote[This is on the left, as expected]{}[1in]
\marginnote{This should be on the right of the text.}[2in]

\end{document}

Update 2016/03/08

I have also tried using the marginfix and mparhack packages. These did not fix my issues, either. The only improvement is that loading the marginfix package made the \marginpar's no longer overlap, but that is not what I am trying to fix.

1
  • The class is already using \reversemarginpar, I think.... But since you want double margin pars, that's more of a thing.
    – cfr
    Mar 24, 2016 at 1:50

2 Answers 2

4
+100

Maybe something like this that uses the tabto package. The top of the \rmarginpar should be on the call line. I provide an optional argument to give vertical shift to the \rmarginpar, to help the user avoid overlap.

In the MWE below, I use red for the \rmarginpar, just to see more easily what has been added.

\documentclass{ltxdoc}

\usepackage{lipsum}
\usepackage{marginnote}
\usepackage{tabto,xcolor}
\newcommand\rmarginpar[2][0pt]{\leavevmode%
  \tabto*{\dimexpr\linewidth+10pt}\smash{\raisebox{%
  \dimexpr.6\ht\strutbox+#1}{\parbox[t]{2.5cm}{%
    \scriptsize\color{red}\raggedright#2}}}\tabto*{\TabPrevPos}%
}
\begin{document}
\lipsum[1]
\marginpar{This marginal note is on the left. This behavior is expected based on
    the page layout.}
\rmarginpar[20pt]{This marginal note that is made
   to appear on the right of the text.
    I have shifted it up by 20 pt.}
\marginnote[This is on the left, as expected]{}[1in]
\marginnote{This should be on the right of the text.}[2in]
\lipsum[2]
And now here is the third paragraph.
\rmarginpar{Another right marginpar, this one lowered a line.}
\end{document}

enter image description here

2
  • Thank you @Steven B. Segletes! It looks like this will work nicely for my needs. Many of the commands you use (leavevmode, dimexpr, smash, ht, strutbox) are foreign to me. A brief explanation or link to an explanation of these would be appreciated so that I can fully understand this solution. Mar 19, 2016 at 21:14
  • 1
    @AnthonyMottaz \leavevmode exits "vertical" (page) mode, entering "horizontal" (paragraph) mode. This affects some commands that otherwise appear at the beginning of a paragraph. \dimexpr treats the following quantities as lengths, allowing simple addition and subtraction operations therein. \smash creates its argument with zero height and depth. \ht followed by a box name yields a length equal to the height (above the baseline) of the box (\dp is the depth below the baseline of the box that follows). \strutbox is a zero-width vertical box that spans one complete line of text. Mar 20, 2016 at 1:57
2

Okay, I found one solution using the tikzpagenodes package. I defined a new command \rmarginpar which places a tikz node where I want it.

\documentclass{ltxdoc}

\usepackage{lipsum}
\usepackage{tikzpagenodes}

\newcommand{\rmarginpar}[1]{
\tikz[remember picture, overlay] {%
\node[anchor = west, xshift = \textwidth, text width = {\oddsidemargin - 2ex}]
    {#1};
    }%
}

\begin{document}
\lipsum[1]
\marginpar{This marginal note is on the left. This behavior is expected based on
    the page layout.}
\lipsum[2]
\rmarginpar{Hooray! I got this on the right of the text!}

\end{document}

I still have a few problems with this method:

  1. It feels pretty "hacky"... this has nothing to do with normal margin notes. I am just calculation a location to place some text on a page.
  2. The text is vertically centered where it is inserted. I would prefer the vertical placement of text to behave like a normal \marginpar.
  3. Multiple notes overlap each other, and now the marginfix package has no hope of fixing that issue.

Perhaps someone else has a better idea?

Update: 2016/03/11

The solution above has another major issue. Specifically, notes entered using \rmarginpar as defined above must be entered after a line break or paragraph break, since otherwise xshift will push the note off the page. For example:

\documentclass{ltxdoc}

\usepackage{lipsum}
\usepackage{tikzpagenodes}

\newcommand{\rmarginpar}[1]{%
\tikz[remember picture, overlay] {%
\node[anchor = west, xshift = \textwidth, text width = {\oddsidemargin - 2ex}]
    {#1};
    }%
}

\begin{document}
This is a sentence. Here is another one, but this one is a bit longer. Short 
sentence. A medium length sentence. 

\rmarginpar{I need a new line or new paragraph for this note to appear.}
This is a sentence. Here is another one, 
but this one is a bit longer. Short sentence. A medium length sentence. This is 
a sentence. Here is another one, but this one is a bit longer. Short sentence. 
A medium length sentence. This is a sentence. Here is another one, but this one 
is a bit longer. Short sentence.
\rmarginpar{This margin note does not appear since the \texttt{xshift} pushes 
it off the page.}
 A medium length sentence. This is a sentence. Here is another one, but this 
 one is a bit longer. Short sentence. A medium length sentence. This is a 
 sentence. Here is another one, but this one is a bit longer. Short sentence. A 
 medium length sentence. This is a sentence. Here is another one, but this one 
 is a bit longer. Short sentence. A medium length sentence.

\end{document}

Output:

output

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .