20

My documentclass is book, so I've got an even and odd sides. I like that the equation number are always aligned on the right side - independent of the page (even/odd). I started using marginnotes. I wanted to do the same, that the notes are always on the right side of the page? How do I do that?

  • 1
    I know, you asked for the standard class book and Gonzalo Medina has already presented a brilliant answer, but with the KOMA-script class scrbookit would be a simple class option: \documentclass[twoside=semi]{scrbook}. I don’t know the class memoir, but I wouldn’t be surprised, when there were a similar option. In general both in KOMA-script and in memoir there are a lot of settings available as simple options or commands, which need massive redefinition in a standard class, cf. Gonzalo’s answer. – Speravir Aug 31 '12 at 22:08
21

Here's one possible solution patching (with the help of the etoolbox package) the LaTeX kernel command \@addmarginpar:

\documentclass[twoside]{book}
\usepackage[centering,marginparwidth=2.5cm]{geometry}
\usepackage{etoolbox}
\usepackage{lipsum}

\makeatletter
\patchcmd{\@addmarginpar}{\ifodd\c@page}{\ifodd\c@page\@tempcnta\m@ne}{}{}
\makeatother
\reversemarginpar

\begin{document}

\chapter{Test Chapter One}
\section{Test Section}
\lipsum[2]\marginpar{test marginnote one}
\lipsum[2-4]\marginpar{test marginnote two}
\lipsum[2-4]\marginpar{test marginnote three}
\lipsum[4-5]]\marginpar{test marginnote three}
\lipsum[6]

\end{document}

enter image description here

With this layout for marginnotes, it's convenient to activate the centering option for the geometry package; further adjustments might be needed so that marginnotes on even pages can be correctly displayed on a printed document.

The above code provides a solution for the standard LaTeX \marginpar command; if \marginnote (from the marginnote package) is used instead of \marginpar to produce the margin notes, then the internal macro \@mn@@@marginnote will have to be redefined:

\documentclass[twoside]{book}
\usepackage[centering,marginparwidth=2.5cm]{geometry}
\usepackage{marginnote}
\usepackage{lipsum}

\makeatletter
\long\def\@mn@@@marginnote[#1]#2[#3]{%
  \begingroup
    \ifmmode\mn@strut\let\@tempa\mn@vadjust\else
      \if@inlabel\leavevmode\fi
      \ifhmode\mn@strut\let\@tempa\mn@vadjust\else\let\@tempa\mn@vlap\fi
    \fi
    \@tempa{%
      \vbox to\z@{%
        \vss
        \@mn@margintest
        \if@reversemargin\if@tempswa
            \@tempswafalse
          \else
            \@tempswatrue
        \fi\fi
          \rlap{%
            \ifx\@mn@currxpos\relax
              \kern\marginnoterightadjust
              \if@mn@verbose
                \PackageInfo{marginnote}{%
                  xpos not known,\MessageBreak
                  using \string\marginnoterightadjust}%
              \fi
            \else\ifx\@mn@currxpos\@empty
                \kern\marginnoterightadjust
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos not known,\MessageBreak
                    using \string\marginnoterightadjust}%
                \fi
              \else
                \if@mn@verbose
                  \PackageInfo{marginnote}{%
                    xpos seems to be \@mn@currxpos,\MessageBreak
                    \string\marginnoterightadjust
                    \space ignored}%
                \fi
                \begingroup
                  \setlength{\@tempdima}{\@mn@currxpos}%
                  \kern-\@tempdima
                  \if@twoside\ifodd\@mn@currpage\relax
                      \kern\oddsidemargin
                    \else
                      \kern\evensidemargin
                    \fi
                  \else
                    \kern\oddsidemargin
                  \fi
                  \kern 1in
                \endgroup
              \fi
            \fi
            \kern\marginnotetextwidth\kern\marginparsep
            \vbox to\z@{\kern\marginnotevadjust\kern #3
              \vbox to\z@{%
                \hsize\marginparwidth
                \linewidth\hsize
                \kern-\parskip
                \marginfont\raggedrightmarginnote\strut\hspace{\z@}%
                \ignorespaces#2\endgraf
                \vss}%
              \vss}%
          }%
      }%
    }%
  \endgroup
}
\makeatother

\begin{document}

\chapter{Test Chapter One}
\section{Test Section}
\lipsum[2]\marginnote{test marginnote one}
\lipsum[2-4]\marginnote{test marginnote two}
\lipsum[2-4]\marginnote{test marginnote three}
\lipsum[4-5]\marginnote{test marginnote three}
\lipsum[6]

\end{document}

enter image description here

  • Nice answer! Instead of patching the \marginpar command, is it possible to define a new command, e.g., \rightmarginpar? I'm using \marginpar to define a side note command, which I would like to be always on the right margin, but I don't want to modify marginpar because it may affect other commands. Thanks a lot! – Khue May 8 '18 at 15:42
  • I tried your answer in a memoir document but it does not work :( – Khue May 8 '18 at 21:59
  • A recent update of the marginnote package (2018/04/13) has changed the way \@mn@@@marginnote is defined. That method now gives errors reporting that \marginnoterightadjust is undefined. If I'm not beaten to it, when I have a fix to this I'll post it as a new answer below. – rbrignall May 14 '18 at 19:43
3

Further to my comment a few days ago, if you are using the version of the marginnote package dated 2018/04/13 (or newer, at the time of writing), then you need to redefine \@mn@@@marginnote in a different but entirely analogous way.

For those who want to understand what's going on, the code below (and in the answer above) is the half of the definition from marginnote that handles marginnotes in the right hand margin (note the commented-out line \if@tempswa below is what used to find whether we're using an odd or even page: I left this line in to help anyone who is curious).

So, the code below, inserted in your preamble, should be compatible with marginnote versions since April 2018.

\makeatletter
\long\def\@mn@@@marginnote[#1]#2[#3]{%
  \begingroup
    \ifmmode\mn@strut\let\@tempa\mn@vadjust\else
      \if@inlabel\leavevmode\fi
      \ifhmode\mn@strut\let\@tempa\mn@vadjust\else\let\@tempa\mn@vlap\fi
    \fi
    \@tempa{%
      \vbox to\z@{%
        \vss
        \@mn@margintest
        \if@reversemargin\if@tempswa
            \@tempswafalse
          \else
            \@tempswatrue
        \fi\fi
        %\if@tempswa
          \rlap{%
            \if@mn@verbose
              \PackageInfo{marginnote}{xpos seems to be \@mn@currxpos}%
            \fi
            \begingroup
              \ifx\@mn@currxpos\relax\else\ifx\@mn@currxpos\@empty\else
                  \kern-\dimexpr\@mn@currxpos\relax
              \fi\fi
              \ifx\@mn@currpage\relax
                \let\@mn@currpage\@ne
              \fi
              \if@twoside\ifodd\@mn@currpage\relax
                  \kern\oddsidemargin
                \else
                  \kern\evensidemargin
                \fi
              \else
                \kern\oddsidemargin
              \fi
              \kern 1in
            \endgroup
            \kern\marginnotetextwidth\kern\marginparsep
            \vbox to\z@{\kern\marginnotevadjust\kern #3
              \vbox to\z@{%
                \hsize\marginparwidth
                \linewidth\hsize
                \kern-\parskip
                \marginfont\raggedrightmarginnote\strut\hspace{\z@}%
                \ignorespaces#2\endgraf
                \vss}%
              \vss}%
          }%
      }%
    }%
  \endgroup
}
\makeatother
  • I'm sorry to be a pain, but can I ask: what would I need to change in this block to make all notes appear on the left margin? As you say, the old solutions no longer work. This works, but obviously puts all the notes on the right side. Thanks in advance. – Telemachus Jan 31 at 13:38
  • I've posted a parallel question about the left margin here. If you are up for it, post an adjusted answer there, and I'll gladly accept it. – Telemachus Jan 31 at 16:07
1

I met the same question recently, and I found a simple solution in geometry manual. All I need is to add the option asymmetric.

\documentclass[12pt]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{geometry}
\geometry{verbose, a4paper, twoside, showframe, asymmetric}

\renewcommand{\familydefault}{\sfdefault}

\begin{document}

\chapter{}

Hello, \LaTeX.
\marginpar{Hello, \TeX.}

\newpage

Hello, \LaTeX.
\marginpar{Hello, \TeX.}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.