# Equation tag in twoside-mode outer

At the moment I thinking about the following think. When I use the twoside mode to write a book I want to print the number of the equation outer.

I can modify it manually with these two commands:

\makeatletter
\def\LEFTTAG{\tagsleft@true}\def\RIGHTTAG{\tagsleft@false}
\makeatother


Here a minimal example:

\documentclass[a4paper]{book}
\usepackage{multido,lipsum,showframe}
\usepackage{amsmath}
\begin{document}
\chapter{foo}
\multido{\iA=0+1}{10}{%
\lipsum[1]
\begin{align}
x+y
\end{align}}
\end{document}


How can I modify the default environments to print the tags outer?

EDIT In relation to the answers of Werner and the following code the possibilities doesn't work well:

\usepackage{scrextend,etoolbox}
\makeatletter
\preto\set@field{%
\ifthispageodd{\global\tagsleft@false}{\global\tagsleft@true}%
}
\makeatother

-
Don't; equation numbers are not like page numbers. – egreg Aug 29 '11 at 7:46
@egreg: What do you mean? – Marco Daniel Aug 29 '11 at 8:08
you can redefine the equation environments and then set for every equation an internal label. Then you can have a reference to \ifodd\pageref{eqn:label} – Herbert Aug 29 '11 at 8:11
I mean that equation numbers must go on the same side always. Page numbers can go on the outer side because they are used in a different way, when thumbing through the book for finding a page; equation numbers are looked at by following the text and it's immaterial what page they are in. – egreg Aug 29 '11 at 8:16

As a first attempt at solving this, the everyshi package is of great help. In the definition below, a choice between left/right tag is made based on whether the page number is odd/even:

\usepackage{everyshi}% http://ctan.org/pkg/everyshi
\usepackage{xifthen}% http://ctan.org/pkg/xifthen
...
\makeatletter
\EveryShipout{%
\ifthenelse{\isodd{\value{page}}}% Test page number
{\global\tagsleft@true}% Following page will have left-tagged equations
{\global\tagsleft@false}% Following page will have right-tagged equations
}
\makeatother


There's one minor hiccup. Whenever typesetting an equation at the bottom of a page that does not entirely fit and has to be broken/flushed to the next, the \EveryShipout command is "too late" in modifying the tag location (either left or right). One remedy for this is some manual intervention to make sure the equation is pushed to the next page after the page is shipped out (for example, by issuing a manual \break or perhaps using the needspace package). The latter, perhaps more elegant and automated approach, requires the definition of a new environment (using the environ package):

\usepackage{needspace}% http://ctan.org/pkg/needspace
\usepackage{environ}% http://ctan.org/pkg/environ
...
\NewEnviron{altalign}{% Alternating tag align \begin{altalign} ... \end{altalign}
\setbox0=\hbox{% Store contents in box0
\begin{minipage}{\linewidth}\begin{align*}% Unnumbered align
\BODY
\end{align*}\end{minipage}
}%
\Needspace{\ht0}% Need exactly height of box0 at the bottom of the page
\begin{align}
\BODY% Typeset regular tagged align
\end{align}
}


The use of the environ package \NewEnviron is essential here, since the body/contents of an environment needs to be captured in order to use it (\BODY in this case). In essence, the provided altalign environment typesets the contents of the required align in an align* environment (that is, without equation numbers), embedded in a minipage of width \linewidth, and stores everything in a box (box0). Then, \Needspace is issued to either do nothing if there is enough space (\ht0) on the page, or \break if not. Subsequently, the equations are typeset in the traditional align (numbered) environment, this time with the correct addition of EveryShipout. The solution works since the align environment does not break across pages/columns and can therefore be treated as a single block/box.

-
Thanks for the inspiration. I tried it. Unfortunately this solution isn't enough for me ;-). During my test I tried something like: \usepackage{scrextend} \usepackage{etoolbox} \makeatletter \preto\set@field{% \ifthispageodd{\global\tagsleft@false}{\global\tagsleft@true}% } \makeatother but with the same result. – Marco Daniel Aug 28 '11 at 17:24
@Marco: I've updated my answer to correct for this unwanted, yet TeX-correct, behaviour. I've also included a description of the solution method. – Werner Aug 30 '11 at 6:40