16

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

10 Answers 10

6

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)

See also the documentation : p.17 and more

2
  • 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 :-) Commented Feb 8, 2011 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)
    – PHL
    Commented Feb 8, 2011 at 16:24
12
\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}

enter image description here

6
  • 2
    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.. Commented Feb 7, 2011 at 13:34
  • 1
    I suggest dcases environment provided by mathtools package. Then \dfrac can be \frac.
    – Leo Liu
    Commented Feb 7, 2011 at 13:41
  • makes no difference when having a fraction inside a fraction, then I have ti use again \dfrac
    – user2478
    Commented Feb 7, 2011 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.
    – user2478
    Commented Feb 7, 2011 at 14:10
  • 15pt may be too much. How about increasing \arraystretch slightly (and locally)?
    – Leo Liu
    Commented Feb 7, 2011 at 15:49
11

enter image description here

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}
4

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%
  \dcases@custom@build{&\quad}}%
}
\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
% "&\quad". Because of how badly \dcases@custom@build, the actual commands
% 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 
    \let\math@cr@@\AMS@math@cr@@  \spread@equation \ialign 
    \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}
&\frac{n(x)}{1+j^2F^2} & for &  j=0\\[15pt]
2&\frac{\cos(jk)}{1+j^2F^2}f_j^{FD}(n(x))& for & j>0\\[15pt]
2&\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{FD}(n(x)) & for& j<0 
\end{customdcases}
\\[30pt]
\begin{mycases}
&\frac{n(x)}{1+j^2F^2} & for &  j=0\\[15pt]
2&\frac{\cos(jk)}{1+j^2F^2}f_j^{FD}(n(x))& for & j>0\\[15pt]
2&\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{FD}(n(x)) & for& j<0 
\end{mycases}
\end{align*}

\end{document}
3

cloning the amsmath cases environment, increasing the \arraystretch from the default setting of 1.2:

\makeatletter
\newenvironment{mycases}{%
  \matrix@check\mycases\env@mycases
}{%
  \endarray\right.%
}
\def\env@mycases{%
  \let\@ifnextchar\new@ifnextchar
  \left\lbrace
  \def\arraystretch{1.5}%
  \array{@{}l@{\quad}l@{}}%
}
\makeatother

you can adjust the 1.5 as necessary to get a pleasing result.

granted, this won't adjust for the missing "2" in the first line.

3

The mathtools package also provides the dcases environment.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}

\begin{document}
  \begin{equation}
    \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^\text{FD}(n(x))& \text{for } j>0\\
      2\frac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^\text{FD}(n(x)) & \text{for } j<0 
    \end{dcases}
  \end{equation}
\end{document}
1

Here is a more elegant syntax, which you can even use in your \newenvironment command:

 \documentclass{article}
 \usepackage{amsmath}
 \def\FD{\text{FD}}
 \newcommand\when[1]{&\text{for #1,}\\}
 \newcommand\otherwise{&\text{otherwise.}}
 \begin{document}
 \[ \begin{cases}
 \phantom{2}\dfrac{n(x)}{1+j^2F^2}                           \when{$j=0$}
     2 \dfrac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))        \when{$j>0$}
     2 \dfrac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x)) \when{$j<0$} 
    e^{i\pi} +1 \otherwise
 \end{cases} \]
 \end{document}
1
  • I have the same issues with this solution as I have with Herbert's. Commented Feb 7, 2011 at 13:34
0

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\quad$j>0$\cr\noalign{\vskip1\jot}
  2{\sin(jk) jF/\tau\over1+j^2F^2}f_j^{\rm FD}\left(n(x)\right)&
    for\quad$j<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
4
  • 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. Commented Feb 7, 2011 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
    Commented Feb 7, 2011 at 15:47
  • I was referring to the \phantom or \enspace that you need to align nicely the stuff on the first column. Commented Feb 8, 2011 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
    Commented Feb 8, 2011 at 13:32
0

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}
This is your example:
\[
\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:

cases

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:

abs

0

Via TABstack.

\documentclass{article}
\usepackage{mathtools,tabstackengine}
\def\FD{\text{FD}}
\setstackgap{L}{30pt}
\def\stackalignment{l}
\TABstackMath
\begin{document}
\[
\left\{\tabbedCenterstack{
   &\dfrac{n(x)}{1+j^2F^2}                                 & \text{for }j=0\\
  2& \dfrac{\cos(jk)}{1+j^2F^2}f_j^{\FD}(n(x))             & \text{for }j>0\\
  2& \dfrac{\sin(jk)jF/\tau}{1+j^2F^2}f_j^{\FD}(n(x))\quad & \text{for }j<0 
}\right.
\]
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .