5

I use the fleqn option with the standard mathindent setting. That nicely shifts equations further to the left if they would collide with the right page margin.

With alignat, however, that does not work. As a workaround, I set the mathindent manually with \setlength{\mathindent}{10pt} for the equation, where I needed the space and reset it to what I found to be the default value \setlength{\mathindent}{\dimen102} after that equation. Now for all following equations, that automatic shifting does not work anymore.

\documentclass[12pt,a4paper,fleqn]{scrreprt}
\usepackage{amsmath}
\usepackage{array}

\newdimen{\oldmathindent}
\setlength{\oldmathindent}{\mathindent}

\begin{document}

Some short equation, which has the default \texttt{mathindent} due to \texttt{fleqn}:
\begin{equation}
E=mc^2.
\end{equation}
Some long equation, which automatically has a smaller \texttt{mathindent} due to its length and a collision with the right margin:
\begin{equation}
v = \left[ \begin{array}{c c c c c c c c c c c c c c c c c c c c c c c c c c c c}
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0
\end{array}\right]^\top.
\end{equation}
Now some long equation with \texttt{alignat}
\begin{alignat}{4}
\nonumber \ddot{x}_c &= -&&\frac{1}{m_c(\Delta m_c)} &&[&& F_s(x_c-x_w,u_1,\Delta m_c) + F_d(x_c-x_w,\dot(x_c)-\dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c)) + F_{Delta m_c}( \Delta m_c ) ]   \\ 
\nonumber \ddot{x}_w &= -&&\frac{1}{m_w} &&[&& F_s(x_c-x_w,u_1) + F_d(x_c-x_w,\dot(x_c)-\dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c))].
\end{alignat}
The same long equation with \emph{alignat} but manual set of \texttt{mathindent} to 0
\setlength{\mathindent}{0pt}
\begin{alignat}{4}
\nonumber \ddot{x}_c &= -&&\frac{1}{m_c(\Delta m_c)} &&[&& F_s(x_c-x_w,u_1,\Delta m_c) + F_d(x_c-x_w,\dot(x_c)-\dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c)) + F_{Delta m_c}( \Delta m_c ) ]   \\ 
\nonumber \ddot{x}_w &= -&&\frac{1}{m_w} &&[&& F_s(x_c-x_w,u_1) + F_d(x_c-x_w,\dot(x_c)-    \dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c))].
\end{alignat}\\
\setlength{\mathindent}{\oldmathindent}
And now - after setting back the \texttt{mathindent} to \texttt{oldmathindent}, the same short equation from above looks like this:
\begin{equation}
v = \left[ \begin{array}{c c c c c c c c c c c c c c c c c c c c c c c c c c c c}
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 1 & 0 & 0
\end{array}\right]^\top.
\end{equation}

\end{document}

Note that the layout of the long alignat equation is how I want it. If anyone has an idea how to achieve that without alignat, that would be fine for me, too.

Thanks!

edit_0528: I applied the changes suggested by barbara beeton (if I got it right). However, all equations after the \setlength{\mathindent}{0pt} start at the very left, disregarding the \setlength{\mathindent}{\oldmathindent}.

Bazi
  • 163

3 Answers3

3

Instead of saving and restore use:

\begingroup
\setlength{\mathindent}{0pt}
\begin{alignat}{4}
\nonumber \ddot{x}_c &= -&&\frac{1}{m_c(\Delta m_c)} &&[&& F_s(x_c-x_w,u_1,\Delta m_c) + F_d(x_c-x_w,\dot(x_c)-\dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c)) + F_{Delta m_c}( \Delta m_c ) ]   \\ 
\nonumber \ddot{x}_w &= -&&\frac{1}{m_w} &&[&& F_s(x_c-x_w,u_1) + F_d(x_c-x_w,\dot(x_c)-    \dot(x_w)) + F_f(\dot(x_c)-\dot(x_w)) + \\ &&&&&&&  +F_{fc}(\dot(x_c))].
\end{alignat}
\endgroup
2

although you've chosen the appropriate \dimen, what the command \setlength{\mathindent}{0pt} does is reset \dimen102. so you no longer have access to the original value.

save the original value of \mathindent thus in the preamble:

\newdimen{\oldmathindent}
\AtBeginDocument{%
  \setlength{\oldmathindent}{\mathindent}%
}

then, instead of \setlength{\mathindent}{\dimen102}, specify

\setlength{\mathindent}{\oldmathindent}

edit: it turns out that \mathindent itself isn't set until after \begin{document}, so trying to set \oldmathindent in the preamble has no useful effect.

  • Unfortunately, that didn't quite work. I added \newdimen{\oldmathindent} and \setlength{\oldmathindent}{\mathindent} to my preamble, changed the mathindent for the long equation with \setlength{\mathindent}{0pt} and afterwards modified the \setlength{\mathindent}{\dimen102} to \setlength{\mathindent}{\oldmathindent}. Did I get you wrong? The result is that all following equations have a mathident of 0pt. – Bazi May 25 '14 at 15:40
  • @Bazi -- i finally had a chance to test this recommendation, and found why it wasn't working. answer is updated. – barbara beeton May 29 '14 at 12:33
1

You make two errors:

  1. You should use \newlength{\oldmathindent} and not \newdimen, because \mathindent is a rubber length.

  2. You should save the value of \mathindent at begin document, not in the preamble, where its value is still –1sp (it's not zero, in order to use it as a signal).

So you should do

\newlength{\oldmathindent}
\AtBeginDocument{\setlength{\oldmathindent}{\mathindent}}

The value set by amsmath at begin document is 29.3747pt minus 29.3747pt and the minus part is essential.

\DeclareOption{fleqn}{%
    \@fleqntrue
    \@mathmargin = -1sp
    \let\mathindent=\@mathmargin
    \AtBeginDocument{%
        \ifdim\@mathmargin= -1sp
            \@mathmargin\leftmargini minus\leftmargini
        \fi
    }%
}

As you see, amsmath uses \@mathmargin, but the fleqn option makes \mathindent equivalent to \@mathmargin, but the value is set later, when the value of \leftmargini is known.

Of course, resetting \mathindent in a group is the best path.

egreg
  • 1,121,712