Math & ScienceHaving trouble getting this equation set up correctly

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
MathPaula
Posts: 2
Joined: Sat May 14, 2011 4:04 pm

Having trouble getting this equation set up correctly

Post by MathPaula »

I've added and deleted dollar signs until my eyes are blurry. Can anyone see what's wrong with this set up?

Code: Select all

$d_{H}(X,Y)=\left\{$ \begin{array}{ll} $ \left|x_{1}-y_{1}\right|$,
 & $\mbox{if $x_{2}=y_{2}$} $\\ $\left|x_{1}\right|+\left|x_{2}-y_{2}\right|
 +\left|y_{1}\right|$,
 & $\mbox{if $x_{2} \neq y_{2}$}$ \\ $\left|x_{2}-y_{2}\right|$,
 & $\mbox{if $x_{2}=y_{2}=0$}$ \end{array} $\right\}$.
It's supposed to be a piecewise function with three parts. I'm pretty sure I have too many $ in there, but TXC keeps giving me a missing $ error and I can't figure out where it is and am afraid I've added too many now, because I'm now getting a fatal error upon building. It was working and formatting and building just fine an hour ago. :(

Thanks in advance,

PGY

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Having trouble getting this equation set up correctly

Post by Stefan Kottwitz »

Hi PGY,

welcome to the board!

Here are some tips:
  • the array environment works in math mode, no additional $ required
  • End \mbox when the text ends, don't nest math and \mbox if it's not necessary.
  • Even better, use \text instead of \mbox, loading the amsmath package is required.
  • Use line breaks and indentation in the editor to make your code more readable.
  • If there's only one character or number in a subscript or superscript, you don't need braces. Omitting them can make the code easier to read, though in general I don't recommend to omit braces whenever possible - they can help.

Code: Select all

$d_{H}(X,Y)=\left\{
  \begin{array}{ll}
    \left|x_1-y_1\right|, & \text{if } x_2=y_2 \\
    \left|x_1\right| + \left|x_2-y_2\right| + \left|y_1\right|,
      & \text{if } x_2 \neq y_2 \\
    \left|x_2-y_2\right|, & \text{if } x_2=y_2=0
  \end{array} \right\}.$
equation
equation
equation.png (3.77 KiB) Viewed 1606 times
Btw. our forum supports code markup. You can use the Code button above the edit field when writing or editing. I just did this for you.

Stefan
LaTeX.org admin
MathPaula
Posts: 2
Joined: Sat May 14, 2011 4:04 pm

Re: Having trouble getting this equation set up correctly

Post by MathPaula »

Thanks a million!
Post Reply