Creating LaTeX math macros within Sphinx

后端 未结 5 781
梦谈多话
梦谈多话 2020-12-14 11:03

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.s

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-14 11:44

    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 \bold{#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.

提交回复
热议问题