# How to right align matrix

\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.

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

You can use mathtools provided bmatrix*

\documentclass{article}

\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}


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.

• Thanks for the quick answer! Mar 18 at 11:30

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

\documentclass{article}
\begin{document}
Right alignment can be obtained using \verb|array| environment.
$$\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{document}

• 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... 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 ??? Mar 18 at 15:41
• Hopefully the correct format is (if you are using array) \left[\begin{array}{@{}...@{}} Mar 18 at 16:02
• 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! Mar 18 at 16:38

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}


• First time, I came to know about spalign.sty...thanks, and hope all is well.... Mar 19 at 6:23
• @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. Mar 19 at 8:50

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}