[Tex/LaTex] Dealing with many margin notes

marginnote

I have to typeset a book that contains a lot of side material (comments, notes, references, supplementary explanations, …) that I want to appear alongside the main material.

At first, I thought of using the tufte-latex class, which reserves quite a large space for margin notes. Unfortunately, I have to use a b5 paper size, which does not allow for too large margin notes. Also, I do not like that when there are no margin notes, there would be a lot of "white" space on the page.

My idea was to create a type of "side note" that will share the same space with the main text where the note is issued, such that:

  • each paragraph together with all of its side notes will be typeset similar to a two-columns layout;
  • the side notes will appear in the right column; the main text will appear in the left column;
  • the side note should be typeset using a smaller font size;
  • each side note will be typeset in its one "box" over a colored background, and the color can be specified for each note independently;
  • the distribution of the width of the two columns should be computed for each paragraph, depending on the ration between the main text and the side text, such that one obtains the right balance, meaning that:
    • if there are no side notes, the paragraph should be typeset using the full text width;
    • if there are side notes, the width of the right column that contains the side notes should be between a minimum (e.g., 0.3\textwidth) and a maximum (e.g., 0.5\textwidth) value;
    • the height of the right column should not exceed the height of the left column;
    • no side note should break across pages;
    • if the previous criteria can not be satisfied, then the side notes should appear right after the main text, using the full text width (it may break across pages)

I know that I ask too much, and maybe this should be a package, but these were the criteria that I thought would give a good result for my book.

Unfortunately, I don't have enough "TeXperience" to achieve this task, so any help is much appreciated.

Edit: Here is a sample of what I have in mind. It may very well contain a lot of maths.

sample file

Best Answer

EDIT: I've updated the solution below to use the wrapfig environment, which simplified at least some parts of the code. This solved all the spacing issues I had before. I also changed the definition of the command to only require that it is put before each paragraph, and added functionality for putting long notes on new lines. The only issues remaining is that the solution still does not work well with page breaks, and also that it is quite cumbersome to but the same command before each paragraph, even if it is less cumbersome than the syntax of my previous solution.


I've now made a new solution (with the help of David Carlise) which I think is a bit closer to what you intended than my last attempt. It still has some problems though:

  • Some unwanted spaces sometimes appear between paragraphs
  • Notes are never put on separate lines
  • No sidebreaks are allowed inside paragraphs

The result now look as in the image below: example result

And the source code.. :)

    \documentclass[10pt]{article}


% Packages
\usepackage[newcommands]{ragged2e}
\usepackage{newfile}
\usepackage{color}
\usepackage{xparse}
\usepackage{graphics}
\usepackage[dvipsnames]{xcolor}
\usepackage{hyperref}
\usepackage{wrapfig}
\usepackage{ifthen}

% Settings
\colorlet{Pbardefault}{yellow!70} % Default color for the colored bar left to the notes
\colorlet{Plightdefault}{yellow!10}% Default color for background of the note
\def\Pmin{0.5} % ~minimal amount main text
\def\Pmax{0.95} % ~maximal amount main text / \Psf
\def\Psf{1.14} % 


\newcounter{localsidenotecounter}
\newlength{\currentparskip}
\setlength{\currentparskip}{\parskip}


% The formatting of the note
\newdimen\Pparsep
\Pparsep=0.2em

\newdimen\Pcolorwidth
\Pcolorwidth=2mm


% The paragraph command
\makeatletter
\def\sidenotepi#1\par%
{
%
\immediate\openout\tempfile=sidenotes.tex %
%
\twopara%
{
#1   \immediate\closeout\tempfile 
}
{\input{sidenotes.tex}}
}

\makeatother




% A command for finding the optimal text widths
\makeatletter

\long\def\twopara#1#2{%

\newdimen\columnsep
\columnsep=\dimexpr \Pparsep + \Pcolorwidth

 \def\@elt##1{\global\value{##1}\the\value{##1}\relax}% 
\edef\TY@ckpt{\cl@@ckpt}%

\@tempdima\dimexpr\textwidth-\columnsep\relax
{\hbadness\@M\raggedright\hsize.5\@tempdima \@tempdima\hsize
\setbox\z@\vbox{{#1\endgraf}}%
\setbox\tw@\vbox{{#2\endgraf}}%
\Gscale@div\tmp{\ht\z@}{\dimexpr\ht\z@+\ht\tw@\relax}%
\global\let\xtmp\tmp}%
\dimen@ \xtmp\@tempdima
\ifdim\dimen@<\Pmin\@tempdima \dimen@\Pmin\@tempdima\fi
\ifdim\dimen@>\Pmax\@tempdima \dimen@\Pmax\@tempdima\fi
\dimen@ii\dimexpr\@tempdima-\dimen@\relax 

\TY@ckpt


\ifdim\dimen@ii<20pt
  #1
\else
  \ifdim \Psf\dimen@ii>\dimexpr\textwidth-\Pmin\textwidth
    #1
    \let\Pnotebox\Pnotebox@
    \dimen@ii = \dimexpr \textwidth-4mm
      \setlength{\intextsep}{2pt}%
      \begin{figure}[h]
          #2
      \end{figure}
  \else

      \let\Pnotebox\Pnotebox@
      \dimen@ii\dimexpr\Psf\dimen@ii
      \setlength{\intextsep}{0pt}%
      \begin{wrapfigure}{o}[-4mm]{\dimen@ii}
          #2
      \end{wrapfigure}
      #1
      \fi
  \fi

\let\Pnotebox\Pnotebox@@
}
\makeatother



% The unformatted note text
\makeatletter
\long\def\Pnotebox@@#1#2#3{ %barcolor, backcolor, text
#3}
\makeatother

\makeatletter
\long\def\Pnotebox#1#2#3{ %barcolor, backcolor, text
#3}
\makeatother

% The formatting of the notes
\makeatletter
\newdimen\Pnoteheight
\long\def\Pnotebox@#1#2#3{ %barcolor, backcolor, text
\settoheight{\Pnoteheight}{\parbox{\dimen@ii}{#3}}%
\begin{minipage}{\dimexpr\dimen@ii+\Pcolorwidth}
  \textcolor{#1}{\rule[\dimexpr-\Pnoteheight+1mm]{\Pcolorwidth}{\dimexpr 2\Pnoteheight}}%
  \colorbox{#2}{\parbox{\dimen@ii}{#3}}
\end{minipage}%
}
\makeatother



% Commands for making the sidenote

\newwrite\tempfile

\makeatletter

\DeclareDocumentCommand \sidenote {m O{Pbardefault} O{Plightdefault}} {%
\stepcounter{localsidenotecounter}%
\hyperlink{sidenote\arabic{localsidenotecounter}}{${}^\textrm{\footnotesize\arabic{localsidenotecounter}}$}%
\immediate\write\tempfile%
{%
\noexpand\Pnotebox{#2}{#3}{%
\noexpand\footnotesize \noexpand{\noexpand\hypertarget{sidenote\arabic{localsidenotecounter}}{${}^{\arabic{localsidenotecounter}}$} \unexpanded{#1 }}%
}}}
\makeatother



\begin{document}





\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure


\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.


\sidenotepi
But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure} pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.\sidenote{Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure.}[blue][blue!5]

\end{document}
Related Question