3

in the equations shown below I would like to have the 'if' texts aligned - right now this does not work due to the use of three independent array environments. Thanks!! three equations with cases

Here is the code to generate above shown equations:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\begin{document}
\begin{align}
\label{eq:RawOffsetCalculation}
y_\text{offset raw}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right. \\
\label{eq:CorrectedOffsetCalculation}
y_\text{offset corr}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right.
\\
\label{eq:CumulativeOffsetCalculation}
y_\text{offset cum}
&
=\left\{
\begin{array}{rl}
0
&
\quad\text{if }n_\text{offset}=0 \\
\\
\dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}}
&
\quad\text{if }n_\text{offset}>0
\end{array}
\right.
\end{align}
\end{document}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Jul 27 '15 at 6:59
  • @Wolf277, Out of theme: You could use cases math environment for equations with cases. – juanuni Jul 27 '15 at 7:06
  • Your summation notation is a bit idiosyncratic: The summation index is set to i, yet the variables being summed do not seem to contain an i-component. – Mico Jul 27 '15 at 8:04
2

Use the p column specifier instead of r:

\begin{array}{>{\RaggedLeft$}p{6em}<{$}l}

It needs the $ ..$ definition because its contents is in text mode:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{array,ragged2e}
\begin{document}

    \begin{align}\label{eq:RawOffsetCalculation}
    y_\text{offset raw} & =\left\{
        \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
            0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right. \\
    \label{eq:CorrectedOffsetCalculation}
    y_\text{offset corr}
    &
    =\left\{ \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
      0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right.
    \\
    \label{eq:CumulativeOffsetCalculation}
    y_\text{offset cum} & =\left\{
    \begin{array}{>{\RaggedLeft$}p{6em}<{$}l}
      0 & \quad\text{if }n_\text{offset}=0 \\[\normalbaselineskip]
    \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}} & \quad\text{if }n_\text{offset}>0
    \end{array} \right.
    \end{align}

\end{document}

enter image description here

  • having more space between the intra-\cases lines than between the \cases entries makes them look like the last part of the first \cases is closely associated with the first part of the second one. a mixed message. – barbara beeton Jul 27 '15 at 9:13
1

You can use cases environment for this kind of equations. For your problem You could define multicases envinment and use phantom command to get correct alignment:

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
%\usepackage[latin1]{inputenc}
\usepackage{amsmath}

\makeatletter
 \newenvironment{multicases}[1]
   {\let\@ifnextchar\new@ifnextchar
    \left\lbrace\def\arraystretch{1.2}%
    \array{@{}l*{#1}{@{\quad}l}@{}}}
   {\endarray\right.\kern-\nulldelimiterspace}
\makeatother

 \begin{document}
 \begin{align}
 y_\text{offset raw}&=
 \begin{multicases}{1}
  0,& \text{if }n_\text{offset}=0 \\
  \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{raw\phantom{ cum}}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq1}\\
 y_\text{offset corr}&=
 \begin{multicases}{1}
 0,&\text{if }n_\text{offset}=0 \\
 \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr\phantom{ cum}}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq2}\\
 y_\text{offset cum}&=
 \begin{multicases}{1}
 0,&\text{if }n_\text{offset}=0 \\
 \dfrac{\sum\limits_i^{n_\text{offset}}y_\text{corr cum}}{n_\text{offset}},&\text{if }n_\text{offset}>0
 \end{multicases}\label{eq3}
 \end{align}
 \end{document}

enter image description here

PS: Note label is after each equation.

1

Here's another solution that keeps your basic align/array setup. The desired alignment is achieved by (a) measuring the width of the largest fractional term and (b) placing the 0-terms in boxes of said width.

I would further like to suggest that you (i) insert a bit of whitespace between the three equations and (ii) indicate explicitly the dependence of the terms being summed on some index variable i. (In the code you've posted, i occurs below the summation signs but nowhere else.)

enter image description here

\documentclass[11pt]{scrreprt}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} % for "align" environment

% define the largest fractional term and save its width to "\mywid"
\newcommand\bigfrac{%
  \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}} y_{\text{corr\,cum},i}}{n_{\text{offset}}}}
\newlength\mywid
\settowidth\mywid{$\bigfrac$}  % save width of "\bigfrac" to "\mywid"

\begin{document}
\setcounter{chapter}{3}
\setcounter{equation}{13}
\begin{align}
y_\text{offset\,raw} &=\left\{
\begin{array}{r@{\qquad}l}
   \makebox[\mywid][r]{$0$} %% place "0" in a box of width \mywid
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}}y_{\text{raw},i}}{n_{\text{offset}}}
   &\text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:RawOffsetCalculation}\\[3ex]
y_\text{offset\,corr} &=\left\{
\begin{array}{r@{\qquad}l}
   \makebox[\mywid][r]{$0$} %% place "0" in a box of width \mywid
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \dfrac{\sum\limits_{i=1}^{n_{\text{offset}}}y_{\text{corr},i}}{n_{\text{offset}}}
   & \text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:CorrectedOffsetCalculation}\\[3ex]
y_\text{offset\,cum} &=\left\{
\begin{array}{r@{\qquad}l}
   0 %% no special treatment needed for this term
   & \text{if $n_{\text{offset}}=0$} \\[1.5ex]
   \bigfrac  % re-use the macro (no need to type it in again, right?)
   & \text{if $n_{\text{offset}}>0$}
\end{array}\right. \label{eq:CumulativeOffsetCalculation}
\end{align}
\end{document}

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.