Search code examples
python-sphinxmathjax

How to allocate the label center of case using sphinx?


I'm writing equations using sphinx. My code looks like this:

.. math::
  :label: eq1

  \begin{eqnarray}
  \begin{cases}
  a_{11}x_{1} + \dots + a_{1m}x_{m} = b_1\\
  a_{21}x_{1} + \dots + a_{2m}x_{m} = b_2
  \end{cases}
  \end{eqnarray}

This displays the label (1) on the right side of the first line, but I would to allocate it the center of this {cases}, namely, between the first and the second line.

How do I do this?


Solution

  • This question seems to pertain principally about MathJax usage.

    By some experimenting I discovered it understands \smash and that using it provides the hoped for label placement. But perhaps there is some MathJax setting which would avoid using this \smash. Besides \smash works fine only for two or three "cases".

    I have moved the \smash approach to second part, where also its drawbacks are explained. Indeed, I have since found a half-satisfying CSS approach.

    At any rate, don't use eqnarray there.

    CSS based approach

    Create a file _static/custom.css in your source repertory with these contents.

    .MathJax_Display {
        transform: translate(0%,-50%) translate(0%,8px);
    }
    
    div.math {
        transform: translate(0%,50%);
    }
    

    Add this at end of conf.py:

    if html_theme != 'alabaster':
        def setup(app):
            app.add_stylesheet('custom.css') 
    

    For example I obtain this with 'classic' theme:

    enter image description here

    This works also with 'agogo' and 'alabaster' themes, but some fine tuning might be needed for equations with only one line to be positioned like the label.

    In the case RTD theme it does not work at all.

    Support may be browser dependent.


    \smash approach

    Text before.
    
    .. math::
       :label: eq1
    
       \smash{\begin{cases}
       a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
       a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
       \end{cases}}
    
    Text after.
    

    I get from make html (with classic theme)

    enter image description here

    conf.py contains

    extensions = ['sphinx.ext.mathjax',
    ]
    
    html_theme = 'classic'
    

    By the way your eqnarray is not good mark-up. IF you use it you should use :nowrap: option. See Sphinx doc.

    Note Of course usage of cases here is dubious because your example is one of equations. I added &'s but the spacing is one appropriate for cases, which is surely not the one expected. So remove them.

    Here is mark-up which does not abuse cases environment:

    Text before.
    
    .. math::
       :label: eq1
    
       \smash{\left\{\begin{aligned}
       a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
       a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
       \end{aligned}\right.}
    
    Text after.
    

    CAVEAT The smash trick only works with two (perhaps three) equations, even in MathJax.

    enter image description here