# Sidenote formatting

I want to have sidenotes (alternate footnotes) in the margin. I found this code on the Latex Community website.

\newcommand{\sidenote}[1]{%
\refstepcounter{sidenote}\mbox{\textsuperscript{\alph{sidenote}}}%
\marginpar{\footnotesize \mbox{\textsuperscript{\alph{sidenote}} }#1}%
}
\newcounter{sidenote}

It works, in general, but there is some personalization I would like to make:

1. I would like to have my sidenotes in the opposite (inner) margin. I tried to change the \marginpar to \reversemarginpar, but it did not work. It actually put my sidenotes into the main body of text!

2. I would like to have these fit into paragraph style. (I use the fnpara package to accomplish a similar effect with my footnotes.)

MWE (as requested):

\documentclass[letter]{book}

\newcommand{\sidenote}[1]{%
\refstepcounter{sidenote}\mbox{\textsuperscript{\alph{sidenote}}}%
\marginpar{\footnotesize \mbox{\textsuperscript{\alph{sidenote}} }#1}%
}
\newcounter{sidenote}

\begin{document}

In the beginning\sidenote{Gn 1:1; Pr 8:22-31; Jn 8:58; 17:5; Cl 1:17-18; He 1:8-12; 7:3; 13:8; 1Jn 1:1; Rv 1:4,8,11,17; 2:8; 3:14; 21:6; 22:13} the Word\sidenote{Is 55:11; Jn 1:14; He 4:12; 1Jn 1:1; 5:7; Rv 1:2; 19:13} was \ldots

\end{document}
-
Just for the record: there is a sidenotes package. – clemens Oct 17 '12 at 20:04

The margins are set differently for odd/even pages. So you need to adjust \marginparwidth in order for the \marginpar to fit inside the inner margin:

\documentclass[letter]{book}

\renewcommand{\verse}[1]{\textsuperscript{#1}}

\newcommand{\sidenote}[1]{% \sidenote{<side note>}
\refstepcounter{sidenote}\mbox{\textsuperscript{\alph{sidenote}}}%
\marginpar{\footnotesize\raggedright\strut\mbox{\textsuperscript{\alph{sidenote}} }#1}%
}
\newcounter{sidenote}
\setlength{\marginparwidth}{.8in}
\reversemarginpar
\newcommand{\note}[1]{[#1]}

\begin{document}

\verse{1} \note{\textbf{P}} In the beginning%
\sidenote{Gn 1:1; Pr 8:22-31; Jn 8:58; 17:5; Cl 1:17-18; He 1:8-12; 7:3; 13:8; 1Jn 1:1; Rv 1:4,8,11,17; 2:8; 3:14; 21:6; 22:13}
the Word\sidenote{Is 55:11; Jn 1:14; He 4:12; 1Jn 1:1; 5:7; Rv 1:2; 19:13}
was \note{\textit{the Word already existed; there was the Word}}.
\newpage
\verse{2} \note{\textbf{P}} In the beginning%
\sidenote{Gn 1:1; Pr 8:22-31; Jn 8:58; 17:5; Cl 1:17-18; He 1:8-12; 7:3; 13:8; 1Jn 1:1; Rv 1:4,8,11,17; 2:8; 3:14; 21:6; 22:13}
the Word\sidenote{Is 55:11; Jn 1:14; He 4:12; 1Jn 1:1; 5:7; Rv 1:2; 19:13}
was \note{\textit{the Word already existed; there was the Word}}.
\end{document}

I've added a \raggedright to the \marginpar alignment to make for better reading.

The following is an implementation using flowfram to manage the flow of text from one page to the next. In fact, flowfram mimics the boundaries set by geometry by setting up frames (static, dynamic and flow). We use the last two to create the solution.

The flow frames contain the text block detail, while dynamic frames contain the side notes. Each odd/even page uses its own flow and dynamic frame, the latter being appended with a new side note as they are used. A supplementary \sidenoteblock is kept to measure the height of the side notes on the page. If the side note addition exceeds the text block height, a \newpage is issued and the side note is reset.

\documentclass{book}
\newlength{\innermargin}\setlength{\innermargin}{\dimexpr1in+2em}
\setlength{\marginparwidth}{.75in}
\usepackage[twoside,hmargin={\innermargin,1.5in}]{geometry}% http://ctan.org/pkg/geometry
\usepackage{flowfram}% http://ctan.org/pkg/flowfram
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{lipsum}% http://ctan.org/pkg/lipsum

% Regular odd/even text blocks
%  \newflowframe[<page list>]{<width>}{<height>}{<x>}{<y>}[<label>]
\newflowframe[odd]{\textwidth}{\textheight}{0pt}{0pt}[oddpage]
\newflowframe[even]{\textwidth}{\textheight}{0pt}{0pt}[evenpage]
% Inner odd/even margin blocks
%  \newdynamicframe[<page list>]{<width>}{<height>}{<x>}{<y>}[<label>]
\newdynamicframe[odd]{\marginparwidth}{\textheight}
{\dimexpr-\marginparsep-\marginparwidth}{0pt}[oddsidenote]
\newdynamicframe[even]{\marginparwidth}{\textheight}
{\dimexpr\textwidth+\marginparsep}{0pt}[evensidenote]
\EveryShipout{% At every shipout
\setdynamiccontents*{oddsidenote}{\rule{0pt}{1em}\footnotesize\raggedright}% Clear odd side notes
\setdynamiccontents*{evensidenote}{\rule{0pt}{1em}\footnotesize\raggedright}% Clear even side note
\gdef\sidenoteblock{\footnotesize\raggedright}%
}
\AtBeginDocument{% At \begin{document}
\setdynamiccontents*{oddsidenote}{\rule{0pt}{1em}\footnotesize\raggedright}% Clear odd side notes
\setdynamiccontents*{evensidenote}{\rule{0pt}{1em}\footnotesize\raggedright}% Clear even side note
\gdef\sidenoteblock{\footnotesize\raggedright}%
}
\AtEndDocument{% At \begin{document}
\appenddynamiccontents*{oddsidenote}{\par}% Finalize odd side notes
\appenddynamiccontents*{evensidenote}{\par}% Finalize even side notes
}

\makeatletter
\newcounter{sidenote}\renewcommand{\thesidenote}{\arabic{sidenote}}
\newcommand{\sidenoteblock}{}
\newcommand{\sidenote}[1]{% \sidenote{<side note>}
\refstepcounter{sidenote}%
\protected@edef\the@sidenote{\textsuperscript{\thesidenote}}% http://tex.stackexchange.com/a/40006/5764
\sbox0{\begin{minipage}{.75in}\sidenoteblock\end{minipage}}%
\ifodd\value{page}%
\getdynamicid{\@tempb}{oddsidenote}%
\else
\getdynamicid{\@tempb}{evensidenote}%
\fi%
\ifdim\dimexpr\ht0+\dp0>\textheight%
\appenddynamiccontents{\@tempb}{\par}%
\sidenote{#1}%
\else
\textsuperscript{\thesidenote}%
\expandafter\appenddynamiccontents\expandafter{\expandafter\@tempb\expandafter}\expandafter{\the@sidenote #1\space}%
\fi
}
\makeatother
\begin{document}
\sidenote{Here is the first side note.}\lipsum[1]
\sidenote{Here is the second side note.}\lipsum[2]
\sidenote{This is the third side note.}\lipsum[3]
\sidenote{Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.
Here is another, now the fourth, side note.}\lipsum[4]
\sidenote{Side note number five.}\lipsum[5]
\sidenote{Another, the sixth one.}\lipsum[6]
\sidenote{More side notes are coming. This is number seven.}\lipsum[7]
\sidenote{Eight, yes, this is the eighth.}\lipsum[8]
\end{document}

Caveat: The above solution will not work for a side note that is longer that the text block height.

You can define

\newcommand{\sidenote}[2]{% \sidenote{<word>}{<side note>}
\refstepcounter{sidenote}%
\protected@edef\the@sidenote{\textsuperscript{\thesidenote}}% http://tex.stackexchange.com/a/40006/5764
\sbox0{\begin{minipage}{.75in}\sidenoteblock\end{minipage}}%
\ifodd\value{page}%
\getdynamicid{\@tempb}{oddsidenote}%
\else
\getdynamicid{\@tempb}{evensidenote}%
\fi%
\ifdim\dimexpr\ht0+\dp0>\textheight%
\appenddynamiccontents{\@tempb}{\par}%
\sidenote{#1}{#2}%
\else
#1\textsuperscript{\thesidenote}%
\expandafter\appenddynamiccontents\expandafter{\expandafter\@tempb\expandafter}\expandafter{\the@sidenote #2\space}%
\fi
}

that allows you to use \sidenote{<word>}{<side note>}, which makes sure that <word> and <side note> are always together, while the side note reference always follows the word; similar to

... blah blah blah word7 blah blah blah ...

-
Werner, This perfectly answers the desire to switch sides for the notes. Out of curiosity, why did you also add \strut? I couldn't tell a difference when it was there or not. Any thoughts on the paragraph style in the sidenotes? – themarshian Oct 17 '12 at 20:00
With the paragraph style, I would like for the notes to start at the top of the page, regardless of where the first note is. Is this possible? – themarshian Oct 17 '12 at 20:03
@themarshian: The \strut is not always necessary, but it sometimes used when dealing with paragraph boxes to allow for an even height of the first/last line. Since you're not using it in paragraph text, it could probably be removed. Regarding paragraph style: I'll look into such options, but it would not be easy. What should happen to if the entire margin is filled with sidenotes yet you still want to add more? Where do they go? – Werner Oct 17 '12 at 20:13
If the sidenote gets too full, it ideally would force the start of the next page, moving the corresponding main text with it. – themarshian Oct 17 '12 at 20:49
@themarshian: Wow, asking for the moon and the stars! :) Interaction between the text block and the margin block would require some overhead. As mentioned, an interesting problem, but a large scope in general... – Werner Oct 17 '12 at 20:54

You can use footmisc package with option side, which replaces footnotes for numbered \marginpars, reformating \thefootnote for alphabetic references with

\renewcommand{\thefootnote}{\alph{footnote}}

Notice that if you have long footnotes, you might need to load marginfix to dea with page breaks. Also, even with marginfix, if you have too many footnotes you might run into Too many floats errors.

Here's a MWE:

\documentclass[twoside]{article}
\usepackage[side]{footmisc}
\renewcommand{\thefootnote}{\alph{footnote}}
\usepackage{kantlipsum}% dummy text
\usepackage{marginfix}
\begin{document}
This is a short footnote\footnote{Short footnote.} and another\footnote{Another short footnote.}.

\kant*[3]\footnote{Yet another short one.}

\kant*[4]\footnote{But: Too many (or too long) footnotes won't work for these are still marginpars. \emph{That's why we need \textbf{marginfix}}}

\kant*[5]\footnote{\kant*[6]}

\kant*[7]\footnote{\kant*[8]}

\kant*[9]\footnote{\kant*[10]}

Marginfix however might have a limit for too many footnotes will become too many floats, and you might end up with none being typeset.
\end{document}

The memoir class also provides a \sidebar and a \sidefootnote command: while the latter behaves pretty much like a \marginpar (the note is typeset next to its anchor), the first typesets all sidenotes in the bottom of the page.

-
Any way you know of to have the sidenotes that either the \footmisc package or the memoir class creates to be in paragraph style, similar to the effect that fnpara does for footnotes? – themarshian Oct 18 '12 at 16:52
I'm sorry, I had completely ignored that request! footmisc provides a para option, but it is incompatible with its side option. I don't know if memoir can do it. – henrique Oct 18 '12 at 18:13