3

Context

I am using cases inside an R-Markdown document to display a formula (this for the context as I am not totally aware which tools Rmarkdown uses to typeset the formula eventually). But ultimately it has (IMHO) nothing to do with rmarkdown but a pure LaTeX problem (besides that it may become more difficult to add external packages or commands which are not part of the Rmarkdown tool chain)

Problem

I want to align the inequalities on the right side of the cases environment. The problem is that for the corner case I do not have a lower bound and thus the inequalities are not aligned anymore.

I tried to fill the gap with \phantom but since I have to substitute the \leq sign, the spacing is not aligned (I read here that LaTeX adds a \thickmuskip after an inequality operator and I guess this is exactly what is missing . However, I cannot add \thickmuskip in Rmarkdown).

(N.B. I know that the inqualities do not make sense ;) - but that's clearly not the point)

\begin{cases}
1, & \text{if}\ \phantom{{-1.0}<} x_i < -0.2\\                % too little space
2, & \text{if}\ {-0.2} \leq x_i < -0.1\\                      % add brackets around to -0.2 to treat `-` as unary
3, & \text{if}\ {-0.1} \leq x_i < \phantom{-}0.1 \\
4, & \text{if}\ \phantom{{-}}0.1 \leq x_i < \phantom{-}0.5\\
5, & \text{if}\ \phantom{{-0.5}<1} x_i < \phantom{-}5.0       % too much space
\end{cases}

Output

Misaligned inequalities

  • 1
    When your \phantom{ } ends with <, change it to \phantom{...<{}} – Donald Arseneau Aug 19 '20 at 8:23
  • Nice, even better that adding a \; which was my "solution". Would you mind adding an answer so I would happily accept it? – thothal Aug 19 '20 at 8:26
3

Since the spacing around infix binary operators depends on what surrounds them, they can lose their spacing when they appear at the edge of an inner math list (against braces). You have made use of this to enure the minus signs remain as unaray minus (negative numbers). Good! (Although unnecessary in some cases.) The problem comes with the relation symbols that come at the end of the phantom.

You can add explicit space (\;) to compensate, or you can provide something to restore the binary-relation treatment:

\begin{cases}
1, & \text{if}\ \phantom{{-1.0}<{}} x_i < -0.2\\
2, & \text{if}\ {-0.2} \leq x_i < -0.1\\
3, & \text{if}\ {-0.1} \leq x_i < \phantom{-}0.1 \\
4, & \text{if}\ \phantom{{-}}0.1 \leq x_i < \phantom{-}0.5\\
5, & \text{if}\ \phantom{{-0.5}<{}} x_i < \phantom{-}5.0 
\end{cases}
  • Thanks, but I disagree that it is unnecessary. Cf.\[\text{a}\ -1 \quad \text{vs.} \quad text{a}\ {-1}\] and in the first case it renders as a substraction between the \text and the 1 and only in the latter it renders correctly as -1 – thothal Aug 19 '20 at 8:48
  • Ah yes. I was looking at the ones inside \phantom{ }. – Donald Arseneau Aug 19 '20 at 9:02
  • Edited to reflect that. – Donald Arseneau Aug 19 '20 at 9:03
4

You can use an array:

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

\begin{document}

\begin{equation*}
\left\{
\renewcommand{\arraystretch}{1.2} % like cases does
\setlength{\arraycolsep}{0pt} % we don't want padding
\begin{array}{
  l           % value
  @{\quad}    % like cases does
  >{$}l<{ $}  % text column with trailing normal space
  r           % lower bound
  >{{}}c<{{}} % relation
  c           % variable
  >{{}}c<{{}} % relation
  r           % upper bound
}
1, & if &      &     & x_i & < &  0.2 \\
2, & if & -0.2 & \le & x_i & < & -0.1 \\
3, & if & -0.1 & \le & x_i & < &  0.1 \\
4, & if &  0.1 & \le & x_i & < &  0.5 \\
5, & if &  0.5 & \le & x_i
\end{array}
\right.
\end{equation*}

\end{document}

Instead of \left\{\renewcommand{\arraystretch}{1.2} and \right. you could use \begin{cases} and \end{cases} (at the expense of a slight shift to the left of the whole display.

enter image description here

  • I would be inclined to use array or alignedat just to get rid of the big brace, which serves no purpose in this math display. – Donald Arseneau Aug 19 '20 at 9:23
  • 1
    @DonaldArseneau I guess there is something like f(x_i)= at the left of the brace. – egreg Aug 19 '20 at 9:37
3

You also have a simpler possibility with alignedat nested in cases. I'm not sure it is a good idea to align the numbers at the end of lines, in addition to aligning the < signs. So I added a variant code , using the empheq package, without this final alignement. It adds the possibility to number or subnumber each line, which might be useful in some situations:

\documentclass[a4paper,12pt]{article}

\usepackage{array}
\usepackage{empheq}

\begin{document}

 \[ \begin{cases}
\begin{alignedat}{3}
&1, &\quad & \text{if} & x_i &< -0.2\\
&2, & &\text{if} & \quad -0.2 \leq x_i &< -0.1\\
&3, & &\text{if} &-0.1 \leq x_i & < \phantom{-}0.1 \\
&4, & &\text{if} & \phantom{{-}}0.1 \leq x_i &< \phantom{-}0.5\\
&5, & &\text{if} & x_i &< \phantom{-}5.0 % too much space
\end{alignedat}
\end{cases} \]
\vspace{1cm}

\begin{subequations}
 \begin{empheq}[left=\empheqlbrace]{alignat=3}
&1, &\quad & \text{if} & x_i &< -0.2\\
&2, & &\text{if} & \quad -0.2 \leq x_i &< -0.1\\
&3, & &\text{if} &-0.1 \leq x_i & < 0.1 \\
&4, & &\text{if} & \phantom{{-}}0.1 \leq x_i &< 0.5\\
&5, & &\text{if} & x_i &< 5.0 % too much space
\end{empheq}
\end{subequations}

\end{document} 

enter image description here

2

If you don't want to number the equations, I would use an array as suggested in others answers. However, if you want to number the equations, you can use {DispWithArrow} of witharrows:

\documentclass{article}
\usepackage{witharrows}
\usepackage{amsmath}

\begin{document}

\begin{DispWithArrows}<f(x_i)=>[format=lrrcccr]
1, & \quad \text{if } &        &         & x_i & {}<{} &  0.2 \\
2, &       \text{if } & {-0.2} & {}\le{} & x_i & {}<{} & {-0.1} \\
3, &       \text{if } & {-0.1} & {}\le{} & x_i & {}<{} &  0.1 \\
4, &       \text{if } &  0.1   & {}\le{} & x_i & {}<{} &  0.5 \\
5, &       \text{if } &  0.5   & {}\le{} & x_i
\end{DispWithArrows}

\end{document}

Output of the above code

1

Ok, apparently I misunderstood \thickmuskip which is a length, rather than a command. \; should add this:

\begin{cases}
1, & \text{if}\ \phantom{{-1.0}<}\; x_i < -0.2\\
2, & \text{if}\ {-0.2} \leq x_i < -0.1\\
3, & \text{if}\ {-0.1} \leq x_i < \phantom{-}0.1 \\
4, & \text{if}\ \phantom{{-}}0.1 \leq x_i < \phantom{-}0.5\\
5, & \text{if}\ \phantom{{-0.5}<}\; x_i < \phantom{-}5.0 
\end{cases}

enter image description here

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.