# Marginpar notes in the bibliography and vertical position with marginfix

I'm attempting to include notes in my bibliography for important items. I'm putting them in the margin with \marginpar. This is where the problem arises.

If I have a longer note that happens to be for a bibliography item that is at the bottom of a page then it has the potential to flow off the page or outside the margin boundaries. (See full page figure at the bottom of post for reference.) To fix this I'd thought I'd use the marginfix package. When I include the marginfix package in my preamble It solves my large notes flowing off the page problem (hooray!); however, it introduces another problem. Margin notes are now shifted down by some unknown value and no longer line up with the line they were introduced on.

Without marginfix

With marginfix

Long story short. How do I move these back up and preserve the marginpar positioning, but maintain the sweet shifting of marginal notes at the bottom of the page by marginfix?

The current implimentation renews the begentry and the finentry in biblatex to provide the formatting. There are a bunch of if statements to determine if an entry has a note, and if so apply the formatting. I also made some commands to turn the note-base formatting on and off.

I also have some custom commands which add introductory notes to citations à la this wonderful answer. I've left them in the minimal example as I'd really like to keep them. Any solution should maintain this feature.

I'm also open to someone correcting my entire implementation if it happens that I'm patching biblatex bibliographies completely wrong.

MWE

\documentclass[10pt]{article}

% I'm a problem maker (Or is it the author and i've just been maligned?)
% \usepackage{marginfix}

\usepackage[]{geometry}
\geometry{%
twoside,%
a5paper,%
heightrounded,%
textheight=36\baselineskip,%
marginparwidth=5em,%
}

\usepackage[%
backend=biber,%
articletitle=true,%
style=chem-rsc,%
autopunct=true,%
]{biblatex}%

\newtoggle{bbx:printnote}
\DeclareBibliographyOption{printnote}[false]{%
\settoggle{bbx:printnote}{#1}%
}

% Command to turn marginnotes on and off
\newcommand\setprintnotetrue{\settoggle{bbx:printnote}{true}}
\newcommand\setprintnotefalse{\settoggle{bbx:printnote}{false}}

% Command to add a small introductory note to a citation
\makeatletter

% Needed in floating environments
\newcommand*{\nociteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
}

\newcommand*{\citeintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\cite{#1}
}

\newcommand*{\autociteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\autocite{#1}
}

\newcommand*{\textciteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\textcite{#1}
}

\makeatother

% Annotate important items, items of note in bibliography.
\DeclareBibliographyCategory{important}%
\DeclareFieldFormat{note}{%
\thefield{entrykey}%
}\marginpar{%
\scriptsize\raggedright\textbf{\printfield{labelnumber}}~\textbullet~#1\newline%
}%
}

%% METHOD 1
\renewbibmacro*{begentry}{%
\ifcategory{important}{%
% \vspace{1\baselineskip}%
\iftoggle{bbx:printnote}{%
\printfield{note}\clearfield{note}\bfseries%
}%
{%
\clearfield{note}%
}%
}%
{}%
\ifcsdef{cbx@\thefield{entrykey}@intronote}{%
}%
{}%
}

% add extra space after important bib items
\renewbibmacro*{finentry}{%
\ifcategory{important}{%
\iftoggle{bbx:printnote}{%
\vspace{0.5\baselineskip}%
\finentry
}%
{%
\finentry
}%
}%
{%
\finentry
}%
}

% add extra space before important bib items
\AtEveryBibitem{%
\ifcategory{important}{%
\iftoggle{bbx:printnote}{%
\vspace{0.5\baselineskip}%
}%
{}%
}%
{}%
}

\begin{filecontents}{refs.bib}
@article{article:a,
author = {Author, A. Buthor, B.},
year = {2001},
title = {A is the first letter of the English alphabet: Part 1},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:b,
author = {Author, A. Buthor, B.},
year = {2002},
title = {A is the first letter of the English alphabet: Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {A test note in the margin. It needs to be long enough to make it overlap with the next few lines.},
}

@article{article:c,
author = {Author, A. Buthor, B.},
year = {2003},
title = {Alpha},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {Another test note in the margin. It needs to be long enough this should do.},
}

@article{article:d,
author = {Author, A. Buthor, B.},
year = {2004},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 1.},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:e,
author = {Author, A. Buthor, B.},
year = {2005},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:f,
author = {Author, A. Buthor, B.},
year = {2006},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 3},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:g,
author = {Author, A. Buthor, B.},
year = {2007},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 1},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:h,
author = {Author, A. Buthor, B.},
year = {2008},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {Another test note in the margin. This one is really really really really really really really really really really long. Like seriously long. Crazy long. Long enough to break some boxes.},
}

@article{article:i,
author = {Author, A. Buthor, B.},
year = {2009},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 3},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

\end{filecontents}

\begin{document}
\section{A Section}\label{sec:a_section}
Some text written by \textciteintronote{article:a}{The second edition is best.}, it's true!\autocite{article:a,article:b,article:c,article:d,article:e,article:f,article:g,article:h,article:i}

\setprintnotetrue
\printbibliography[title={Marginal notes are \emph{on}}]

\setprintnotefalse
\printbibliography[title={Marginal notes are \emph{off}}]

\end{document}


Full page images for reference

Without marginfix

With marginfix

Bonus question. (I'll move this to a new question if deemed inappropriate) Is there a way to see that the previous item is also important? And so, ignore skip the \vspace at the beginning of the second (and possibly subsequent) bibliography items. That was I don't get the doubling up of the \vspaces in between two important items. Perhaps temporarily editing \bibitemsep?

The documentation of the \marginposadjustment length in marginfix referes to this kind of problem: marginfix only approximates the height of the line and might be off sometimes. In this case the "approximation" seems to be zero, but you can "fix" this by setting \marginposadjustment. An appropriate adjustment is 1em, so you insert

\marginposadjustment=-1em


before including the bibliography:

\documentclass[10pt]{article}

% I'm a problem maker (Or is it the author and i've just been maligned?)
\usepackage{marginfix}

\usepackage[]{geometry}
\geometry{%
twoside,%
a5paper,%
heightrounded,%
textheight=36\baselineskip,%
marginparwidth=5em,%
}

\usepackage[%
backend=biber,%
articletitle=true,%
style=chem-rsc,%
autopunct=true,%
]{biblatex}%

\newtoggle{bbx:printnote}
\DeclareBibliographyOption{printnote}[false]{%
\settoggle{bbx:printnote}{#1}%
}

% Command to turn marginnotes on and off
\newcommand\setprintnotetrue{\settoggle{bbx:printnote}{true}}
\newcommand\setprintnotefalse{\settoggle{bbx:printnote}{false}}

% Command to add a small introductory note to a citation
\makeatletter

% Needed in floating environments
\newcommand*{\nociteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
}

\newcommand*{\citeintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\cite{#1}
}

\newcommand*{\autociteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\autocite{#1}
}

\newcommand*{\textciteintronote}[2]{%
\csdef{cbx@#1@intronote}{#2}%
\textcite{#1}
}

\makeatother

% Annotate important items, items of note in bibliography.
\DeclareBibliographyCategory{important}%
\DeclareFieldFormat{note}{%
\thefield{entrykey}%
}\marginpar{%
\scriptsize\raggedright\textbf{\printfield{labelnumber}}~\textbullet~#1\newline%
}%
}

%% METHOD 1
\renewbibmacro*{begentry}{%
\ifcategory{important}{%
% \vspace{1\baselineskip}%
\iftoggle{bbx:printnote}{%
\printfield{note}\clearfield{note}\bfseries%
}%
{%
\clearfield{note}%
}%
}%
{}%
\ifcsdef{cbx@\thefield{entrykey}@intronote}{%
}%
{}%
}

% add extra space after important bib items
\renewbibmacro*{finentry}{%
\ifcategory{important}{%
\iftoggle{bbx:printnote}{%
\vspace{0.5\baselineskip}%
\finentry
}%
{%
\finentry
}%
}%
{%
\finentry
}%
}

% add extra space before important bib items
\AtEveryBibitem{%
\ifcategory{important}{%
\iftoggle{bbx:printnote}{%
\vspace{0.5\baselineskip}%
}%
{}%
}%
{}%
}

\begin{filecontents}{refs.bib}
@article{article:a,
author = {Author, A. Buthor, B.},
year = {2001},
title = {A is the first letter of the English alphabet: Part 1},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:b,
author = {Author, A. Buthor, B.},
year = {2002},
title = {A is the first letter of the English alphabet: Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {A test note in the margin. It needs to be long enough to make it overlap with the next few lines.},
}

@article{article:c,
author = {Author, A. Buthor, B.},
year = {2003},
title = {Alpha},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {Another test note in the margin. It needs to be long enough this should do.},
}

@article{article:d,
author = {Author, A. Buthor, B.},
year = {2004},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 1.},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:e,
author = {Author, A. Buthor, B.},
year = {2005},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:f,
author = {Author, A. Buthor, B.},
year = {2006},
title = {Alpha is the first letter of the Greek alphabet: a literary masterpiece, Part 3},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:g,
author = {Author, A. Buthor, B.},
year = {2007},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 1},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

@article{article:h,
author = {Author, A. Buthor, B.},
year = {2008},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 2},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
note = {Another test note in the margin. This one is really really really really really really really really really really long. Like seriously long. Crazy long. Long enough to break some boxes.},
}

@article{article:i,
author = {Author, A. Buthor, B.},
year = {2009},
title = {Beta is the second letter of the Greek alphabet: also a literary masterpiece, Part 3},
journal = {Phys Chem Chem Phys},
volume = {1},
page = {101-102},
}

\end{filecontents}

\begin{document}
\section{A Section}\label{sec:a_section}
Some text written by \textciteintronote{article:a}{The second edition is best.}, it's true!\autocite{article:a,article:b,article:c,article:d,article:e,article:f,article:g,article:h,article:i}

\setprintnotetrue