1

I am using Overleaf to write a piecework function, and I know that in MathJax the function works, I write \begin{cases} \end{cases} and then separate the intervals with \\, but in Overleaf it gives me the curly bracket only for the first row, anything that is after \\ is not in these brackets, although I put all the expression in \begin \end function. Can anybody help? Also for additional info, maybe anyone knows an online platform, that works on MathJax, not on LaTeX? This is my code:

$ F(x)= 
\begin{cases} 
0, & x\leq 0 \textbf{ or } y\leq 0 \\ 
\frac{x^2y+xy^3}{2}, & \textbf{ if} 0\leq x,y \leq 1 \\ 
\frac{x^2+x}{2}, & \textbf{ kad } 0\leq x \leq 1, \; y \geq 1 \\ 
\frac{y+y^3}{2}, & \textbf{ if} x>1, 0\leq y \leq 1 \\ 
    1,& \textbf{ if } y \geq 1, x \geq 1 
    \end{cases}$
  • 7
    Welcome to TeX.SX! First off, MathJax is not LaTeX, so things may (and they do) differ. Second, please show us a minimal example that reproduces the problem. WIthout seeing the code we can only guess – Phelype Oleinik Jan 3 at 23:01
  • 4
    Can you elaborate on what you mean by "I put all the expression in \begin \end function."? – Werner Jan 3 at 23:08
  • I am unable to replicate your issue on Overleaf as well as my local system. You must be doing something different. Can you supply a link to your project on Overleaf? – Werner Jan 4 at 1:05
8

Never ignore errors, you have

\documentclass{article}

\begin{document}

$ F(x)= 
\begin{cases} 
0, & x\leq 0 \textbf{ or } y\leq 0 \\ 
\frac{x^2y+xy^3}{2}, & \textbf{ if} 0\leq x,y \leq 1 \\ 
\frac{x^2+x}{2}, & \textbf{ kad } 0\leq x \leq 1, \; y \geq 1 \\ 
\frac{y+y^3}{2}, & \textbf{ if} x>1, 0\leq y \leq 1 \\ 
    1,& \textbf{ if } y \geq 1, x \geq 1 
    \end{cases}$
\end{document}

which produces

! Misplaced alignment tab character &.
l.7 0, &
         x\leq 0 \textbf{ or } y\leq 0 \\
? 

You need to load the amsmath package.

\documentclass{article}
\usepackage{amsmath}
\begin{document}

$ F(x)= 
\begin{cases} 
0, & x\leq 0 \textbf{ or } y\leq 0 \\ 
\frac{x^2y+xy^3}{2}, & \textbf{ if} 0\leq x,y \leq 1 \\ 
\frac{x^2+x}{2}, & \textbf{ kad } 0\leq x \leq 1, \; y \geq 1 \\ 
\frac{y+y^3}{2}, & \textbf{ if} x>1, 0\leq y \leq 1 \\ 
    1,& \textbf{ if } y \geq 1, x \geq 1 
    \end{cases}$
\end{document}

tex never makes any attempt to make sensible typeset output after an error, the output is possibly useful for debugging but it not intended to be a usable display.

|improve this answer|||||
2

(I am assuming you are going to use LaTeX)

The cases environment from amsmath uses & for the "left and right" parts of the definition (left being the function and right being the condition, usually) and \\ to go to a new line.

For example the following code

\documentclass{article}

\usepackage{amsmath}

\begin{document}

\[
u(x) = 
\begin{cases} 
 \exp{x} & \text{if } x \geq 0 \\
 1       & \text{if } x < 0
\end{cases}
\]

\end{document}

yields

cases

Note the \\ to end the line. You can of course have multiple lines.

|improve this answer|||||
  • I added my code, and it looks like yours, but Overleaf still presents it incorrectly. Any advice? – user Jan 4 at 0:43
  • 3
    @user see my answer, your examples should always be complete documents that show the problem, and also if you get an error, show the error not the typeset output. – David Carlisle Jan 4 at 1:23
1

Elaborating a little on David Carlisle's answer, LaTeX has a \cases macro, not an environment, which can be used as follows:

Sample output

\documentclass{article}

\begin{document}

\begin{displaymath}
  f(x) =
  \cases{
  0&for $x \leq 0$,\cr
  e^{-1/x}&for $x > 0$.
  }
\end{displaymath}

\end{document}

The syntax is not LaTeX like, e.g. line changes with \cr not \\, since the macro is essentially inherited from plain TeX. Also the brace is too small (but can be improved by writing \mathstrut before each line) and the spacing is rather tight. Because of LaTeX's internals, if you write \begin{cases}...\end{cases} then the \cases macro is invoked, but its argument is not interpreted correctly. This makes the error message received somewhat opaque, i.e. there is no complaint about an undefined macro/environment but rather the misplaced alignment character error reported by David.

On the other hand, the amsmath package implements a good cases environment, as demonstrated by David's code, but to access it you need to load the package.

|improve this answer|||||

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.