[Tex/LaTex] Write this equation in latex

equationsmath-mode

I want to write this equation in latex:

enter image description here

I need that comments be in right side.

I did something like this but it has some flaws.

   \begin{eqnarray}
\label{cons1}
  % \begin{aligned}
x_i+w_{mi}\le X_j   \hfill \hfill\text{if i is to the left of j}\\
\label{cons2}
x_i-w_{mj}\ge X_j   \hfill \hfill\text{if i is to the right of j}\\
\label{cons3}
y_i+h_{mi}\le y_j   \hfill \hfill\text{if i is below of j}\\
\label{cons4}
y_i-h_{mj}\ge y_j    \hfill \hfill\text{if i is above of j}
%\end{aligned}
\end{eqnarray}

Where did I make mistake?

Best Answer

You would probably want to use an align environment

\documentclass{article}

\usepackage{amsmath}

\begin{document}
\begin{align}
\label{cons1}
x_i+w_{mi}&\le X_j   &&\text{if $i$ is to the left of $j$}\\
\label{cons2}
x_i-w_{mj}&\ge X_j   &&\text{if $i$ is to the right of $j$}\\
\label{cons3}
y_i+h_{mi}&\le y_j   &&\text{if $i$ is below of $j$}\\
\label{cons4}
y_i-h_{mj}&\ge y_j   &&\text{if $i$ is above of $j$}
\end{align}
\end{document} 

enter image description here

You shouldn't use eqnarray in LaTeX, see eqnarray vs align