4

enter image description here

\begin{align*}
 \mathbf{J}_{f}
 = \begin{bmatrix}
   -2 x \sin\left(y\right) ^{2} & -2x^{2}\sin\left(y\right) \cos\left(y\right) &  0
   \\
   \sin\left(y\right)  & x \cos\left(y\right)  & 0
   \\
   2x & 0 & 0
 \end{bmatrix} 
.\end{align*}

How do I right align the matrix, so that for example in the first column the sin(y) aligns with -2 and not in the middle.

1
  • Welcome! Do you mean “left align”?
    – egreg
    Mar 18 at 11:01

4 Answers 4

6

You can use mathtools provided bmatrix*

\documentclass{article}
\usepackage{mathtools} % also loads amsmath

\begin{document}

You can get left alignment
\begin{equation*}
\mathbf{J}_{f} =
\begin{bmatrix*}[l]
  -2 x \sin(y) ^{2} & -2x^{2}\sin(y) \cos(y) &  0
  \\
  \sin(y)  & x \cos(y)  & 0
  \\
  2x & 0 & 0
\end{bmatrix*}.
\end{equation*}
or, alternatively, right alignment
\begin{equation*}
\mathbf{J}_{f} =
\begin{bmatrix*}[r]
  -2 x \sin(y) ^{2} & -2x^{2}\sin(y) \cos(y) &  0
  \\
  \sin(y)  & x \cos(y)  & 0
  \\
  2x & 0 & 0
\end{bmatrix*}.
\end{equation*}
but I find neither to be prettier than with the standard
center alignment
\begin{equation*}
\mathbf{J}_{f} =
\begin{bmatrix}
  -2 x \sin(y) ^{2} & -2x^{2}\sin(y) \cos(y) &  0
  \\
  \sin(y)  & x \cos(y)  & 0
  \\
  2x & 0 & 0
\end{bmatrix}.
\end{equation*}

\end{document}

enter image description here

A few unrelated comments. Avoid \left and \right in that context: they only add undesired spacing. Also align should be used for several equations to be aligned.

1
  • 1
    Thanks for the quick answer!
    – Richard
    Mar 18 at 11:30
3

Right alignment can be obtained using array environment (even without amsmath).

enter image description here

\documentclass{article}
\begin{document}
Right alignment can be obtained using \verb|array| environment.
\begin{equation}
\mathbf{J}_{f} =\left[
\begin{array}{rrr}
-2 x \sin(y) ^{2}&-2x^{2}\sin(y) \cos(y) &  0   \\
\sin(y)  & x \cos(y)  & 0   \\
2x & 0 & 0
\end{array}\right].
\end{equation}
\end{document}
4
  • 1
    If you the combination with \left[ and \begin{array} then there are some white space occurs between square brackets and array, so \begin{bmatrix} is the correct one...
    – MadyYuvi
    Mar 18 at 13:11
  • @MadyYuvi Oh, I see that now. I have heard that array environment can be highly customized. Maybe some negative space \! should be used ???
    – Black Mild
    Mar 18 at 15:41
  • Hopefully the correct format is (if you are using array) \left[\begin{array}{@{}...@{}}
    – MadyYuvi
    Mar 18 at 16:02
  • 1
    MadyYuvi : I did not know this difference (some white space) between the two commands: (1) \begin{bmatrix} and (2) the combo of \left[ and \begin{array} until I read your reply. Thanks!
    – Dr. Sundar
    Mar 18 at 16:38
1

Also the old package spalign package provides to have right/center/left align matrix. In fact you can see that is possible to obtain the same ouptut of the other users.

\documentclass[a4paper,12pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{spalign}
\usepackage{parskip}


\begin{document}
Center option
\[\mathbf{J}_{f}=\spaligndelims{[}{]}
\spalignmat[c]{-2x\sin(y)^{2} -2x^{2}\sin(y)\cos(y)  0; \sin(y) x\cos(y) 0; 2x 0 0} 
.\]
Left option 
\[\mathbf{J}_{f}=\spaligndelims{[}{]}
\spalignmat[l]{-2x\sin(y)^{2} -2x^{2}\sin(y)\cos(y)  0; \sin(y) x\cos(y) 0; 2x 0 0} 
.\]
Right option
\[\mathbf{J}_{f}=\spaligndelims{[}{]}
\spalignmat[r]{-2x\sin(y)^{2} -2x^{2}\sin(y)\cos(y)  0; \sin(y) x\cos(y) 0; 2x 0 0} 
.\]
\end{document}

enter image description here

2
  • 1
    First time, I came to know about spalign.sty...thanks, and hope all is well....
    – MadyYuvi
    Mar 19 at 6:23
  • 1
    @MadyYuvi Dear friend, from time to time I use old packages that are forgotten but sometimes they reduce LaTeX code by a lot. I always wish you and your family well.
    – Sebastiano
    Mar 19 at 8:50
1

The environment {bNiceMatrix} of nicematrix has (among other features) keys r and l for the horizontal alignment.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

You can get left alignment
\begin{equation*}
\mathbf{J}_{f} =
\begin{bNiceMatrix}[l]
  -2 x \sin(y) ^{2} & -2x^{2}\sin(y) \cos(y) &  0
  \\
  \sin(y)  & x \cos(y)  & 0
  \\
  2x & 0 & 0
\end{bNiceMatrix}.
\end{equation*}
or, alternatively, right alignment
\begin{equation*}
\mathbf{J}_{f} =
\begin{bNiceMatrix}[r]
  -2 x \sin(y) ^{2} & -2x^{2}\sin(y) \cos(y) &  0
  \\
  \sin(y)  & x \cos(y)  & 0
  \\
  2x & 0 & 0
\end{bNiceMatrix}.
\end{equation*}

\end{document}

Output of the above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.