# Custom color for LaTeX output in Sphinx documentation package

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 :)

• you are using mathjax not latex, so \usepackage etc do not work and unfortunately by default mathjax uses the wrong syntax for color, try \color{red}{x_{1}} – David Carlisle May 20 '18 at 21:29
• I still get an error "Undefined control sequence...". Anywhere I should declare the use of colors? – DalyaG May 20 '18 at 22:51
• that sounds like a latex error, are you not getting pdf output (which does use latex) for mathjax the wrong color is defined by default or you can use the color extension to fix it docs.mathjax.org/en/latest/tex.html?highlight=latex#color – David Carlisle May 20 '18 at 22:55
• (1) The file in which this formula exists is a .rst file, that is rendered using the conf.py file. These do not conform to the "usual" LaTeX (etc.) documents, and configurations are set in a different way. e.g., latex is converted to .png and then presented inside the html as images. (2) Your suggestion to use the mathjx syntax gave the same error as the one given by the latex syntex. – DalyaG May 20 '18 at 23:03
• Duplicate of stackoverflow.com/questions/50440108/… – user4686 May 21 '18 at 10:20

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)