# marginnote placed wrongly (linebreak) within an environment

Within an environment (see environ), I have a marginnote placed next to the environment header. The header contains some text. If that text extends towards the end of the line (but even a bit before that as the MWE shows), then the margin note is not placed next to the environment header anymore (it seems that a line break occurs in the marginnote). Why? And how can I fix the environment to avoid this? I guess there's an additional space (where?) generated from the environment header which then pushes the margin note to the next line.

\documentclass{scrbook}
\usepackage[american]{babel}
\usepackage{amsmath}
\usepackage{marginnote}
\usepackage{hyperref}

% Environment
\newcounter{counter}% environment counter
\numberwithin{counter}{chapter}% number counter within chapters
\makeatletter
\newenvironment{environ}[2][]{\refstepcounter{counter}\par
\normalfont\topsep6\p@\@plus6\p@\relax
\trivlist
\item[\hskip\labelsep\sffamily\bfseries MyEnviro~\thecounter\ #1]%
\marginnote{OtherEnviro p.~\pageref{#2}}\ignorespaces
}{%
\endtrivlist\@endpefalse
}
\makeatother

\begin{document}
\chapter{That's the problem}

\begin{environ}[(Some text which covers the amount of space to show the problem)]{foobar}\\
This seems odd (vertical space, wrong placement 'OtherEnviro') because the line
is actually not ending there.
\end{environ}

\begin{environ}[(Some shorter text to show that there's no problem here)]{foobar}\\
This is okay
\end{environ}
\end{document}

\marginnote adds a invisible box, something like \mbox{}. The separation (\labelsep) after the \item in addition with the \mbox{} results in the empty line. BTW: Without this box your \\ would result in a LaTeX Error: There's no line here to end.

So to avoid the problem, set \labelsep to 0pt and (optionally) add the \\ to the definition of the environment.

\documentclass{scrbook}
\usepackage[american]{babel}
\usepackage{amsmath}
\usepackage{marginnote}
\usepackage{hyperref}

% Environment
\newcounter{counter}% environment counter
\numberwithin{counter}{chapter}% number counter within chapters
\makeatletter
\newenvironment{environ}[2][]{\refstepcounter{counter}\par
\normalfont\topsep6\p@\@plus6\p@\relax
\trivlist
\labelsep 0pt
\item[\hskip\labelsep\sffamily\bfseries MyEnviro~\thecounter\ #1]%
\marginnote{OtherEnviro p.~\pageref{#2}}\\*
\ignorespaces
}{%
\endtrivlist\@endpefalse
}
\makeatother

\begin{document}
\chapter{That's the problem}

\begin{environ}[(Some text which covers the amount of space to show the problem)]{foobar}
This seems odd (vertical space, wrong placement 'OtherEnviro') because the line
is actually not ending there.
\end{environ}

\begin{environ}[(Some shorter text to show that there's no problem here)]{foobar}
This is okay
\end{environ}
\end{document}

I've used \\* instead of \\ to avoid page breaks immediately after the heading.