# How to define switches for turning on and off the option fleqn of amsmath

Then, how to define switches for turning on and off the option fleqn after amsmath is loaded?

I looked into the source of amsmath.sty and define the switches as

\makeatletter
\newcommand*{\fleqnon}{\setboolean{@fleqn}{true}}
\newcommand*{\fleqnoff}{\setboolean{@fleqn}{false}}
\makeatother


These two switches work only when amsmath is loaded with the option fleqn.

MWE:

\documentclass[b6paper]{scrartcl}

%\usepackage[fleqn]{amsmath}  % works with the option fleqn
\usepackage{amsmath}          % fails without the option fleqn
\usepackage{ifthen}

\makeatletter
% https://tex.stackexchange.com/a/212099
\newcommand*{\leqnomode}{\tagsleft@true\let\veqno=\@@leqno}
\newcommand*{\reqnomode}{\tagsleft@false\let\veqno=\@@eqno}
% based on my guess on amsmath.sty
\newcommand*{\fleqnon}{\setboolean{@fleqn}{true}}
\newcommand*{\fleqnoff}{\setboolean{@fleqn}{false}}
\makeatother

\begin{document}

\fleqnon

\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= dx^2 + ex + f
\end{align}

\fleqnoff

\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= dx^2 + ex + f \tag{e}
\end{align}

\fleqnon
\leqnomode

\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= dx^2 + ex + f \tag{a}
\end{align}

\fleqnoff
\begin{align}
f(x) &= ax^2 + bx + c \\
g(x) &= dx^2 + ex + f
\end{align}

\begin{equation}
a^2+b^2=c^2.
\end{equation}

\reqnomode

\begin{equation}
-\Delta\phi=4\pi k\rho.\tag{2}
\end{equation}

\end{document} • Assuming that there are reasons for doing this, why don't you simply load amsmath with the fleqn option, given that it works? Oct 28, 2019 at 12:26
• the package only defines flush left or centred layouts when the package is loaded, so you can not switch mid document without copying chunks of the package code for the other case, there is no simple switch. Oct 28, 2019 at 12:37
• @egreg Hmm, from my side there are several reasons for this question. Firstly, I am not sure whether the version based on my guess is correct. Secondly, there are switches for leqno and reqno, so it may be helpful to have one for fleqn. Oct 28, 2019 at 12:38
• @Eli4ph there are not really supported switches for leqno either. I think the answer that you link to is misleading, they work sometimes but not in general. I may fix that later.... Oct 28, 2019 at 12:50
• Not exactly switches, but nccmath defines fleqn and ceqn environments, which work somewhat like subequations. Oct 28, 2019 at 13:29

The comments in amsmath.dtx say:

%    The code for calculating the appropriate placement of equation
%    tags in the \env{align} environments is quite complicated and
%    varies wildly depending on the settings of the |tagsleft@| and
%    |@fleqn| switches.  To minimize memory and hash space usage, we
%    only define the variant appropriate for the current setting of
%    those switches.
%


this means that of the four possible combinations of fleqn and leqno options being used or not used, definitions are made for only one combination, chosen at package loading time. The other three possibilities are skipped over and the TeX definitions for that combination are not made.

So if you change either of these switches mid document without copying code from the package sources and implementing the appropriate definitions, then the layout will basically be arbitrary and typically wrong, as you show.

• Is there any way to center individual display math, at least unnumbered, while fleqn option was given? Is there something like center environment for math? Jul 4 at 12:23
• @Alexey $$x=1$$ Jul 4 at 13:14
• Thanks, forgot about it. Is there a way that would work for gather* and align*? Jul 4 at 14:32
• @Alexey no, as I explain in this answer Jul 4 at 14:35