1

Ref: How to put equation numbers on the left or the right, depending on whether the page is odd or even

The original question/request was for equation numbers to be on the right side for even pages, and the left side for odd pages; that is, near the inside margins. ``Jagath'' gave a good answer, but the solution is mostly incomprehensible for my small mind. I tried switching odd and even, left and right, but I am unable to get equation numbers to appear in or near the outer margins.

\documentclass{book}
  \usepackage{lipsum}
  \usepackage{amsmath}
  \usepackage{bm}
  \newcommand{\Cdot}{\bm{\cdot}}

  % Inner margin numbering (I want outer margin numbering)
  \makeatletter
  \def\mathlabel#1{\@bsphack
    \protected@write\@auxout{}%
           {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
    \@esphack}
  \def\eqnWrite{\@bsphack
    \protected@write\@auxout{}%
           {\string\EqnStat{\theequation}{\thepage}}%
    \@esphack}%
  \def\EqnStat#1#2{%
    \expandafter\gdef\csname eqn@#1\endcsname{#2}%
  }
  \newcommand\@reqnnum{\hb@[email protected]\p@{}%
                        \rlap{\normalfont\normalcolor%
                          \hskip -\displaywidth(\theequation)}}
  \def\equation{\let\mathlabel\label$$\refstepcounter{equation}}
  \def\endequation{\eqno\eqnWrite\@ifundefined{eqn@\theequation}{\hbox{\@eqnnum}}%%
       {\expandafter\ifodd\csname eqn@\theequation\endcsname\hbox{\@reqnnum}%
       \else\hbox{\@eqnnum}\fi}$$\@ignoretrue}
  \def\@@eqncr{\let\reserved@a\relax%
      \ifcase\@eqcnt \def\reserved@a{& & &}\or \def\reserved@a{& &}%
       \or \def\reserved@a{&}\else%
         \let\reserved@a\@empty%
         \@latex@error{Too many columns in eqnarray environment}\@ehc\fi%
       \reserved@a \if@eqnsw\eqnWrite%
       \@ifundefined{eqn@\theequation}{\@eqnnum}%
       {\expandafter\ifodd\csname eqn@\theequation\endcsname\@reqnnum%
       \else\@eqnnum\fi}\stepcounter{equation}\fi%
       \global\@eqnswtrue\global\@eqcnt\z@\cr}
  \makeatother

\begin{document}
  \lipsum[1]
  \begin{equation}
    E^2 = (mc^2)^2 + (pc)^2
  \end{equation}
  \lipsum[2]
  \begin{subequations}
  \begin{equation}
    A = B + C
  \end{equation}
  \begin{equation}
    B = C + D
  \end{equation}
  \begin{equation}
    D = E + F
  \end{equation}
  \end{subequations}
  \begin{equation}
  \begin{array}{|cc|}
    d & e \\
    f & g
  \end{array}
  \end{equation}
  \begin{align}
    F &= A\times B\qquad\text{ ALIGN FAILS!}\\
    G\Cdot D &= H\Cdot P\times Q + B\Cdot V
  \end{align}
  \lipsum[3-4]
  \begin{equation}
    E^2 = (mc^2)^2 + (pc)^2
  \end{equation}
  \lipsum[2]
  \begin{subequations}
  \begin{equation}
    A = B + C
  \end{equation}
  \begin{equation}
    B = C + D
  \end{equation}
  \begin{equation}
    D = E + F
  \end{equation}
  \end{subequations}
  \begin{equation}
  \begin{array}{|cc|}
    d & e \\
    f & g
  \end{array}
  \end{equation}
  \begin{align}
    F &= A\times B\\
    G + D &= P\times Q + B
  \end{align}

\end{document}

1 Answer 1

1

There are two \ifodd tests in the code: you have to switch \@reqnnum and \@eqnnum in their definitions.

\documentclass[twoside]{book}
\usepackage{lipsum}
\usepackage{amsmath}
\usepackage{bm}
\newcommand{\Cdot}{\bm{\cdot}}

% Inner margin numbering (I want outer margin numbering)
\makeatletter
\def\mathlabel#1{\@bsphack
    \protected@write\@auxout{}%
    {\string\newlabel{#1}{{\@currentlabel}{\thepage}}}%
    \@esphack}
\def\eqnWrite{\@bsphack
    \protected@write\@auxout{}%
    {\string\EqnStat{\theequation}{\thepage}}%
    \@esphack}%
\def\EqnStat#1#2{%
    \expandafter\gdef\csname eqn@#1\endcsname{#2}%
}
\newcommand\@reqnnum{\hb@[email protected]\p@{}%
    \rlap{\normalfont\normalcolor%
        \hskip -\displaywidth(\theequation)}}
\def\equation{\let\mathlabel\label$$\refstepcounter{equation}}
\def\endequation{\eqno\eqnWrite\@ifundefined{eqn@\theequation}{\hbox{\@eqnnum}}%%
    {\expandafter\ifodd\csname eqn@\theequation\endcsname\hbox{\@eqnnum}% <- Here
        \else\hbox{\@reqnnum}\fi}$$\@ignoretrue}
\def\@@eqncr{\let\reserved@a\relax%
    \ifcase\@eqcnt \def\reserved@a{& & &}\or \def\reserved@a{& &}%
    \or \def\reserved@a{&}\else%
    \let\reserved@a\@empty%
    \@latex@error{Too many columns in eqnarray environment}\@ehc\fi%
    \reserved@a \if@eqnsw\eqnWrite%
    \@ifundefined{eqn@\theequation}{\@eqnnum}%
    {\expandafter\ifodd\csname eqn@\theequation\endcsname\@eqnnum% <- Here
        \else\@reqnnum\fi}\stepcounter{equation}\fi%
    \global\@eqnswtrue\global\@eqcnt\z@\cr}
\makeatother

\begin{document}
    \lipsum[1]
    \begin{equation}
    E^2 = (mc^2)^2 + (pc)^2
    \end{equation}
    \lipsum[2]
    \begin{subequations}
        \begin{equation}
        A = B + C
        \end{equation}
        \begin{equation}
        B = C + D
        \end{equation}
        \begin{equation}
        D = E + F
        \end{equation}
    \end{subequations}
    \begin{equation}
    \begin{array}{|cc|}
    d & e \\
    f & g
    \end{array}
    \end{equation}
    \begin{align}
    F &= A\times B\qquad\text{ ALIGN FAILS!}\\
    G\Cdot D &= H\Cdot P\times Q + B\Cdot V
    \end{align}
    \lipsum[3-4]
    \begin{equation}
    E^2 = (mc^2)^2 + (pc)^2
    \end{equation}
    \lipsum[2]
    \begin{subequations}
        \begin{equation}
        A = B + C
        \end{equation}
        \begin{equation}
        B = C + D
        \end{equation}
        \begin{equation}
        D = E + F
        \end{equation}
    \end{subequations}
    \begin{equation}
    \begin{array}{|cc|}
    d & e \\
    f & g
    \end{array}
    \end{equation}
    \begin{align}
    F &= A\times B\\
    G + D &= P\times Q + B
    \end{align}
    
\end{document}

I don't know how to solve the align issue… enter image description here

1
  • The alignment looks good now, no doubt due to package upgrades. Commented Sep 23, 2022 at 1:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .