3

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?

4

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.

  • +1 - That looks great! Can you tell me about the part of code where you added some comments on padding? what does it mean? – Abid Rahman K Jul 4 '13 at 10:25
0

For Sphinx, you can use directly @egreg's solution,

.. math::
   :nowrap:

   \[
   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.
   \]

and make sure your conf.py loads extension sphinx.ext.imgmath (or .mathjax: either one or the other is needed for math directive, but the LaTeX file will simply inherit the code as above and PDF output will work as with hand-written LaTeX source).

HTML with imgmath

You need to add imgmath_latex_preamble = r'\usepackage{array}' to your conf.py (and of course load sphinx.ext.imgmath extension).

enter image description here

For svg rather than png, add imgmath_image_format = 'svg' to conf.py.

HTML with MathJax

I don't know how to do that. There does not seem to be a contributed array.js extension to MathJax. There is siunitx.js. As a LaTeX package it requires array, but this does not at all mean that the siunitx.js implements array syntax ...

For more info:

As there does not seem to be a MathJax solution I could identify, even when editing manually the HTML file, I have not looked too closely into how to get the extra <script> tags.

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.