# Memoir footnotes in margin with ragged2e

I would like to set up memoir so that when I use \footnote{foo}, foo is displayed in the margin with the proper label and preferably break over pages.

There is an option \footnotesinmargin which gives almost exactly what I need, only justified. I want it to use ragged2e and change alignment based on even/odd page so that the margin note is always flushed towards the main text area and allows hyphenation on the side of the edge.

\documentclass[12pt, draft]{memoir}
\footnotesinmargin
\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont[Ligatures=TeX,Numbers=OldStyle]{Minion Pro}
\usepackage{kantlipsum}

\begin{document}
\kant[1]\footnote{\kant[1]}
\kant[2]\footnote{\kant[2]}
\kant[3-5]
\end{document}


## Possible partial work-around

I have remembered a nice thesis based upon the Classic thesis template and found the source which indeed contains a possible solution:

\usepackage{marginnote}
\usepackage{ragged2e}
\newcommand{\sidemark}[1]{#1.{\:}}

\newcommand{\marginelement}[2][0]{%
\marginnote{%
\strictpagechecktrue
\checkoddpage
\ifoddpage%
\RaggedRight\footnotesize%
\else%
\RaggedLeft\footnotesize%
\fi%
#2%
}[#1\onelineskip]%
}%

\newcommand{\sidenote}[2][0]{%
\footnotemark%
\ignorespaces%
\marginelement[#1]{%
\sidemark{\thefootnote}%
\ignorespaces#2%
}%
\unskip%
}%


Nevertheless, I would still prefer a memoir-specific solution with the re-definition of footnotes when used with the option \footnotesinmargin mainly for the sake of portability. This work-around also does not work with hyperref (there is no hypertext connection) and does not break over pages automatically.

• In my experience, it is (virtually) impossible to get all three things together for a marginal note: page-breaking (esp. within paragraphs); context-sensitive justification; and floating notes. If your notes are as long as a \kant[1], a solution will be difficult. – jon Mar 22 '14 at 17:39
• @jon Are you familiar with \sidefootnote from memoir? It is aligned to bottom and breaks over pages, so perhaps I could use a combination of two: a) \footnote with \footnotesinmargin for short notes horizontally aligned with the text, and b) \sidefootnote for long notes that need to be broken across pages. The problem is that the latter is not aligned to the text on even pages. Could you suggest such a solution, please? – Harold Cavendish Mar 22 '14 at 18:17
• Yes I am. My suspicion for why memoir adds \sidenote, \sidebar, and \sidefootnote (are there others?) rather than just makes one command that can be made to do all of these things is because it is super-difficult to implement. (I stopped saying impossible regarding tricky implementations because everytime I did, someone would come up with a solution.) Anyway, I looked briefly this morning at how the different marginal notes were implemented, but it doesn't seem easy to combine them at all. I hope to look again tonight, but I'm on a deadline, so it may not happen. – jon Mar 22 '14 at 19:57
• @jon Also \marginpar and \sidepar as far as I am aware. You can see my solution resorting to the use of two different functions and some additional tweaks below. If you produce anything better, I will gladly accept it. You are probably right that there is a reason for this mess, although I remain oblivious to the technical background that makes the implementation of such functionality so difficult. – Harold Cavendish Mar 22 '14 at 21:19

You want to be manipulating the \foottextfont.

Something along these lines should do the trick:

\documentclass[12pt, draft]{memoir}
\usepackage{ragged2e}
\footnotesinmargin
\renewcommand{\foottextfont}{%
\strictpagechecktrue
\checkoddpage
\ifoddpage
\scriptsize\RaggedRight
\else
\scriptsize\RaggedLeft
\fi
}%
\usepackage{marginfix}
\usepackage{polyglossia}
\setmainlanguage{english}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmainfont[Ligatures=TeX,Numbers=OldStyle]{Minion Pro}
\usepackage{kantlipsum}

\begin{document}
\kant[1]\footnote{\kant[1]}
\kant[2]\footnote{\kant[2]}
\kant[3-5]
\end{document}


Note though that with such heavy margin material, the chances of you being able to have the notes placed correctly is slim. I ran into a similar problem with my thesis and wound up increasing the margin space so that I wouldn't have a problem like the one in this example (where footnote #2 winds up on the wrong page).

• Can you give a full example? Manipulating foottextfont does not work for footnotes turned into marginpars via \footnotesinmargin. – jon Mar 22 '14 at 17:40
• This compiled just fine for me, although as I point out, if using marginfix the second footnote gets bumped to the following page so it won't overflow. – Alexander Mar 22 '14 at 17:45
• @Alexander I think you misunderstood my objective. I have just compiled your example and the problem still persists. The margin note in the picture should be aligned to the right. s2.postimg.org/r5s4uax8p/test.png I have already tried manipulating \foottextfont and none of my approaches worked. The documentation says nil. – Harold Cavendish Mar 22 '14 at 18:03
• Successful compilation, sure; but it doesn't actually change any behaviour for marginal notes... – jon Mar 22 '14 at 20:00

I have finally figured it out but I am not going to accept this answer yet because I think it can be done better.

I am combining the use of \footnote with the option \footnotesinmargin and \sidefootnote. The former can be used to make a margin note vertically aligned with the reference mark and is intended for short notes that do not break across pages while the latter is aligned at the bottom of a page, breaks across pages, and is intended for longer notes or if you are running out of space.

Both are hacked to become \RaggedRight or \RaggedLeft based on whether the page on which they appear is even or odd by this code:

## Modify the behaviour of \footnote:

\makeatletter  % All between \makeatletter and \makeatother originally located in memoir.cls
\renewcommand{\@footnotetext}[1]{%
\ifm@mfnmargin%   use marginpar
\marginpar{%
\def\baselinestretch{\m@m@footnote@spacing}%
\reset@font
\foottextfont
\protected@edef\@currentlabel{%
\csname p@footnote\endcsname\@thefnmark}%
\color@begingroup
\@makefntext{\ignorespaces
\strictpagechecktrue         % <-- Start of change
\checkoddpage
\ifoddpage
\scriptsize\RaggedRight
\else
\scriptsize\RaggedLeft
\fi #1}%                             <-- End of change
\color@endgroup}%
\else% regular feet
\insert\footins{%
\def\baselinestretch{\m@m@footnote@spacing}%
\reset@font
\foottextfont
\@preamfntext
\hsize\columnwidth
\protected@edef\@currentlabel{%
\csname p@footnote\endcsname\@thefnmark}%
\color@begingroup
\@makefntext{%
\rule\z@\footnotesep\ignorespaces{\foottextfont #1}%
\@finalstrut\strutbox}%
\color@endgroup}%
\fi%
\m@mmf@prepare}
\makeatother
\footnotesinmargin


## Modify the behaviour of \sidefootnote:

\renewcommand{\sidefoottextfont}{%
\strictpagechecktrue
\checkoddpage
\ifoddpage
\scriptsize\RaggedRight
\else
\scriptsize\RaggedLeft
\fi
}%


You might also want to add

\letcountercounter{sidefootnote}{footnote}


if you want to combine their counters into one, and

\setlength{\footmarkwidth}{0em}
\setlength{\footmarksep}{-\footmarkwidth}
\setlength{\footparindent}{1em}


to modify the appearance of \footnote side notes in order to match \sidefootnote.

Finally, I must say that TeX is the definition of chaos.

I am not sure how to get it to break across pages, but for justification there is an extremely simple fix.

Background

The memoir class provides a \mpjustification command, which is enabled by calling either of:

1. \marginparmargin{}

outer is the default, which sets margin notes on the fore-edge. This also sets up the below command.

1. \setmpjustification{}{}

\raggedleft and \raggedright are default, which you might like to change to \RaggedLeft and \RaggedRight from the ragged2e package.

The \mpjustification command will justify following text depending on what page the text appears on.

Solution

Manipulating the \foottextfont command isn't enough, since this is just a command that appears inside a footnote, and not before it. Basically, you need to be setting up the justification before the footnote is output at all. It's very simple, just put the below after any footnote styling commands:

\let\oldfootnot\footnote
\renewcommand*{\footnote}[1]{\oldfootnote{\mpjustification#1}}


\footnote will now set the text in the margin, justified flush against the type block.