# Testing and modifying white space around an environment

I'm trying to create something similar to marginpar but without the hassle of floats (particularly because I'm going to have a lot of these).

First issue These notes always appear at the beginning of a paragraph, and I would like to be able to take into account whether the paragraph has been indented or not (such as whether it's followed a section command). I really only need to know this so I can position the note correctly in the right hand margin. I suppose there may be so tikz trick for this. If you know of one, I'd greatly appreciate your suggestions for finding the position of the right margin of the current line. I'd also be very interested in non-tikz answers too.

Second issue I want to be able to kill the extraneous whitespace after the environment. I know I can manually add % after the environment. But I'd like not to have to do this. I thought using etoolbox I could achieve this effect with the hook \AtEndEnvironment, but that's not working.

Here's a MWE

\documentclass{article}
\usepackage[driver=dvips,
top=0.5in,bottom=0.5in,
left=0.5in,right=3.25in,
footskip=1.5\baselineskip,
]{geometry}
\pagestyle{empty}
\usepackage{xcolor}
\usepackage{etoolbox}
\usepackage{lipsum}
%%'
\newsavebox{\aemarginpar}
\newenvironment{mnote}
{%
\begin{lrbox}{\aemarginpar}\color{blue}%
\begin{minipage}[t]{\dimexpr 3.25in - 2em - 0.5in\relax}%\raggedright%
}{%
\end{minipage}%
\end{lrbox}%
\rule{0pt}{0pt}%
\hspace*{\linewidth}%
\raisebox{0pt}[0pt][0pt]{%
\makebox[0pt][l]{\hspace*{2em}\usebox{\aemarginpar}}%
}%
\hspace*{-\linewidth}%
}
\AtEndEnvironment{mnote}{\ignorespaces}
\begin{document}

\section{Hello}

\begin{mnote}
First margin note.  I want to kill white space at the end of the
environment, but even with \verb=etoolbox=, I can't seem to get rid of
the extra white space.
\end{mnote}
\lipsum[1-2]

\begin{mnote}
My margin note is pushed too far to the left which is, of course,
because the paragraph has been indented.
\end{mnote}
\lipsum[3-4]

\end{document}


And here's the result:

-
You can use the package marginnote. –  Marco Daniel May 11 '13 at 16:25

Using \ignorespacesafterend to ignore spaces after \end. As requested this just adds the box as positioned, but of course it will go off the page if the paragraph is near the bottom of the page, which is why standard marginpars are designed to move.

\documentclass{article}
\usepackage[%driver=dvips,
top=0.5in,bottom=0.5in,
left=0.5in,right=3.25in,
footskip=1.5\baselineskip,
]{geometry}
\pagestyle{empty}
\usepackage{xcolor}

\usepackage{lipsum}
%%'
\newsavebox{\aemarginpar}
\newenvironment{mnote}
{%
\begin{lrbox}{\aemarginpar}\color{blue}%
\begin{minipage}[t]{\dimexpr 3.25in - 2em - 0.5in\relax}%\raggedright%
}{%
\end{minipage}%
\end{lrbox}%
\smash{\raise\dp\strutbox\hbox{%
\kern\dimexpr\textwidth+\marginparsep\relax
\usebox{\aemarginpar}}}}%
\ignorespacesafterend}

\begin{document}

\section{Hello}

\begin{mnote}
First margin note.  I want to kill white space at the end of the
environment, but even with \verb=etoolbox=, I can't seem to get rid of
the extra white space.
\end{mnote}
\lipsum[1-2]

\begin{mnote}
My margin note is pushed too far to the left which is, of course,
because the paragraph has been indented.
\end{mnote}
\lipsum[3-4]

\end{document}

-
Very nice. It works correctly even if embedded in the middle of a paragraph. I thought \textwidth was a rigid length. What is keeping track of the remaining length of the line? –  A.Ellett May 11 '13 at 16:50
It is the vadjust: the note is added to the space between the lines, the horizontal position of the vadjust is not recorded by TeX. –  David Carlisle May 11 '13 at 17:02