7

I'm writing a paper and I have inserted a piecewise function into my tex-code. My problem is that the piecewise function is very tightly packed. How could I stretch the bracket and get more space between the formulas? See code and result below:

$$
\displaystyle{
P(Y_i = j) = \left\{
        \begin{array}{ll}
            \frac{1}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad k=K \\
            \frac{e^{\beta_j\cdot x_i}}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad 1 \leq k \leq K-1
        \end{array}
    \right.,
    }
$$

enter image description here

8

Don't use $$ for display math.

\displaystyle is not needed.

Option -1:

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

\[
P(Y_i = j) = \left\{
        \begin{array}{ll}
            \frac{1}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad k=K \\[1em]
            \frac{e^{\beta_j\cdot x_i}}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad 1 \leq k \leq K-1
        \end{array}
    \right.,
\]
\end{document}

enter image description here

Option - 2

You can use cases.

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

\[
P(Y_i = j) = \begin{cases}
            \frac{1}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad k=K \\[1em]   %%% <--- here
            \frac{e^{\beta_j\cdot x_i}}{1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad 1 \leq k \leq K-1
        \end{cases}
\]
\end{document}

In either case, you can use \\[<dimen>] to change the vertical separation.

Beautification

To make your sums to look less cramped, you can use \limits as in

\sum\limits_{k=1}^{K-1}

Code:

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

\[
P(Y_i = j) = \begin{cases}
            \frac{1}{1+\sum\limits_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad k=K \\[2em]
            \frac{e^{\beta_j\cdot x_i}}{1+\sum\limits_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad 1 \leq k \leq K-1
        \end{cases}
\]
\end{document}

enter image description here

  • ehm cases ehm – yo' Dec 11 '13 at 13:16
  • 1
    if you are loading amsmath anyway you could use the cases environment which is specifically for this use. – David Carlisle Dec 11 '13 at 13:16
  • @tohecz I didn't finish yet. Options are on way :-) – user11232 Dec 11 '13 at 13:17
  • @HarishKumar However, by that time, the OP will very likely leave, using quite not as good solution as he could. – yo' Dec 11 '13 at 13:17
  • @tohecz I am slow ;-). Now I am finished. :-) – user11232 Dec 11 '13 at 13:23
4

The dcases environment by »mathtools« from the »mh« bundle seems to be quite handy here.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}  % loads »amsmath«

\begin{document}
  \[
    P(Y_i=j)=
    \begin{dcases}
      \frac{1}{1+\sum\limits_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad k=K \\
      \frac{e^{\beta_j\cdot x_i}}{1+\sum\limits_{k=1}^{K-1}e^{\beta_k\cdot x_i}} & \quad 1 \leq k \leq K-1
    \end{dcases}
  \]
\end{document}

enter image description here

4

I had no idea that amsmath had such a useful redefinition, so that should be the correct answer (after use of cases). However, for completeness (or if you want more space) you have a few options:

  • reformulate your terms to allow for more vertical leg-room
  • modify \arraystretch to a desired scale (but will cause potentially awkward space on top)
  • use a strut, as in the example below:

    \documentclass{article}
    \usepackage{array}
    
    %\renewcommand\arraystretch{4}
    
    \begin{document}
    \[
    P(Y_i = j) = \left\{
      \begin{array}{>{\displaystyle}ll}
        \biggl(1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}\biggr)^{-1}
        & \quad k=K \\
        \biggl(1+\sum_{k=1}^{K-1}e^{\beta_k\cdot x_i}\biggr)^{-1}
        e^{\beta_j\cdot x_i}
        & \quad 1 \leq k \leq K-1
    
        \rule{0pt}{8ex} % called a 'strut' -- a vertical rule of nil width
      \end{array}
      \right.,
    \]
    \end{document}
    

    output

  • 1
    No, this is wrong. The parentheses are too large. Try \biggl(...\biggr) instead of \left(...\right), please. After all, why \displaystyle when `\textstyle would fit there much better? Anyways, +1 for getting rid of the fractions. – yo' Dec 11 '13 at 13:25
  • 1
    @tohecz Ah, yes - \left and \right don't take the limits into account very well... And personally, while I haven't much of a leg to stand on as for conventions, if you're going to have a large cases like this, it should be \displayed, not so cramped as in \textstyle. Just personal preference probably prejudiced by handwritten math :) – Sean Allred Dec 11 '13 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.