# How to separate a case into lines in a cases environment

I need to divide both the condition and equation part of some cases into lines, as the cases would not fit to text width. I read some similar questions and could not see that solves my problem. A MWE is given.

\documentclass[11pt,a4paper]{article}
\usepackage[paperheight=22.9cm, paperwidth=8.1cm, margin=0.1cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{authblk}
\usepackage{amsmath}
\usepackage{bm}

\title{The Title}
\author{Somebody}

\begin{document}
\maketitle

$$\label{eq:carcost} c = \begin{cases} 0 & \mbox{if aaaa bbbb ssssss ddddd fffff ssssss} \\ t(\psi_2-\psi_1)+t_p(\psi_3-\psi1) & \mbox{if aaaa bbbb ssssss ddddd fffff ssssss}\\ t(\psi_4-\psi_1)+t_p(\psi_5-\psi_1) & \mbox{if if aaaa bbbb ssssss ddddd fffff ssssss}\\ t(\psi_4-\psi_1)+t|\psi_2-\psi_4|+t|\psi_2-\psi_6|+t_p|\psi_3+\psi_5-\psi_1|& \mbox{if if aaaa bbbb ssssss ddddd fffff ssssss} \end{cases}$$

\end{document}

-

This might be what you're after in terms of the presentation:

\documentclass{article}
\usepackage[textwidth=15cm,showframe]{geometry}
\usepackage{amsmath}

\newcommand{\casestext}[2][2in]{\parbox[t]{#1}{\strut\ignorespace #2\strut}}
\begin{document}

$$\label{eq:carcost} c = \begin{cases} 0 & \casestext{if aaaa bbbb ssssss ddddd fffff ssssss} \\ t(\psi_2 - \psi_1) + t_p(\psi_3 - \psi_1) & \casestext{if aaaa bbbb ssssss ddddd fffff ssssss} \\ t(\psi_4 - \psi_1) + t_p(\psi_5 - \psi_1) & \casestext[2.5in]{if if aaaa bbbb ssssss ddddd fffff ssssss} \\ \parbox[t]{2.2in}{% t(\psi_4 - \psi_1) + t|\psi_2 - \psi_4| \\ \null\hfill{} + t|\psi_2-\psi_6|+t_p|\psi_3+\psi_5-\psi_1|} & \casestext{if if aaaa bbbb ssssss ddddd fffff ssssss} \\ \end{cases}$$

\end{document}


\casestext[<width>]{<stuff>} sets stuff in a \parbox that is [t]op-aligned and of width <width>. The default is 2in when you use \casestext{<stuff>}, although you can adjust this with each use by specifying a different length in the optional argument (as is done in the 3rd case). \struts ensure proper vertical spacing between elements.

The use of \parbox[t]{2.2in} in setting the equation in the 4th case was to give it a multi-line feel without resorting to much acrobatics.

I've adjusted the geometry layout to be more reasonable in this example.

-
Can parbox also automatically indent lines after the first line? Also for automatic breaking of equation part, I saw a breqn package. –  ozi Apr 2 '14 at 21:48
@ozi: You can use \newcommand{\casestext}[2][2in]{\parbox[t]{#1}{\hangindent=1em\hangafter=1 #2}} as a definition for \casetext which would allow for a hanging indent of 1em. breqn would not help in this instance. –  Werner Apr 2 '14 at 21:59
I wanted to further push the last term in the last equation down the way you show. It works with a glitch: the last two terms are aligned to the right of parbox, whereas I would want them to be aligned to the left. –  ozi Apr 3 '14 at 0:07
You can just remove the \null\hfill in my construction. Otherwise, I'm not exactly sure how you want it displayed. Do you want it spread over three lines? –  Werner Apr 3 '14 at 0:13
Yes I want that last one to spread over three lines. By the way, while playing, I discovered that something like \parbox[t]{1.5in}{\hangindent=1em\hangafter=1 $....\\....\\...$} nicely typesets the equation part too. Hence parbox with hangindent and hangafter solved all my problems in this equation. Thanks. –  ozi Apr 3 '14 at 0:19

You can use a tabular for the conditions and aligned for the formulas: no need to guess lengths, because you're deciding where to split.

\documentclass[11pt,a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{bm}

\newcommand{\splitcond}[1]{%
\begin{tabular}[t]{@{}l@{}}#1\end{tabular}%
}

\title{The Title}
\author{Somebody}

\begin{document}
\maketitle

\label{eq:carcost} c = \begin{cases} 0 & \splitcond{if aaaa bbbb ssssss \\ ddddd fffff ssssss} \\[4ex] t(\psi_2-\psi_1)+t_p(\psi_3-\psi1) & \splitcond{if aaaa bbbb ssssss \\ ddddd fffff ssssss} \\[4ex] t(\psi_4-\psi_1)+t_p(\psi_5-\psi_1) & \splitcond{if if aaaa bbbb ssssss \\ ddddd fffff ssssss} \\[4ex] \!\begin{aligned}[t] &t(\psi_4-\psi_1)+t|\psi_2-\psi_4|\\ &\quad+t|\psi_2-\psi_6|+t_p|\psi_3+\psi_5-\psi_1| \end{aligned} & \splitcond{if if aaaa bbbb ssssss \\ ddddd fffff ssssss} \end{cases}

\end{document}


The extra spacing is for making better visual separation between the conditions.

-
What is \! for? –  ozi Apr 2 '14 at 22:41
@ozi For historical reasons, amsmath adds a thin space in front of aligned and gathered. With \! it's removed. –  egreg Apr 2 '14 at 22:43

Here's a solution that makes the material fit into the very narrow measure specified by the OP. I've abandoned the cases environment and substituted in groups of array (for math stuff) and tabular (for text stuff) environments.

\documentclass[11pt,a4paper]{article}
\usepackage[paperheight=22.9cm, paperwidth=8.1cm, margin=0.1cm]{geometry}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{bm}
\begin{document}

$$\label{eq:carcost} c = \left\{ \begin{array}{@{}ll@{}} 0 & \begin{tabular}[t]{@{}l@{}} if aaaa bbbb ssssss \\ \ \ ddddd fffff ssssss \end{tabular} \\[4ex] \begin{array}[t]{@{}l@{}} t(\psi_2-\psi_1)\\ \ \ {}+t_p(\psi_3-\psi1) \end{array} & \begin{tabular}[t]{@{}l@{}} if aaaa bbbb ssssss \\ \ \ ddddd fffff ssssss \end{tabular}\\[4ex] \begin{array}[t]{@{}l@{}} t(\psi_4-\psi_1)\\ \ \ {}+t_p(\psi_5-\psi_1) \end{array} & \begin{tabular}[t]{@{}l@{}} if aaaa bbbb ssssss \\ \ \ ddddd fffff ssssss \end{tabular}\\[4ex] \begin{array}[t]{@{}l@{}} t(\psi_4-\psi_1)\\ \ \ {}+t|\psi_2-\psi_4|\\ \ \ {}+t|\psi_2-\psi_6|\\ \ \ {}+t_p|\psi_3+\psi_5-\psi_1| \end{array} & \begin{tabular}[t]{@{}l@{}} if aaaa bbbb \\ \ \ ssssss ddddd \\ \ \ fffff ssssss \end{tabular} \end{array} \right.$$
\end{document}

-
what do those @{}l@{} specify? –  ozi Apr 2 '14 at 21:34
@ozi - the @{} elements tell LaTeX not to insert whitespace at the left and right hand ends of the array and tabular environments, and the l ("left") is the alignment specifier. –  Mico Apr 2 '14 at 21:38

Yet another solution that you may find cleaner than some of the previous ones.

It uses package mathtools, which provides two environments that I'm using here:

1. dcases*, which is supposed to be an improvement over cases and normally saves you the trouble of switching to text mode in the condition, and

2. multlined, which splits a mathematical formula in multiple lines.

I'm using a fixed length parbox for the condition and the two macros \shoveright (for the last line) and \shoveleft (for inner lines) to make it look better. Also, I'm setting the \multlinegap length, which controls indentation.

\documentclass[11pt]{article}
\usepackage[paperheight=22.9cm,paperwidth=8.1cm,margin=0.1cm]{geometry}
\usepackage{mathtools}
\begin{document}
$$\setlength{\multlinegap}{0.5em} c = \begin{dcases*} 0 & \parbox[t]{2.25cm}{% if aaaa bbbb \\ ssssss ddddd \\ fffff ssssss } \\[3pt] \begin{multlined}[t] t(\psi_2-\psi_1)+ \\ \shoveright{t_p(\psi_3-\psi1)} \end{multlined} & \parbox[t]{2.25cm}{% if aaaa bbbb \\ ssssss ddddd \\ fffff ssssss } \\[3pt] \begin{multlined}[t] t(\psi_4-\psi_1)+ \\ \shoveright{t_p(\psi_5-\psi_1)} \end{multlined} & \parbox[t]{2.25cm}{% if aaaa bbbb \\ ssssss ddddd \\ fffff ssssss } \\[3pt] \begin{multlined}[t] t(\psi_4-\psi_1)+ \\ \shoveleft[\multlinegap]{t|\psi_2-\psi_4|+} \\ \shoveleft[\multlinegap]{t|\psi_2-\psi_6|+} \\ \shoveright{t_p|\psi_3+\psi_5-\psi_1|} \end{multlined} & \parbox[t]{2.25cm}{% if aaaa bbbb \\ ssssss ddddd \\ fffff ssssss } \end{dcases*}$$
\end{document}


Needless to say, I don't like the result, but this is what you asked for.

-