# Margin notes with little arrowhead that is aligned with margin area border

I'm trying to create a two-sided document with an outer margin containing margin notes. The outer margin area has a colored background, e.g. gray. Each margin note is supposed to be associated with a little arrowhead / triangle that points inward, from the margin note to the body text line that the note relates to.

Now, the crux is that the little arrowhead should have a (vertically extending) base that lies flat against the (also vertically extending) inner edge of the margin area. I managed to get this working for left pages, but not for the right pages. It seems that marginnote always uses the left edge of the margin area to horizontally position the note, and that the little triangle causes issues on right pages as its point always leans against the left edge (thus pushing the note frame to the right, out of the margin area).

Anyway, this is what I try to achieve (left page):

And this is what I get instead on right pages:

My code thus far looks something like this:

\documentclass[9pt]{extbook}
% --------------------------
% Packages
% --------------------------
\usepackage{xparse}
\usepackage{calc}
\usepackage{marginnote}
\usepackage[x11names,dvipsnames]{xcolor}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{background}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage[backgroundcolor=gray!40,%
hidealllines=true,%
roundcorner=5pt,%
skipabove=15pt,%
skipbelow=15pt,%
leftmargin=0cm,%
rightmargin=0cm,%
innertopmargin=5pt,%
innerbottommargin=5pt,%
innerleftmargin=5pt,%
innerrightmargin=5pt,%
framemethod=tikz%
]{mdframed}%

% --------------------------
% Page geometry
% --------------------------
\usepackage[includemp,
twoside,
marginparwidth=45mm,
marginparsep=5mm]{geometry}

% --------------------------
% Color the margins
% --------------------------
\backgroundsetup{%
scale=1,%
angle=0,%
contents={\tikz[remember picture,overlay]\fill[gray!80] (current page marginpar area.north west) rectangle (current page marginpar area.south east);}%
}

% --------------------------
% Margin notes
% --------------------------
\newlength{\mnvoffset}%
\newcommand{\mn}[2]{%
\leavevmode%
\setlength{\mnvoffset}{#1pt - 29.5pt}%
\marginnote[{\begin{mdframed}[singleextra={\fill[black] ( $(P) + (0,-11pt + #1pt)$ ) -- +(3pt,3pt)  -- +(0,6pt) -- cycle;}]\sloppy\small#2\end{mdframed}}]%
{\begin{mdframed}[singleextra={\fill[black] ( $(P-|O) + (0,-11pt + #1pt)$ ) -- +(-3pt,3pt) -- +(0,6pt) -- cycle;}]\sloppy\small#2\end{mdframed}}[\mnvoffset]%
}

\begin{document}
\blindtext
\mn{0}{Test}
\blindtext
\newpage
\blindtext
\mn{0}{Test}
\blindtext
\end{document}


Can the code be fixed to achieve the desired effect, or should I try an entirely different approach?

• There is a way (but not a very stable one) to decide if a marginnote is on a recto or a verso page. memoir had the code. – Oleg Lobachev Dec 12 '17 at 22:52
• @OlegLobachev you mean an odd or even page? marginnote does already detect that. That's why he uses two different definitions based on the page (one with the little arrow to the left, one to the right). – Skillmon Dec 12 '17 at 23:06
• This code renders correctly for me (TL 2017). – Alan Munn Dec 13 '17 at 3:42
• @AlanMunn Do you mean that, when you run the code, the right page actually looks OK, and not the way it does in the picture? I also run TL2017, but for me it ends up with a right-shifted note frame box as shown. – ozzy Dec 13 '17 at 7:17
• @AlanMunn activate showframe in geometry's options. – Skillmon Dec 13 '17 at 8:52

The following is a very hacky way but seems to give the desired results (you should test that though...):

\documentclass[9pt]{extbook}
% --------------------------
% Packages
% --------------------------
\usepackage{xparse}
\usepackage{calc}
\usepackage{marginnote}
\usepackage[x11names,dvipsnames]{xcolor}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{background}
\usepackage[english]{babel}
\usepackage{blindtext}

\usepackage[backgroundcolor=gray!40,%
hidealllines=true,%
roundcorner=5pt,%
skipabove=15pt,%
skipbelow=15pt,%
leftmargin=0cm,%
rightmargin=0cm,%
innertopmargin=5pt,%
innerbottommargin=5pt,%
innerleftmargin=5pt,%
innerrightmargin=5pt,%
framemethod=tikz%
]{mdframed}%

% --------------------------
% Page geometry
% --------------------------
\usepackage[includemp,
showframe,
twoside,
marginparwidth=45mm,
marginparsep=5mm]{geometry}

% --------------------------
% Color the margins
% --------------------------
\backgroundsetup{%
scale=1,%
angle=0,%
contents={\tikz[remember picture,overlay]\fill[gray!80] (current page marginpar area.north west) rectangle (current page marginpar area.south east);}%
}

% --------------------------
% Margin notes
% --------------------------
\newlength{\mnvoffset}%
\newcommand{\mn}[2]{%
\leavevmode%
\setlength{\mnvoffset}{#1pt - 29.5pt}%
\marginnote[{\begin{mdframed}[singleextra={\fill[black] ( $(P) + (0,-11pt + #1pt)$ ) -- +(3pt,3pt)  -- +(0,6pt) -- cycle;}]\sloppy\small#2\end{mdframed}}]%
{\hspace*{-3pt}\parbox[t]{\marginparwidth}{\strut\begin{mdframed}[singleextra={\fill[black] ( $(P-|O) + (0,-11pt + #1pt)$ ) -- +(-3pt,3pt) -- +(0,6pt) --
cycle;}]\sloppy\small#2\end{mdframed}}}[\mnvoffset]%
}

\begin{document}
\blindtext
\mn{0}{Test}
\blindtext
\newpage
\blindtext
\mn{0}{Test}
\blindtext
\end{document}


• Awesome! Precisely the kind of hack I was hoping for; works like a charm. Thanks a lot. – ozzy Dec 13 '17 at 19:17
• @Ozzy I'm not sure whether vertical placement is unaffected by this, you should really test that if not done already. – Skillmon Dec 13 '17 at 20:52
• I did indeed make some fine-tuning adjustments to the vertical placement of various elements (possibly rendered necessary by the hack; not really sure), but the variables for that were already built-in into the code, so that was no problem. – ozzy Dec 13 '17 at 22:29