201

The code I used to write

this

is this one

$$
X(m,n)=
\begin{cases}
x(n),\\
x(n-1)\\
x(n-1)
\end{cases}
$$

But I don't know how to write this one


Also, what if I want to write this

user202729
  • 7,143
Maryà
  • 4,830

8 Answers8

170

For the question as it was originally posed, we can still use cases very easily. We simply employ the technique of using \left. at the start and then we can put \right\} at the end:

\[
  X(m, n) = \left.
  \begin{cases}
    x(n), & \text{for } 0 \leq n \leq 1 \\
    x(n - 1), & \text{for } 0 \leq n \leq 1 \\
    x(n - 1), & \text{for } 0 \leq n \leq 1
  \end{cases}
  \right\} = xy
\]

enter image description here

I'm not sure that you can span rows with cases as in the edit, however.

Au101
  • 10,278
  • 7
    that's my favourite answer, because of simplicity and keeping the original formulation with cases. Great job! – loved.by.Jesus Feb 18 '16 at 11:01
  • 1
    Apparently simple, sure, and it keeps the original cases environment, yes; but it does have a drawback, it inserts a spurious space (of width \nulldelimiterspace, usually equal to 1.2pt) both between the “=” sign and the left brace, and between the last column and tht right brace. (Will @loved.by.Jesus read this reply posted more than two years after her/his comment?) – GuM Apr 06 '18 at 22:18
  • @GuM I read it, of course, stack-overflow notifies about replies ;). Thanks for mentioning, what I think it is the only drawback of this answer. Now the info is complete! — I wonder how you know these intricacies about LaTeX; hat off. – loved.by.Jesus Apr 08 '18 at 18:06
  • @loved.by.Jesus: They are explained in several manuals, for example in The TeXbook. – GuM Apr 10 '18 at 17:59
  • how does one do it with a single curly bracket in front? – Charlie Parker Nov 10 '18 at 17:24
  • @CharlieParker just f(x) = \begin{cases} a \\ b \end{cases} – Au101 Nov 10 '18 at 20:04
  • this results in flat text for me sometimes. Why is that? Do I need to import some package? I just copy pasted your exact code. – Charlie Parker Apr 09 '21 at 18:05
  • seems one needs this package to be imported: \usepackage{amsmath} Perhaps you'd like to mention it in your answer? reference: https://www.overleaf.com/project/6070991842b7216188fa9324 – Charlie Parker Apr 09 '21 at 18:13
  • In github markdown I needed to escape the right curly bracket twice like \right\\} to make it work. – asmaier Nov 14 '23 at 12:54
141
\documentclass{article}
\usepackage{amsmath}

\begin{document}
  \[
    X(m,n) = \left\{\begin{array}{lr}
        x(n), & \text{for } 0\leq n\leq 1\\
        x(n-1), & \text{for } 0\leq n\leq 1\\
        x(n-1), & \text{for } 0\leq n\leq 1
        \end{array}\right\} = xy
  \]
\end{document}

enter image description here

If you want two conditions on different lines, can use a \multirow for the first column:

\documentclass{article}
\usepackage{amsmath}
\usepackage{multirow}

\begin{document}
  \[
    X(m,n) = \left\{\begin{array}{@{}lr@{}}
        \multirow{2}{*}{x(n),} & \text{for }0\leq n\leq 1\\
                               & \text{or }0\leq n\leq 1\\
        x(n-1), & \text{for }0\leq n\leq 1\\
        x(n-1), & \text{for }0\leq n\leq 1
        \end{array}\right\} = xy
  \]
\end{document}

enter image description here

Ignasi
  • 136,588
21

Essentially the same answer as Ignasi, but with the use of \text for the word for. (And with the \leqslant symbol of amssymb which I find much more elegant.)

\documentclass{article}
\usepackage{amsmath, amssymb}
\newcommand{\for}{\text{for }}
\begin{document}
  \[
    X(m,n)=
    \left\{
    \begin{array}{lr}
      x(n),& \for 0\leqslant n \leqslant 1 \\
      x(n-1),& \for 0\leqslant n \leqslant 1 \\
      x(n-1),& \for 0\leqslant n \leqslant 1
    \end{array}
    \right\} = xy.
  \]
\end{document}

enter image description here

Edit As for the modification recently asked:

\documentclass{article}
\usepackage{amsmath, amssymb}
\newcommand{\for}{\text{for }}
\begin{document}
  \[
    X(m,n)=
    \left\{
    \begin{array}{@{}lr@{}}
      x(n),& 
      \begin{array}{r@{}}
        \for 0\leqslant n \leqslant 1\\ 
        \text{or } 0\leqslant x \leqslant 1
      \end{array}\\
      x(n-1),& \for 0\leqslant n \leqslant 1 \\
      x(n-1),& \for 0\leqslant n \leqslant 1
    \end{array}
    \right\} = xy.
  \]
\end{document}

(I didn't deem it necessary to define a new macro for the word or since it is written only once. Note that it seems better this time to center the second column.)

Edit bis Added the @{} specifications suggested by daleif. No more centering.

enter image description here

Franck Pastor
  • 18,756
16

This has already been addressed, but I can't remember where nor when. So here is a simple solution with the empheq package, which loads mathtools, hence amsmath:

\documentclass{article}
\usepackage{amssymb}
\usepackage[overload]{empheq}
\newcommand{\for}{\text{for }}

\begin{document}

\begin{equation*}[right=\empheqrbrace{=xy.}]
    X(m,n)=
    \begin{cases}
      x(n),& \for 0\leqslant n \leqslant 1 \\
      x(n-1),& \for 0\leqslant n \leqslant 1 \\
      x(n-1),& \for 0\leqslant n \leqslant 1
    \end{cases}
\end{equation*}

\end{document} 

enter image description here

Bernard
  • 271,350
  • I had errors with the package, then, in case of an error with the package: \usepackage[overload]{empheq}, consult this link – Antonio Oct 12 '18 at 07:16
  • @Antonio: Remove the [overload] option and use \begin{empheq}[right=\empheqrbrace{=xy.}]{equation*} ......\end{empheq instead. – Bernard Oct 12 '18 at 11:30
  • I followed your variations, but if I remove [overload] and dispose as you suggested, the code gives me error. Eliminating \begin{empheq} and leaving only \begin{equation*} is ok – Antonio Oct 16 '18 at 09:36
  • 1
    For me both have no problem. Do you have errors with this single code? And which errors? – Bernard Oct 16 '18 at 09:47
  • i have : \begin{empheq}\begin{equation*}[right=\empheqrbrace{=\Div{\bar{u}}=0}]....(my equation)....\begin{cases}\text{.....} \\\text{.....} \\\end{cases}\end{equation*}\end{empheq} , the errors are: 1 ) Missing \endcsname inserted. \begin (in `\begin{equation}), Environment \endcsname undefined.* \begin in (\begin{cases}) and Undefined control sequence. \PassOptionsToPackag in the preamble – Antonio Oct 16 '18 at 10:28
  • 1
    The name of the environment, equation*, should be the mandatory argument of the empheq environment, it's not a nested environment. See the exact code in my first comment. – Bernard Oct 16 '18 at 10:39
  • now it's perfect, I've been in a hurry. – Antonio Oct 16 '18 at 10:48
11

Building upon the answer from Au101, what about nesting the cases environments?:

\[
  X(m, n) = \left.
  \begin{cases}
    x(n), & 
          \begin{cases} 
                \text{for } 0 \leq n \leq 1 \\
                \text{or }  0 \leq n \leq 1 
          \end{cases} \\
    x(n - 1), & \text{for } 0 \leq n \leq 1 \\
    x(n - 1), & \text{for } 0 \leq n \leq 1
  \end{cases}
  \right\} = xy
\]

LaTeX output

The nested braces are ugly, but this approach avoids the need for the multirow or array packages.

Ken
  • 111
8

You may also want to try \begin{Bmatrix}\end{Bmatrix}, for example

\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{equation}
  \begin{Bmatrix}
    c & u & r & l & y
    \\
    b & r & a & c & e
    \\
    i & s &
    \\
    & c & o & o & l
  \end{Bmatrix}
\end{equation}
\end{document}

For your specific example (thanks @Mico for the hint) you can tweak the definition of ams matrix to manipulate (overall) cell-aligment.

\documentclass{article}
\usepackage{amsmath}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% the following two definitions allow the usage of ams matrix environments
%% with an optional argument telling how to align the matrix, useful for
%% linear algebra texts, e.g.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \def\env@matrix{\hskip -\arraycolsep
  \let\@ifnextchar\new@ifnextchar
  \array{*\c@MaxMatrixCols c}}
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \makeatletter
 \renewcommand*\env@matrix[1][c]{\hskip -\arraycolsep
   \let\@ifnextchar\new@ifnextchar
   \array{*\c@MaxMatrixCols #1}}
 \makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{equation}
  X(m,n)
  =
  \begin{Bmatrix}[l]
    x(n),
    &
    \text{ for }
    0\leq n\leq1
    \\
    x(n-1),
    &
    \text{ for }
    0\leq n\leq1
    \\
    x(n-1),
    &
    \text{ for }
    0\leq n\leq1
  \end{Bmatrix}
  =
  xy
\end{equation}
\end{document}

The output is

I'm ignoring the fact that the math is nonsensical.

Note that while matrix environments are easier and more straightforward to use than arrays, they are less powerful. For example, column-dependent alignment is not easily achievable.

cgnieder
  • 66,645
  • 2
    Please clarify how your suggestion addresses the OP's typesetting needs. E.g., please post a full example. – Mico Apr 26 '16 at 09:07
  • 1
    @Mico I've fixed my example, but the downvote still there :-( – Oskar Limka Apr 29 '16 at 05:40
  • 1
    Well, it wasn't me who downvoted, but I've upvoted the answer to remove the -1. I still think you should make the example code more relevant and applicable to the OP's question. – Mico Apr 29 '16 at 06:06
8

I liked @Ken’s idea, but, once you have opted for a solution that uses nested “cases-like” environments, there’s an obvious improvement that permits to get rid of the additional unwanted brace, and it is to use the facilities provided by the mathtools package.

The mathtools package offers a \newcases command that lets you define, exactly, new “cases-like” environments with custom settings; in particular, it lets you specify custom delimiters on either side. There exist already several answers on this site that describe this command (just search the site for \newcases), so I’ll not dwell here on the details; however, the sample code below contains comments that should suffice as a quick, essential reference. The idea is to define two new “cases-like” environments, one with braces on both sides, and one with empty delimiters on both sides; remember, however, that an empty delimiter actually contributes a horizontal space of width \nulldelimiterspace, so you need to compensate for this by means of appropriate negative kerns.

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{mathtools} % automatically loads "amsmath" too

\makeatletter

% Arguments for the "\newcases" command (from "mathtools.sty", amended):
%   #1 <- name of the new environment;
%   #2 <- separation between columns;
%   #3 <- alignment preamble (see "The TeXbook", Ch. 22), left column;
%   #4 <- alignment preamble, right column;
%   #5 <- left delimiter (e.g., "\lbrace" for the standard "cases");
%   #6 <- right delimiter (e.g., "." for the standard "cases");

% To illustrate the full range of possibilities, we'll now define *four*
% new "cases-like" environments (although we need only one of them):
% all of them have braces on both sides, but they differ in the context
% their contents are typeset in.

% Left column is math, right column is math:
\newcases{doublecases}
    {\quad}
    {$\m@th{##}$\hfil} {$\m@th{##}$\hfil}
    {\lbrace} {\rbrace}

% Left column is math, right column is text:
\newcases{doublecases*}
    {\quad}
    {$\m@th{##}$\hfil} {{##}\hfil}
    {\lbrace} {\rbrace}

% Left column is display-style math, right column is display-style math:
\newcases{ddoublecases}
    {\quad}
    {$\m@th\displaystyle{##}$\hfil} {$\m@th\displaystyle{##}$\hfil}
    {\lbrace} {\rbrace}

% Left column is display-style math, right column is text:
\newcases{ddoublecases*}
    {\quad}
    {$\m@th\displaystyle{##}$\hfil} {{##}\hfil}
    {\lbrace} {\rbrace}

% For the case (!) of the "case-like" environment with empty delimiters
% on both sides, however, we define only the variant we actually need:

% Left column is right-aligned text, right column... doesn't matter:
\newcases{nullcases}
    {\quad}
    {\hfil{##}} {{##}\hfil}
    {.} {.}

\makeatother



\begin{document}

Some text before the equation.
\[
    X(m, n) =
        \begin{ddoublecases}
            x(n), & 
                    \kern -\nulldelimiterspace
                    \begin{nullcases} % beware of spaces!
                        for $0 \leq n \leq 1$\\
                        or $0 \leq n \leq 1$%
                    \end{nullcases}
                    \kern -\nulldelimiterspace
                \\
            x(n - 1), & \text{for $0 \leq n \leq 1$} \\
            x(n - 1), & \text{for $0 \leq n \leq 1$}
        \end{ddoublecases}
    = xy
\]
Some text after the equation.

\end{document}

Here is the output:

Output of the code

GuM
  • 21,558
0
\delta_{nm}=\begin{cases}
1, & \text{ if } m=n,\\
0, & \text{ ow.}
\end{cases}

Output 1

If you want the exact output, including the closing braces for the cases, as well as the two conditions for the first case, you can use package delarray as follows:

\documentclass{article}
\usepackage{amsmath}
\usepackage{array}
\usepackage{delarray}

\begin{document}

\begin{eqnarray} X(m, n)= \begin{array}{{ll}} x(n), & \text{ for } 0 \leq x \leq 1 \ x(n-1), & \text{ for } 0 \leq x \leq 1 \ x(n-1), & \text { for } 0 \leq x \leq 1 \ \end{array}=xy \end{eqnarray}

\begin{eqnarray} X(m, n)= \begin{array}{{rl}} x(n), & {\begin{matrix}\text{ for }0 \leq x \leq 1 \\text{ or }0 \leq x \leq 1 \end{matrix}} \ x(n-1), & \text{ for } 0 \leq x \leq 1 \ x(n-1), & \text { for } 0 \leq x \leq 1 \ \end{array}=xy \end{eqnarray} \end{document}

Output 2

This package allows you to specify the position of each element in the array cell, as well as the delimiters for the entire array, in your case the curly braces spanning all three cases.