Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

python - Creating LaTeX math macros within Sphinx

I'm writing some mathematical code in Python and using Sphinx to produce the documentation. I know that Sphinx can handle LaTeX code in Python docstrings; see https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathbase. How can I create LaTeX macros, such as ewcommand{cG}{mathcal{G}}, to use in the Python docstrings?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are using MathJax, here's a possible solution. I'm still looking for a nicer solution, but it might help if you need a quick hack.

  1. Create a file under the directory specified in the html_static_path configuration option (typically _static), say mathconf.js. This will contain the JS configuration for MathJax. For instance (from the MathJax documentation):

    MathJax.Hub.Config({
      TeX: {
        Macros: {
          RR: '{\bf R}',
          bold: ['{\bf #1}', 1]
        }
      }
    });
    

    You can add more commands following the syntax above. The contents shown define the macros RR and old{#1}, this last one accepting one argument.

  2. Add a layout.html file at the _templates directory. The idea is to extend the current theme, so it searches the previous MathJax configuration file. Thus, the contents are:

    {% extends "!layout.html" %}
    {% set script_files = script_files + ["_static/mathconf.js"] %}
    

    Note that in this case it is the _static directory, because in this case it refers to where to search after the build. Sphinx will have moved the file from html_static_path to the _static directory under the build directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...