TransWikia.com

Package amsmath error: begin{cases*} allowed only in math mode

TeX - LaTeX Asked by PandaNinja on July 6, 2021

I am trying to create a systems of equation and I have the following:

documentclass{article}
usepackage[utf8]{inputenc}
usepackage{amsmath}
usepackage{accents}
usepackage[T1]{fontenc}
%Had to add the package above to enable ">" from appearing instead of upside down "?"
%Can also be written as "backslash greater than or less than depending on expression positioning"
usepackage{mathtools}
%Allows me to format my systems of equations properly

Here, $L(lambda) = Pi_{1}^{n} (lambda^{x_{i}}e^{-lambda}/x_{i}!)$
 
$ell(lambda) = -nlambda + sum_{1}^{n} x_{i}Loglambda$

$ell ' (lambda) = -n + (1/lambda) sum_{1}^{n}x_{i}$

$ell ' (lambda)$ = 
begin{cases*}
    > 0, & for $lambda  < overline{X}_{n}$
    = 0,  & for $lambda  = overline{X}_{n}$
    < 0, & for $lambda  > overline{X}_{n}$
end{cases*}

If anyone can point out my error. I apologize if this is dumb question. I am new to LaTeX.

One Answer

Always nice to hear that someone's looking to learn LaTeX to produce beautiful lecture notes! To help get you started, here's a somewhat cleaned-up version of your snippet above, with some comments and tips below:

documentclass{article}
usepackage[utf8]{inputenc}
usepackage{amsmath}
usepackage{accents}
usepackage[T1]{fontenc}
%Had to add the package above to enable ">" from appearing instead of upside down "?"
%Can also be written as "backslash greater than or less than depending on expression positioning"
usepackage{mathtools} % Allows me to format my systems of equations properly
begin{document} 
    Here,
        begin{equation*}
            L(lambda) = prod_{i=1}^{n} frac{ lambda^{x_{i}} e^{-lambda} }{ x_{i}! }
        end{equation*}
    Taking logs,
        begin{equation*}
            ell(lambda) = -nlambda + sum_{i=1}^{n} x_{i} loglambda
        end{equation*}
    The derivative w.,r.,t. $lambda$ is given by
        begin{equation*}
            ell' (lambda) = -n + frac{1}{lambda} sum_{i=1}^{n}x_{i}
        end{equation*}
    We can thus see that
        begin{equation*}
            ell' (lambda) 
            begin{cases*}
                > 0, & for $lambda  < overline{X}_{n}$
                = 0,  & for $lambda  = overline{X}_{n}$
                < 0, & for $lambda  > overline{X}_{n}$
            end{cases*}
        end{equation*}
end{document}

enter image description here

Now, as promised, some notes and tips.

  • First, a few words about modes: although the truth is a bit more complex, TeX (and thus LaTeX) basically has two fundamentally different modes, text mode and math mode. Math mode further comes in two flavors, one for inline mathematics and one for "display" math, i.e. for equations etc. that are separate from the running text. Many things will only work in one mode but not the other.

    Inline math is placed between single dollar signs ($ ... $), as in e.g. Euler's famous formula, $e^{ipi} + 1 = 0$, is widely considered to be the most beautiful formula in mathematics. Display math is introduced using [ ... ] in LaTeX (the use of double dollar signs, $$ ... $$, reaches down to LaTeX's underlying TeX core and should be be avoided; see enter link description here), or using a named environment such as equation (numbered), equation* (from amsmath, if I recall correctly; unnumbered), align, `align*, etc.

    To temporarily switch from math mode to text mode, use text{...}. Some math-mode environments, like cases*, also do this automatically.

  • Speaking of environments: these are ubiquitious in LaTeX, and always take the form begin{<name>} ... end{<name>}. In fact, your entire document (after the preamble) is an example: it should be enclosed in a begin{document} ... end{document}. (And LaTeX will ignore anything and everything that comes after end{document}, BTW.)

    As you can see, in the above code, I used begin{equation*} ... end{equation*} to produce equations that are separate from the running text (a personal choice, but I think it works better here), and $lambda$ to have lambda appear (in math mode!)

  • For products in LaTeX, you should use the symbol prod, which automatically places limits above/below the product sign in display math mode (and next to them in inline math mode), the same way that sum does for sums. Don't use Pi there, just like you wouldn't use Sigma for sums.

  • Fractions are created using frac{num}{dec}; I've used these for the likelihood function and the derivative of the log-likelihood in your example. Again, this is a matter of taste. (Fractions, like sums and products, will automatically appear somewhat smaller in inline vs. display math mode; to explicitly use either version, use tfrac and dfrac.)

  • "Named" functions like log should be typeset using an upright font; in fact LaTeX predefines quite a few, including log, exp sin, cos, min, max, arg, det etc. You'll also find arg, lim, sup, inf etc. useful.

    In fact, when you simply string letters together in math mode, LaTeX will typeset them in a manner appropriate for a product; so it's fine writing, say, the volume of a cuboid is given by $abc$, but if you have a variable with a name consisting of several letters, it's better to use mathit{...}, e.g. frac{ dmathit{hours} }{ dmathit{lwage} } or so.

    "it" stands for "italic" there, BTW, and this is a math mode-specific command; the text mode equivalent is textit. You may find commands such as mathbf and textbf (bold-face) useful as well.

  • A minor note re: beautiful typesetting: there's many small things that will make a document more beautiful and more readable, and while I remain blissfully unaware of most of them, one little trick I have picked up is the use of a small horizontal space for abbreviations, such as "w.,r.,t." in the above example. , is such a space: : and ; are slightly larger. (The mnemonic is that a comma is smaller than a colon is smaller than a semicolon.)

That's all I have for you right now; I hope this'll help get you started. I second the suggestion made in the comments to read up on LaTeX and how to use it, but I also believe that the best thing you can do to learn it is to jump in, start writing, and find out how to solve the challenges you'll no doubt encounter (Google is your friend there, as is this site; and of course LaTeX comes with extensive documentation that you can access using the texdoc command).

Correct answer by chsk on July 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP