Custom color for LaTeX output in Sphinx documentation package

烈酒焚心 提交于 2019-12-24 01:36:35

问题


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.


回答1:


Copying over my answer on cross-posted question at tex.sx:

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.

I tested since and it works.




回答2:


For completeness sake, I am adding here the full solution. (THANKS jfbu!)

  1. In conf.py I defined extensions = ['sphinx.ext.imgmath', <some_more_unrelated_stuff>]

  2. Also in conf.py I defined

    imgmath_latex_preamble=r'\usepackage{xcolor}'
    

    (EDIT: in ooposed to what I previously wrote,there is no need to define in addition imgmath_latex="/usr/local/texlive/2017/bin/x86_64-darwin/latex" thanks jfbu again)

  3. In the .rst file where I have the latex expression, I have

    Let :math:`\color{red}x_{1}` be a binary variable.
    
  4. In the terminal I run

    make clean html
    

    ("make clean" is the sphinx's best friend)

And its working! wohoo!



来源:https://stackoverflow.com/questions/50440108/custom-color-for-latex-output-in-sphinx-documentation-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!