[Tex/LaTex] Writing conditional equations with braces in sphinx

casesequationshorizontal alignmentsphinx

I want to write conditional statement with one-sided curly brackets in Sphinx as shown below:

enter image description here

In Latex, the best I could do is given below:

\[
x = \begin{cases}
        \begin{split}
            d&,               &     I_x &\leq I_p - t \\
            s&, \; \; I_p - t &\leq I_x &\leq I_p + t \\
            b&, \; \; I_p + t &\leq I_x
        \end{split}
    \end{cases}
\] 

But it provides some blank spaces in middle of condition (marked in red) as shown below:

enter image description here

But even that also, doesn't work in Sphinx. It ends up in big error.

So how can I achieve it in both Latex and Sphinx?

Best Answer

For LaTeX I'd use an array:

\documentclass{article}
\usepackage{amsmath,array}
\begin{document}
\[
x =
\left\{
\begin{array}{
  @{}% no padding
  l@{\quad}% some padding
  r@{}% no padding
  >{{}}r@{}% no padding
  >{{}}l@{}% no padding
}
  d,&         &     I_x &\leq I_p - t \\
  s,& I_p - t &\leq I_x &\leq I_p + t \\
  b,& I_p + t &\leq I_x
\end{array}
\right.
\]
\end{document}

If the center terms of the inequalities don't have the same width, more alignment points are needed to make them centered.

enter image description here

We don't want any of the automatic padding between colums that array usually inserts, so between columns (and also at the start and end of the array) I put @{} that disables the insertion. Exception: between the first and second column we want some space.

The mysterious >{{}} bit is readily explained: when TeX is doing the second and third column in the second row, it would essentially do

$I_p-t$$\leq I_x$

because it treats each cell as a separate formula. This doesn't give the correct spacing, so we insert {} at the start of the cells in the third column and the final result is

$I_p-t$${}\leq I_x$

which typesets exactly like

$I_p-t\leq I_x$

and the desired outcome is obtained.

Related Question