# Equations in the margin: automatic flush right or left?

How can I automatically have equations in a \marginnote aligned flush left or right in a two-sided document?

A previous question suggested to make equations flush left by using flalign* and extra alignment characters. This can also be used to make the equations flush right as demonstrated below. The question: how to do this automatically for equations that appear in \marginnotes? The solution probably will use an if-then-else for even/odd pages, but how to cleanly incorporate this with the (fl)align* environment?

\documentclass[twoside]{scrartcl}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{marginnote}
\begin{document}
\lipsum[1]
\marginnote{Is flush left (manually)
\begin{flalign*}
a&=1 &\\
b&=1 &
\end{flalign*}}
\lipsum[2]
\marginnote{How to make flush left automatically?
\begin{align*}
a&=1\\
b&=1
\end{align*}}
\lipsum[3-6]
\marginnote{Is flush right (manually):
\begin{flalign*}
&& a&=1\\
&& b&=1
\end{flalign*}}
\lipsum[7]
\marginnote{How to make flush right automatically?
\begin{align*}
a&=1\\
b&=1
\end{align*}}
\lipsum[8-9]
\end{document}

-

If you do not need numbered equations, then use a simple array

\documentclass[twoside]{scrartcl}
\usepackage{amsmath}
\usepackage{lipsum}
\usepackage{marginnote}
\newenvironment{Array}
{\arraycolsep=1.4pt\par$\array{rl}} {\endarray$}
\begin{document}
\lipsum[1]
\marginnote{Is flush left (manually)
\begin{Array}
a&=1 \\
b&=1
\end{Array}}
\lipsum[2]
\marginnote{How to make flush left automatically?
\begin{Array}
a&=1\\
b&=1
\end{Array}}
\lipsum[3-6]
\marginnote{Is flush right (manually):
\begin{Array}
a&=1\\
b&=1
\end{Array}}
\lipsum[7]
\marginnote{How to make flush right automatically?
\begin{Array}
a&=1\\
b&=1
\end{Array}}
\lipsum[8-9]
\end{document}


-
I think this is going to do it. It would still be nice to be able to use some of the alignment capabilities of align etc. but that will spoil the nice margin alignment anyway, and there is not really space for several aligned columns with most margin notes. I will play with it for a bit and then accept if it works as expected. Thanks! –  mforbes Oct 5 '11 at 7:56
you can add an optional argument to Align for the column specification, then more columns are possible –  Herbert Oct 5 '11 at 8:42
@Herbert, thanks for the answer, but I am experiencing a problem with your solution. If my marginnote only contains an equation, there is a large skip / space inside of the marginnote, that pushes it down and does not appear at the right spot anymore. Is there a workaround to avoid this? I can just do \vspace{-...}, but that's quite a dirty hack. –  Ingo Feb 13 at 14:41
use the inline math mode istead of a displayed one. –  Herbert Feb 13 at 14:43

This is what I would call semi-automatic.

The everyshi package provides a hook into the page shipout routine by means of the \EveryShipout macro. As such, the code

\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\def\flusheqnL{&} \def\flusheqnR{}%
\EveryShipout{%
\ifodd\value{page}
\gdef\flusheqnL{} \gdef\flusheqnR{&&}%
\else
\gdef\flusheqnL{&} \gdef\flusheqnR{}%
\fi
}%


defines \flusheqnL and \flusheqnR to have default values of & and {}, respectively, and alters these values based on the page number \value{page} at shipout. If the page number is odd, the values are swapped to {} and &&, respectively, to accommodate a different alignment on the following (even) page. In fact, these macros are used as hooks for alignment characters in the flalign* environment. However, this requires you to typeset your marginnote equations using the following format:

\marginnote{...
\begin{flalign*}
\flusheqnR ...&... \flusheqnL \\
\flusheqnR ...&... \flusheqnL
\end{flalign*}}


Here's an example of the output:

\documentclass[twoside]{scrartcl}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\usepackage{marginnote}% http://ctan.org/pkg/marginnote
\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\def\flusheqnL{&} \def\flusheqnR{}%
\EveryShipout{%
\ifodd\value{page}
\gdef\flusheqnL{} \gdef\flusheqnR{&&}%
\else
\gdef\flusheqnL{&} \gdef\flusheqnR{}%
\fi
}%

\begin{document}

\lipsum[1]
\marginnote{Is flush left (manually)
\begin{flalign*}
a&=1 &\\
b&=1 &
\end{flalign*}}
\lipsum[2]
\marginnote{How to make flush left automatically?
\begin{flalign*}
\flusheqnR a&=1 \flusheqnL \\
\flusheqnR b&=1 \flusheqnL
\end{flalign*}}
\lipsum[3-6]
\marginnote{Is flush right (manually):
\begin{flalign*}
&& a&=1\\
&& b&=1
\end{flalign*}}
\lipsum[7]
\marginnote{How to make flush right automatically?
\begin{flalign*}
\flusheqnR a&=1 \flusheqnL \\
\flusheqnR b&=1 \flusheqnL
\end{flalign*}}
\lipsum[8-9]
\end{document}


The redefinition of \flusheqnL and \flusheqnR has to be made global (via \gdef) in order for it to hold outside the group (within \EveryShipout).

-