# Looking for a more appropriate version of \cases

Often-times I find myself wanting to write something like,

That is, a vertical brace followed by a table consisting of three columns, the first and the last are math, aligned where-ever I like, and the middle one is text, right aligned.

I have now found the solution that I want using tabularx:

\left\{
\begin{tabular}{>{$}r<{{}$}@{}>{${}}l<{$}r>{$}r<{{}$}@{}>{${}}l<{$}}
&\frac{n(x)}{1+j^2F^2} & for &  j&=0\\
2&\frac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))& for & j&>0\\
2&\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & for& j&<0
\end{tabular}
\right.


(I put the extra braces inside the column definition so that that operators near the alignment character remain relative, which is the usual intention)

My question is two fold really:

1. Is there a package or a command that does this (I think that cases is supposed to but I still need to use \text{} which is annoying)?

2. What is the best way to turn this into an environment so that I can simply write

\begin{mycases}
&\frac{n(x)}{1+j^2F^2} & for &  j&=0\\
2&\frac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))& for & j&>0\\
2&\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & for& j&<0
\end{mycases}

-
Can I suggest a more specific title? The current one doesn't really say anything about your question :) –  Will Robertson Feb 7 '11 at 12:40
@lockstep: can you please explain how your editing fixed the problems I was having with the code block? –  Yossi Farjoun Feb 7 '11 at 13:29
@will: better? :-) –  Yossi Farjoun Feb 7 '11 at 13:32
@Yossi: You can find a bit of code markup help over at meta. –  Hendrik Vogt Feb 7 '11 at 15:23
It would be nice if you changed the title to something of the sort of "more robust syntax for \cases" –  Yossi Gil Feb 8 '11 at 13:14

Here is a "solution" with the package IEEEtranstool :

\documentclass{minimal}
\usepackage{mathtools}
\usepackage[retainorgcmds]{IEEEtrantools}
\def\FD{\text{FD}}
\begin{document}
\begin{IEEEeqnarray*}{rl"t"rC}
&\frac{n(x)}{1+j^2F^2} & for &  j&=0\\
2&\frac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))& for & j&>0\\
2&\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & for& j&<0
\end{IEEEeqnarray*}
\end{document}


The syntax is yours, but these solution lacks the vertical brace. Maybe a TeX wizard may helps.

For the explanation : rcl for right center left column in math mode, RCL is for math operator (extra-space), stu for text column and " for extra-space

The retainorgcmds option prevents IEEEtrantools from overriding existing LaTeX commands (itemize, enumerate and description)

-
I like this, with one small caveat...I need to include a use package just for this type of table...oh well...you can't have it all :-) –  Yossi Farjoun Feb 8 '11 at 13:10
@Yossi Farjoun, if you are ok with a small change in the syntax, a better solution can be {rl"t"rCl} and … j&=&0 ( or maybe {rl"t"r} and … j=0) –  Matsaya Feb 8 '11 at 16:24
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}
\def\FD{\text{FD}}
\def\for{\text{for }}
\begin{document}

$\begin{cases} \phantom{2}\dfrac{n(x)}{1+j^2F^2} & \for j=0\\[15pt] 2 \dfrac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x)) & \for j>0\\[15pt] 2 \dfrac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & \for j<0 \end{cases}$

$\left\{ \begin{matrix*}[l] & \dfrac{n(x)}{1+j^2F^2} & \for j=0\\[15pt] 2 & \dfrac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x)) & \for j>0\\[15pt] 2 & \dfrac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & \for j<0 \end{matrix*} \right.$

\end{document}


-
I must say that I really don't like the \for command. nor the use of phantom. I gave this as an example, but I really much prefer writing whatever text I want in the second column and using & as an alignment character.. –  Yossi Farjoun Feb 7 '11 at 13:34
I suggest dcases environment provided by mathtools package. Then \dfrac can be \frac. –  Leo Liu Feb 7 '11 at 13:41
makes no difference when having a fraction inside a fraction, then I have ti use again \dfrac –  Herbert Feb 7 '11 at 13:46
@Yossi: then use the matrix* environment, which doesn't look better in fact of the arraycolsep. See edit. For a good result you have to modify the spacing. The reason why cases is much more better! However an array is also possible, but then you have to know the number of columns. –  Herbert Feb 7 '11 at 14:10
15pt may be too much. How about increasing \arraystretch slightly (and locally)? –  Leo Liu Feb 7 '11 at 15:49

No magic here:

\documentclass{article}
\usepackage{amsmath}
\usepackage{mathtools}
\newcommand\FD{\mathrm{FD}}
\begin{document}
$\begin{dcases} \phantom{2}\frac{n(x)}{1+j^2F^2} & \text{for } j=0\\ 2\frac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))& \text{for } j>0\\ 2\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & \text{for } j<0 \end{dcases}$
\end{document}

-

Another solution, which I believe obeys your specifications, is below. I prefer Herbert's and Yossi Gil's answers.

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

\makeatletter
% The main environment takes one argument (column specification, which
% looks like {mttmtM{...}T{...}mM{...}}). It parses the column specification
% to create the preamble of a dcases-like environment, then uses this preamble.
%
% Uncomment the "\newlinechar..." line to see what the preamble looks like.
%
\newenvironment{customdcases}[1]{%
\edef\dcases@custom@preamble{%
\dcases@custom@build{} #1\dcases@custom@qstop}%
%{\newlinechar&\show\dcases@custom@preamble}%
\expandafter\dcases@custom@start\expandafter{\dcases@custom@preamble}%
}{\enddcases}

% To build the preamble, we take letters one by one, and transform them
% to the corresponding macro \dcases@custom@<letter> defined below. I still
% need to learn how to do this better. \dcases@custom@qstop is just a "quark"
% which expands to itself, useful for \ifx comparison purposes.
\newcommand*{\dcases@custom@build}[2]{%
\ifx \dcases@custom@qstop #2%
\else\expandafter\@secondoftwo%
\fi
\@gobble{#1\csname dcases@custom@#2\endcsname%
}
\newcommand{\dcases@custom@qstop}{\dcases@custom@qstop}

% The t and m variants produce text- and math- mode. The T and M variants take
% one argument and tell \dcases@custom@build to use it rather than the default
% take three arguments: \dcases@custom@build itself, {&\quad} (default
% argument), and the new custom argument.
\newcommand{\dcases@custom@t}{\unexpanded{\strut@{##}\hfil}}
\newcommand{\dcases@custom@m}{\unexpanded{\strut@$\m@th\displaystyle{##}$\hfil}}
\newcommand{\dcases@custom@T}[3]{\dcases@custom@t#1{&#3}}
\newcommand{\dcases@custom@M}[3]{\dcases@custom@m#1{&#3}}

% For the actual typesetting, we copy \MT_start_cases:nnn from mathtools and
% change it a little bit.
\newcommand{\dcases@custom@start}[1]{%
\relax \ifmmode \else \nonmatherr@ {\begin {\@currenvir }}\fi
\iffalse {\fi \ifnum 0=}\fi
\left\lbrace
\vcenter\bgroup
\Let@  \chardef\dspbrk@context\@ne  \restore@math@cr
\bgroup #1\crcr
}

% Finally, our specific custom environment
\newenvironment{mycases}{\customdcases{M{}mT{$\;$}m}}{\endcustomdcases}
\makeatother

We can then call our environment either using \texttt{customdcases} with the relevant column specifier, or \texttt{mycases}. In general, spacing next to relation symbols (in e.g., \verb|&=|) will be wrong, but in this specific case, it works.
\begin{align*}
\begin{customdcases}{M{}mT{$\;$}m}
\end{document}

-
I have the same issues with this solution as I have with Herbert's. –  Yossi Farjoun Feb 7 '11 at 13:34

The Plain version of \cases works pretty much the way you are hoping for:

\documentclass{article}
\begin{document}
$\cases{ \enspace{n(x)\over1+j^2F^2a}& for\quad$j=0\cr\noalign{\vskip1\jot} 2{\cos(jk)\over1+j^2F^2}f_j^{\rm FD}\left(n(x)\right)& for\quadj>0\cr\noalign{\vskip1\jot} 2{\sin(jk) jF/\tau\over1+j^2F^2}f_j^{\rm FD}\left(n(x)\right)& for\quadj<0$}$
\end{document}


In case you'd like to just & away, you could:

\catcode@=11
\def\cases#1{\left\{\,\vcenter{\normalbaselines\m@th
\ialign{$##\hfil$&&\quad##\hfil&\quad$##\hfil$\crcr#1\crcr}}\right.}
\catcode@=12


in the document preamble, which is just the Plain \cases with a repeating horizontal alignment preamble. Like so:

$$\cases{ math & text & math & text & etc \cr 1234\alpha & actual numbers, too & \sum435 & there as well \cr }$$


Of course, you could add another math-alignment tab for the digit if you don't fancy the \enspace/\phantom:

\catcode@=11
\def\cases#1{\left\{\,\vcenter{\normalbaselines\m@th
\ialign{$\hfil##$&$##\hfil$&&\quad##\hfil&\quad$##\hfil$\crcr#1\crcr}}\right.}
\catcode@=12
$\def\rep{1+j^2F^2}\cases{ &{n(x)\over1+\rep a}&for&j=0\cr 2&{\cos(jk)\over\rep}f_j^{\rm FD}(n(x))&for&j>0\cr 2&{\sin(jk)jF/\tau\over\rep}f_j^{\rm FD}(n(x))&for&j<0\cr }$
\bye

-
Nice plainTeX answer, however, the whole point of my question is that I don't want to fiddle with alignment issue in the text, I want the minimal job of placing a & character where needed, and let the environment do the rest. –  Yossi Farjoun Feb 7 '11 at 13:36
@Yossi Farjoun: Which alignment are you referring to? The quad? If so, just replacing it with a space is the same result as Leo Liu's for that particular part. The point I was trying to make with my answer is that the second tab in \cases is a textual tab. –  morbusg Feb 7 '11 at 15:47
I was referring to the \phantom or \enspace that you need to align nicely the stuff on the first column. –  Yossi Farjoun Feb 8 '11 at 13:06
@Yossi Farjoun: I don't think you can avoid that if you want it to align. \enspace == default (cmr10) \textfont0 digit width == 0.5em –  morbusg Feb 8 '11 at 13:32

This solution use the delarray package. The following:

\documentclass{article}

\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{delarray}

\newenvironment{mycases}[1][@{}@l>{$}l<{$}c@{}]{%
\renewcommand{\arraystretch}{1.3}%
\begin{array}\{{#1}.%
}{%
\end{array}%
}

\newcommand{\FD}{\text{FD}}

\begin{document}
$\begin{mycases} \frac{n(x)}{1+j^2F^2} & for & j=0 \\ 2\frac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x)) & for & j>0 \\ 2\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) & for & j<0 \end{mycases}$
\end{document}


produces:

However sometimes it's useful to have more or less columns, you can control that using the optional argument of the mycases environment, for example:

$|x|=\begin{mycases}[rc] x, & x\geq 0 \\ -x, & x< 0 \end{mycases}$
`

gives:

-