[Tex/LaTex] Custom color for LaTeX output in Sphinx documentation package

colorinline()mathjaxpreamblesphinx

I am trying to customize the color of the LaTeX inline formula when using Sphinx documentation package, and html output.

The details:

I have a file called func.rst, which includes the following line:

Let :math:`x_{1}` be a binary variable.

which is rendered successfully into LaTeX in the documentation I created with Sphinx.

(I have 'sphinx.ext.imgmath' listed in extensions in conf.py)

My goal is to have x_{1} colored in red.

Things I tried:

  1. Adding the color inside the formula:

    Let :math:`\color{red}x_{1}` be a binary variable.
    

    while also defining

    latex_elements['preamble'] = '\usepackage{xcolor}'
    

    in the conf.py file.

  2. Trying to define all math output globally with:

    latex_elements['preamble'] = r'''
    \usepackage{xcolor}
    \everymath{\color{red}}
    \everydisplay{\color{red}}
    '''
    

Needless to say, both (and many more less promising ideas) failed.

Would appreciate any help 🙂

Best Answer

As you seem to be targeting html with math rendered as PNGs images (or SVGs), the current config value to configure isn't latex_elements, but imgmath_latex_preamble.

Notice that this question is not TeX/LaTeX directly related but mainly one on usage of Sphinx. See sphinx-users mailing list (there is a Gmane interface under the gmane.comp.python.sphinx.user (news://news.gmane.org:119/gmane.comp.python.sphinx.user)

Ah I see now that this question is a cross-posted exact copy of https://stackoverflow.com/questions/50440108/custom-color-for-latex-output-in-sphinx-documentation-package