tcolorbox in margin notes

I'm trying to create a command that will allow me to add little "important notes" on the margin of my document. I have managed to make it work as long as as I use the "left" option for the box and the page number is odd. But when I try to place it on an even numbered page it doesn't show anything, whether I use the "left" or "right" options.

\documentclass[a4paper,11pt]{book}

\usepackage[outer=5cm, heightrounded, marginparwidth=4cm, marginparsep=0.5cm]{geometry}

\usepackage[T1]{fontenc}        % to get Type 1 fonts
\usepackage[utf8]{inputenc}     % to enable non ASCII characters
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{marginnote}
\usepackage{xcolor}
\usepackage[most,breakable,many]{tcolorbox}
\tcbset{before skip=0pt,after skip=0pt}
\usepackage{lipsum}         % dummy text

%%%%%%%%%%%%%%%%%%%%%%%%% Important Note %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{important}
\newtcolorbox[use counter=important]
{important}[1][]
{title=Important Note~\thetcbcounter,
width=4cm,
left=0pt,
right=0pt,
fonttitle=\bfseries\color{white},
colframe=olive,
colback=olive!10,
#1
}
\newcommand\Limportant[3][]{%
\marginnote[#1]{%
\makebox[0pt][l]{\begin{important}[label=#3]
#2
\end{important}}}}%

\newcommand\Rimportant[3][]{%
\marginnote[#1]{%
\makebox[0pt][r]{\begin{important}[label=#3]
#2
\end{important}}}}%

\begin{document}
\lipsum[1]
\Limportant[-2cm]{\lipsum[75]}.
\vfill
\pagebreak

\lipsum[1]
\Rimportant{\lipsum[75]}.

\end{document}


Ideally I would like to combine both options so that it automatically decides on which side it should be placed, but I have not figured out how to calculate '$'\pagenumber modulo 2 '$' so I could potentially include it as a command with cases.

In the same command I might have written something wrong because adding the optional command for vertical adjustment has no effect (the -2cm at '$'\Limportant[-2cm]{\lipsum[75]}'$')

• tcolorbox has a command to check for odd and even pages. Please consider to accept answers to your questions instead of piling up new questions! – user31729 Jul 3 '16 at 7:37
• Now we're talking ;-) That's how TeX.SX works -- not only for my answers, but also for other answers (and questions too) of course – user31729 Jul 3 '16 at 9:34

Without using if odd page etc. options from tcolorbox

There were some issues with the arguments etc, i.e. wrongly used for \marginnote.

\documentclass[a4paper,11pt]{book}

\usepackage[outer=5cm, heightrounded, marginparwidth=4cm, marginparsep=0.5cm]{geometry}

\usepackage[T1]{fontenc}        % to get Type 1 fonts
\usepackage[utf8]{inputenc}     % to enable non ASCII characters
\usepackage{lmodern}
\usepackage[english]{babel}
\usepackage{marginnote}
\usepackage{xcolor}
\usepackage[most,breakable,many]{tcolorbox}
\tcbset{before skip=0pt,after skip=0pt}
\usepackage{lipsum}         % dummy text

%%%%%%%%%%%%%%%%%%%%%%%%% Important Note %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcounter{important}
\newtcolorbox[use counter=important]
{important}[1][]
{title=Important Note~\thetcbcounter,
width=4cm,
left=0pt,
right=0pt,
fonttitle=\bfseries\color{white},
colframe=olive,
colback=olive!10,
#1
}

\newcommand{\isimportant}[3][]{%
\ifodd\value{page}
\marginnote{%
\makebox[0pt][l]{%
\begin{important}[label=#3]
#2%
\end{important}%
}%
}[\ifblank{#1}{0pt}{#1}]%
\else
\marginnote[%
{%
\makebox[0pt][r]{%
\begin{important}[label=#3]%
#2%
\end{important}
}%
}]{}[\ifblank{#1}{0pt}{#1}]%
\fi
}

\begin{document}
\lipsum[1]
\isimportant[-5cm]{\lipsum[75]}{Foo}
\vfill
\clearpage
\lipsum[1]
\isimportant[-2cm]{Something different}{Otherstuff}.

\end{document}


• Awesome! that is exactly what I need – Mahoma Jul 3 '16 at 9:21