5

In this answer I found a very useful approach to easily center certain elements in a alignat environment using a new command:

\newcommand{\ccol}[1]{\omit\hfill $#1$\hfill}

I inserted the $’s because \omit seems to terminate math mode.

\begin{alignat}{3}
x_{n}-\frac{f\left(x_{n}\right)}{f^{\prime}\left(x_{n}\right)}-x^{\ast}=\mbox{} & x_{n+1}-x^{\ast} & \mbox{}=\mbox{} & \left(x_{n}-x^{\ast}\right)^{2} & \mbox{}\cdot\frac{f^{\prime\prime}\left(\xi\right)}{2\cdot f^{\prime}\left(x_{n}\right)}\\
& \ccol{\epsilon_{n+1}} & \mbox{}=\mbox{} & \ccol{\epsilon_{n}^{2}} & \mbox{}\cdot\frac{f^{\prime\prime}\left(\xi\right)}{2\cdot f^{\prime}\left(x_{n}\right)}
\end{alignat}

result

Unfortunately, the numbering of the second equation is gone. Even using another command to manually activate numbering for certain equations in alignat* results in the above:

\newcommand\numberthis{\addtocounter{equation}{1}\tag{\theequation}}

\begin{alignat*}{3}
x_{n}-\frac{f\left(x_{n}\right)}{f^{\prime}\left(x_{n}\right)}-x^{\ast}=\mbox{} & x_{n+1}-x^{\ast} & \mbox{}=\mbox{} & \left(x_{n}-x^{\ast}\right)^{2} & \mbox{}\cdot\frac{f^{\prime\prime}\left(\xi\right)}{2\cdot f^{\prime}\left(x_{n}\right)}\numberthis\\
 & \ccol{\epsilon_{n+1}} & \mbox{}=\mbox{} & \ccol{\epsilon_{n}^{2}} & \mbox{}\cdot\frac{f^{\prime\prime}\left(\xi\right)}{2\cdot f^{\prime}\left(x_{n}\right)}\numberthis
\end{alignat*}

How to not omit numbering?

ominug
  • 337
  • 1
  • 2
  • 9

2 Answers2

5

Doing \omit breaks the typesetting of the equation number, as you discovered.

I suggest my macro \Cen from https://tex.stackexchange.com/a/209732/4427

\documentclass{article}
\usepackage{amsmath}

\makeatletter
\newcommand{\Cen}[2]{%
  \ifmeasuring@
    #2%
  \else
    \makebox[\ifcase\expandafter #1\maxcolumn@widths\fi]{$\displaystyle#2$}%
  \fi
}
\makeatother

\begin{document}

\begin{alignat}{3}
x_{n}-\frac{f(x_{n})}{f'(x_{n})}-x^* = {} & x_{n+1}-x^* & {}={} & (x_{n}-x^*)^{2} &
  {}\cdot\frac{f''(\xi)}{2\cdot f'(x_{n})}\\
& \Cen{2}{\epsilon_{n+1}} & {}={} & \Cen{4}{\epsilon_{n}^{2}} &
  {}\cdot\frac{f''(\xi)}{2\cdot f'(x_{n})}
\end{alignat}

\end{document}

You just have to tell what column you're in.

Note that I simplified the input removing all the unnecessary \left and \right and also using ' instead of ^{\prime}.

enter image description here

egreg
  • 1,121,712
  • Thanks and sorry for the code formatting, I used LyX to generate it. – ominug Sep 14 '15 at 21:10
  • @ominug What worries me is the wealth of useless (and harming) \left and \right. Developers of LyX should read this site and the uncountable times we say “don't abuse \left and \right”. – egreg Sep 14 '15 at 21:21
  • @ominug Are you sure LyX put in \left? If you put in (, I don't believe LyX will change it. If you add "smart" parentheses (e.g. through delimiters dialog) then yes the left is added. – scottkosty Sep 15 '15 at 02:33
  • Yes, because it’s just a key combination of “Alt + M, (” in LyX and then missing parentheses are no problem anymore. But you are able to insert single (’s also. – ominug Sep 18 '15 at 16:03
1

An alternative way that doesn't interfere with the numbering is to set the contents you want to centre in a box of pre-specified width, or calculate the width using \widthof (from calc).

Below I've used mathtools (which loads amsmath and calc) to make a math box using \mathmakebox of width equal to \widthof{...} of the element above it in the construction:

enter image description here

\documentclass{article}
\usepackage{mathtools}
\begin{document}

\begin{alignat}{3}
  x_n - \frac{f\bigl(x_n\bigr)}{f'\bigl(x_n\bigr)} - x^\ast 
    &= x_{n+1} - x^\ast 
    &= \bigl(x_n - x^\ast\bigr)^2
      &\cdot \frac{f''\bigl(\xi\bigr)}{2 \cdot f'\bigl(x_n\bigr)} \\
  &\phantom{{}={}} \mathmakebox[\widthof{$x_{n+1} - x^\ast$}]{\epsilon_{n+1}} 
    &= \mathmakebox[\widthof{$\bigl(x_n - x^\ast\bigr)^2$}]{\epsilon_{n}^{2}}
      &\cdot \frac{f''\bigl(\xi\bigr)}{2 \cdot f'\bigl(x_n\bigr)}
\end{alignat}

\end{document}
Werner
  • 603,163