# how to automatically convert \cases to \begin{cases} \end{cases}?

Is there some smart macro I could add in the preamble to handle this LaTeX auto-generated code?

I use Maple's latex() command to convert results to LaTeX that I then compile using texlive in a separate document.

Sometimes it generates the wrong LaTeX or broken LaTeX. Here is an example of one such case:

The above output is send to a latex file and wrapped inside and . I picked equation environment to put the result inside, but I can change this if it helps in conversions.

This is all done using an automated script. I do not use any of Maple style sheets or anything else. I simply just take the latex() output and process that using texlive standard setup.

I have hundreds of such equations, so manually editing any broken Maple LaTeX output is not possible and since each time I re-run the script the edits will be lost.

Is there a way to make a macro that will convert the above to use \begin{cases} with an enclosing \end{cases} using some smart replacement rule to make LaTeX and amsmath happy?

Here is the error, and a MWE:

MWE

\documentclass[11pt,notitlepage]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%smart renewcommand here? or \let?
\begin{document}

$$x- \cases{-2\,\sqrt {-y \left( x \right) }&y \left( x \right) \leq 0\cr 2\,\sqrt {y \left( x \right) }&0<y \left( x \right) \cr}+{\it \_C1}=0_{{1}}$$

\end{document}


and

pdflatex foo.tex

(/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/umsa.fd)
(/usr/local/texlive/2014/texmf-dist/tex/latex/amsfonts/umsb.fd)

! Package amsmath Error: Old form \cases' should be \begin{cases}.

See the amsmath package documentation for explanation.
Type  H <return>  for immediate help.
...

l.9 \cases
{-2\,\sqrt {-y \left( x \right) }&$y \left( x \right) \leq 0$\cr
?

-
The “old form \cases” has nothing to do with amsmath; it is in the LaTeX kernel because it was in Plain TeX. I'm quite surprised that Maple uses that deprecated form, as the cases environment was included in the original amslatex back in 1990. – egreg Jul 14 '14 at 22:40
@egreg thanks. I know little about these things. But I have many problems with latex output from Maple. I am using the latest version 18.01. Either way, was hoping for some automated solution or a way to compile the above in Latex as I have no control of what Maple outputs for its Latex. I thought it was amsmath, since the error says Package amsmath Error: . thanks – Nasser Jul 14 '14 at 22:43
if maple isn't following the "current" standard syntax (which hasn't been updated since at least 1999), they should be told so, and asked to please mend their ways. – barbara beeton Jul 15 '14 at 15:47
@barbarabeeton That request to maple might carry more weight coming from you than from the OP. – Ethan Bolker Jul 15 '14 at 16:54
@EthanBolker -- i'm considering that. the fact that we don't use maple here directly does mean that i'll have to do some research before writing to them though, so it can't happen right away. – barbara beeton Jul 15 '14 at 17:02

The old \cases{...} form is an unabridged copy of the same macro in Plain TeX and is in the LaTeX kernel just because it was in Plain.

The environment form was introduced in amsmath.sty issued, if I remember correctly, in 1994, so I'm quite surprised that Maple insists in using deprecated commands and doesn't exploit amsmath.

What you want can be done, though. Possibly not as robust as one can hope, but it's a hack, after all.

The key is using mathtools in order to overcome the deficiencies of the old \cases command.

\documentclass[11pt,notitlepage]{article}

\usepackage{amsmath,mathtools}
\usepackage{amssymb}

\let\amscases\cases
\makeatletter
\def\cases{\@ifnextchar\bgroup\plaincases\amscases}
\def\plaincases#1{\begin{cases*}#1\end{cases*}}
\makeatother

\begin{document}

$$x- \cases{-2\,\sqrt {-y \left( x \right) }&y \left( x \right) \leq 0\cr 2\,\sqrt {y \left( x \right) }&0<y \left( x \right) \cr}+{\it \_C1}=0_{{1}}$$

$$x- \begin{cases} -2\,\sqrt {-y \left( x \right) }&y \left( x \right) \leq 0\\ 2\,\sqrt {y \left( x \right) }&0<y \left( x \right) \end{cases}+{\it \_C1}=0_{{1}}$$

$$x- \begin{cases} -2\sqrt{-y(x)} & y(x) \leq 0\\ 2\sqrt{y(x)} & 0 < y (x) \end{cases}+\mathit{\_C1}=0_{1}$$

\end{document}


The second example shows that the environment form is typeset correctly, the third is the “really correct” LaTeX input, without useless \left and \right and the obsolete and deprecated \it command.

-
you're a bit behind the times, @egreg. the first formal edition of "joy of tex" is copyright 1982, and there was an earlier draft version that circulated under what i believe may have been a slightly earlier copyright. (but you're right that maple should have fixed this by now!) – barbara beeton Jul 15 '14 at 15:12
@barbarabeeton I was referring to amstex for LaTeX. I'm not sure about the release date of version 1.0. Of course AMS-TeX appeared much earlier than 1990 (I started using it in 1985). – egreg Jul 15 '14 at 15:18
i've just checked (still have access to old versions). amstex.sty never had the environment form, always \cases, up through the last release in 1999 (which was declared obsolete and deprecated long before that, but remained in circulation so that old documents wouldn't become unprocessable). the environment form was introduced with amsmath, along with a general cleanup removing all applicable command forms in favor of environments. but you're correct that the first ams-latex appeared in 1990; the version 1.0 user's guide is dated july 1990. – barbara beeton Jul 15 '14 at 15:29
@barbarabeeton Thanks for the information; I'll update my answer. Is 1994 the right release date for amsmath? – egreg Jul 15 '14 at 15:31
there was almost certainly a preliminary release of amamath in 1994; the first printed ams-latex manual for version 1.2 has the date january 1995. (sadly, a printed date was omitted from some later, intermediate versions; i think this is always a bad idea, but there has been strong disagreement from the contemporaneous management during some periods.) – barbara beeton Jul 15 '14 at 15:45

It should be easy to replace all occurences of \cases by, say, \Cases. Then, with some additional modifications:

\documentclass[11pt,notitlepage]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
%smart renewcommand here? or \let?
\def\Cases#1{\begin{cases}#1\end{cases}}
\begin{document}

$$x- \Cases{-2\,\sqrt {-y \left( x \right) }&y \left( x \right) \leq 0\cr 2\,\sqrt {y \left( x \right) }&0<y \left( x \right) \cr}+{\it \_C1}=0_{{1}}$$

\end{document}


-
But you changed the actual Maple code manually? Changed cases to Cases in the code. The original Maple output used cases not Cases as you have there. The whole point is to not have to edit any of the Maple output as this will run many times and I can't edit the output manually each time I run the script. Thanks. – Nasser Jul 14 '14 at 23:09
@Nasser Yes, but it is one global change easy to do by any good text editor. – Przemysław Scherwentke Jul 14 '14 at 23:11
I understand, but as I said, this is automated and a script runs over them many times. So a solution that can avoid having to do any manual step, even if it is one step will be easier for me. – Nasser Jul 14 '14 at 23:12
Your scripted workflow could incorporate a python or sed or awk` script to automate the substitution. – Ethan Bolker Jul 15 '14 at 16:57