# Small margin notes side by side to avoid overlapping

I defined this function \corr which marks a segment of text or equation in red or blue and adds a marginnote. The problem is having more than one correction per line since then hey overlap. I would like to display them side by side if there is more than one per line. Here is my code:

\documentclass[]{article}
\usepackage{xstring}
\usepackage{marginnote}
\usepackage{tikz}

\newcounter{correctioncounter}

\newcommand{\corr}[2]{%
\refstepcounter{correctioncounter}%
\IfEqCase{#1}{
{a}{{\color{red}#2\marginnote{%
\begin{tikzpicture}%
\draw node[draw,inner sep=2pt] {\tiny \arabic{correctioncounter}};
\end{tikzpicture} }}}% correction
{b}{{\color{blue}#2\marginnote{%
\begin{tikzpicture}%
\draw node[draw,inner sep=2pt] {\tiny \arabic{correctioncounter}};
\end{tikzpicture} }}}% suggestion
{c}{#2}% Turn correction into plain text
{d}{} % Erase
}[\PackageError{correction command}{Undefined for corr: #1}{}]%
}
\begin{document}

Random text with a \corr{a}{correction} in one line.

Having more than one \corr{a}{correction} in \corr{a}{one} line causes them to overlap. Some more random text to show that the annotations are really in the \corr{b}{margin}.

\end{document}


And the output:

You can add another argument for \corr which control \marginparsep and reset it's default value. If the marginnote is not the last in current line you must add add to corr command like this \corr[add]{a}{text}

\documentclass[]{article}
\usepackage{xstring}
\usepackage{marginnote}
\usepackage{tikz}

\newcounter{correctioncounter}

\newcommand{\corr}[3][]{%
\refstepcounter{correctioncounter}%
\IfEqCase{#2}{
{a}{{\color{red}#3\marginnote{%
\begin{tikzpicture}%
\draw node[draw,inner sep=2pt] {\tiny \arabic{correctioncounter}};
\end{tikzpicture}}}}% correction
{b}{{\color{blue}#3\marginnote{%
\begin{tikzpicture}%
\draw node[draw,inner sep=2pt] {\tiny \arabic{correctioncounter}};
\end{tikzpicture} }}}% suggestion
{c}{#3}% Turn correction into plain text
{d}{} % Erase
}[\PackageError{correction command}{Undefined for corr: #2}{}]