6

I tried to created 2 eqnarrays but the equations in those 2 eqnarrays don't line up together. Here is the code:

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{txfonts}
\begin{document}
\begin{eqnarray}
\label{eq1}
&& diag\frac{\partial P}{\partial \theta}= 
diag(-diag(V)[Gdiag(V)\sin(Abus)^{T}-Bdiag(V)cos(Abus)^{T}]
\nonumber\\
&& -diag(V).^{2}diag(diag(B))))  \\
\label{eq2}
&& nondiag\frac{\partial P}{\partial \theta}= 
VV^{T}.* G .* \sin(Abus) -VV^{T}.*B .* \cos(Abus)   
\end{eqnarray}
Replace the diagnol elements of the (\ref{eq2}) with the elements of the                
(\ref{eq1}), we will get the complete $\partial P/\partial \theta$
\begin{eqnarray}
\label{eq3}
&& diag\frac{\partial P}{\partial V}=
diag(Gdiag(V)\cos(Abus)^{T}+Bdiag(V)sin(Abus)^{T}
\nonumber\\
&& +diag(V)diag(diag(G))))  \\
\label{eq4}
&& nondiag\frac{\partial P}{\partial V}= 
diag(V)G .* \cos(Abus) + diag(V)B .* \sin(Abus)   
\end{eqnarray}
Replace the diagnol elements of the (\ref{eq4}) with the elements of the    
(\ref{eq3}), we will get the complete $\partial P/\partial V$
\end{document}

And the result is: We can see that the start positions of equation (1),(2) and equation (3),(4) are different. How can I solve this problem? Thank you.

2
  • What does the operator .* do? Is it even an operator? Similarly, what does .^{2} do?
    – Mico
    Apr 28, 2015 at 7:59
  • .* means it's not matrix multiplication, but element multiplication. For two 2*2 matrix A and B, the result C would be a 2*2 matrix with C(1,1)=a11*b11,C(1,2)=a12*b12,C(2,1)=a21*b21,C(2,2)=a22*b22 Apr 28, 2015 at 11:35

3 Answers 3

6

Replacing eqnarray by align the result is not perfectly aligned, but I looks like it is and I think nicer than with eqnarray.

Following Ian's suggestion diag and nondiag are declared math operators. Also parenthesis sizes has been adjusted.

After Mico's comment a new command dotstar has been declared. It's not clear for me what it does.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{txfonts}

\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\nondiag}{nondiag}
\newcommand\dotstar{\mathbin{.*}}

\begin{document}
\begin{align}
\label{eq1}
\diag\frac{\partial P}{\partial \theta} & = 
\diag\left(-\diag(V)\left[Gdiag(V)\sin(Abus)^{T}-B\diag(V)cos(Abus)^{T}\right]\right.
\nonumber\\
&\quad \left.-\diag(V).^{2}\diag\left(\diag(B)\right)\right)  \\
\label{eq2}
\nondiag\frac{\partial P}{\partial \theta} & = 
VV^{T}\dotstar G\dotstar\sin(Abus)-VV^{T}\dotstar B\dotstar\cos(Abus)   
\end{align}
Replace the diagnol elements of the (\ref{eq2}) with the elements of the                
(\ref{eq1}), we will get the complete $\partial P/\partial \theta$
\begin{align}
\label{eq3}
\diag\frac{\partial P}{\partial V} & =
\diag\left(G\diag(V)\cos(Abus)^{T}+B\diag(V)sin(Abus)^{T}\right.
\nonumber\\
&\quad +\Bigl.\diag(V)\diag\left(\diag(G)\right)\Bigr)  \\
\label{eq4}
\nondiag\frac{\partial P}{\partial V}&= 
\diag(V)G\dotstar\cos(Abus) + \diag(V)B\dotstar\sin(Abus)   
\end{align}
Replace the diagnol elements of the (\ref{eq4}) with the elements of the    
(\ref{eq3}), we will get the complete $\partial P/\partial V$
\end{document}

enter image description here

But if you want all equations perfectly aligned, insert the middle paragraph inside a \intertext command.

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{txfonts}

\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\nondiag}{nondiag}
\newcommand\dotstar{\mathbin{.*}}

\begin{document}
\begin{align}
\label{eq1}
\diag\frac{\partial P}{\partial \theta} & = 
\diag\left(-\diag(V)\left[Gdiag(V)\sin(Abus)^{T}-B\diag(V)cos(Abus)^{T}\right]\right.
\nonumber\\
&\quad \left.-\diag(V).^{2}\diag\left(\diag(B)\right)\right)  \\
\label{eq2}
\nondiag\frac{\partial P}{\partial \theta} & = 
VV^{T}\dotstar G\dotstar\sin(Abus)-VV^{T}\dotstar B\dotstar\cos(Abus)   
%\end{align}
\intertext{Replace the diagnol elements of the (\ref{eq2}) with the elements of the                
(\ref{eq1}), we will get the complete $\partial P/\partial \theta$}
%\begin{align}
\label{eq3}
\diag\frac{\partial P}{\partial V} & =
\diag\left(G\diag(V)\cos(Abus)^{T}+B\diag(V)sin(Abus)^{T}\right.
\nonumber\\
&\quad +\Bigl.\diag(V)\diag\left(\diag(G)\right)\Bigr)  \\
\label{eq4}
\nondiag\frac{\partial P}{\partial V}&= 
\diag(V)G\dotstar\cos(Abus) + \diag(V)B\dotstar\sin(Abus)   
\end{align}
Replace the diagnol elements of the (\ref{eq4}) with the elements of the    
(\ref{eq3}), we will get the complete $\partial P/\partial V$
\end{document}

enter image description here

10
  • 1
    I think you should also use \DeclareMathOperator for diag and nondiag. Apr 28, 2015 at 7:09
  • 1
    Also, the typeset look of .* needs a bit of refinining.
    – Mico
    Apr 28, 2015 at 7:13
  • @IanThompson I've followed your suggestion. Thank you.
    – Ignasi
    Apr 28, 2015 at 7:35
  • @Mico I don't know what .* means. Which refinement do you suggest?
    – Ignasi
    Apr 28, 2015 at 7:36
  • I'm not sure either what .* does, but I assume it's some kind of operator of type mathbin. Thus, I'd define the macro \dotstar iia \newcommand\dotstar{\mathbin{.*}} in the preamble and write \dotstar in the equations themselves. Probably not perfect, but almost certainly better than what's produced by writing .* directly.
    – Mico
    Apr 28, 2015 at 7:48
2

Never use eqnarray (see eqnarray vs align)

Here the combination of align with split seems the best way to tackle the problem.

\documentclass{article}
\usepackage{mathtools}
\usepackage{newtxtext,newtxmath}

\DeclareMathOperator{\diag}{diag}
\DeclareMathOperator{\nondiag}{nondiag}
\newcommand{\dotstar}{\mathbin{.{*}}}
\newcommand{\Abus}{\mathit{Abus}}

\begin{document}

\begin{align}
\begin{split}
\diag\frac{\partial P}{\partial \theta} & = 
\diag\bigl(-\diag(V)[G \diag(V)\sin(\Abus)^{T}-B\diag(V)\cos(\Abus)^{T}]\\
&\qquad -\diag(V)^{2}\diag(\diag(B))\bigr)
\end{split}
\label{eq1} \\
\nondiag\frac{\partial P}{\partial \theta} & = 
VV^{T}\dotstar G\dotstar \sin(\Abus)-VV^{T}\dotstar B\cos(\Abus)   
\label{eq2}\\
\intertext{%
  By replacing the diagonal elements of the (\ref{eq2}) with the elements of
  equation~\eqref{eq1}, we will get the complete $\partial P/\partial \theta$}
\begin{split}
\diag\frac{\partial P}{\partial V} & =
  \diag\bigl(G\diag(V)\cos(\Abus)^{T}+B\diag(V)\sin(\Abus)^{T} \\
&\qquad +\diag(V)\diag(\diag(G))\bigr)
\end{split}
\label{eq3}\\
\nondiag\frac{\partial P}{\partial V}&= 
\diag(V)G\dotstar \cos(\Abus) + \diag(V)B\dotstar \sin(\Abus)   
\label{eq4}
\end{align}
By replacing the diagonal elements of equation~\eqref{eq4} with the elements of
equation~\eqref{eq3}, we will get the complete $\partial P/\partial V$

\end{document}

I replaced txfonts with newtxtext and newtxmath (the fonts for math are better and the packages are actively maintained).

If you add the tbtags option to amsmath the equation number will be aligned with the bottom line of the split parts.

I removed all \left and \right that served no purpose whatsoever (they just added unwanted horizontal space). Just a couple of \big pairs are needed.

enter image description here

2
  • how come there's not a \big on the outer parens in the first equation? (those parens are outside \big pair of brackets, and the outer parens are \big in equation (3).) Apr 28, 2015 at 12:36
  • @barbarabeeton Because I forgot to add them. :-)
    – egreg
    Apr 28, 2015 at 12:49
0

Using intertext in one align environment gets the alignment:

\documentclass{article}
\usepackage[fleqn]{amsmath}
\usepackage{txfonts}
\begin{document}
\begin{align}
\label{eq1}
& diag\frac{\partial P}{\partial \theta}= 
diag(-diag(V)[Gdiag(V)\sin(Abus)^{T}-Bdiag(V)cos(Abus)^{T}]
\nonumber\\
& -diag(V).^{2}diag(diag(B))))  \\
\label{eq2}
& nondiag\frac{\partial P}{\partial \theta}= 
VV^{T}.* G .* \sin(Abus) -VV^{T}.*B .* \cos(Abus)   
\intertext{Replace the diagnol elements of the (\ref{eq2}) with the     elements of the                
(\ref{eq1}), we will get the complete $\partial P/\partial \theta$}
\label{eq3}
&diag\frac{\partial P}{\partial V}=
diag(Gdiag(V)\cos(Abus)^{T}+Bdiag(V)sin(Abus)^{T}
   \nonumber\\
& +diag(V)diag(diag(G))))  \\
\label{eq4}
  & nondiag\frac{\partial P}{\partial V}= 
   diag(V)G .* \cos(Abus) + diag(V)B .* \sin(Abus)   
 \end{align}
    Replace the diagnol elements of the (\ref{eq4}) with the elements        of    the    
(\ref{eq3}), we will get the complete $\partial P/\partial V$
\end{document}

You must log in to answer this question.

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