Adding a javascript script tag some place so that it works for every file in sphinx documentation

假装没事ソ 提交于 2019-12-18 11:17:14

问题


I am using Sphinx to write some notes. I am using the Mathjax extension for Math in the notes. The default size of the math is little larger than I would like. On the Mathjax page I found that I can change that size by adding the following script to the HTML file.

MathJax.Hub.Config({
  "HTML-CSS": {scale: 90}
});

So, I tried by adding the following in a .rst file:

.. raw:: html

    <script type="text/javascript" >
        MathJax.Hub.Config({
            "HTML-CSS": {
                scale: 90
            }
        });
    </script>

==========
Objective
==========

To change math size \\( \\alpha \\).

The above works great for the math in that specific .rst file. But I want to do this for many different .rst files that are all part of the same sphinx document. Is it possible to do this without adding the above script to every .rst file?

Thank you for reading this and would appreciate if you can help.


回答1:


This can be done with a template:

  1. Create a folder called templates in the Sphinx project directory.

  2. In conf.py, add

    templates_path = ["templates"]
    
  3. In the templates directory, create a file called layout.html with the following contents:

    {% extends "!layout.html" %}
    
    {%- block extrahead %} 
     <script type="text/javascript">
           MathJax.Hub.Config({
               "HTML-CSS": {
                   scale: 90
               }
           });
      </script>      
    {% endblock %}
    

The <script> element will be included in the <head> of every generated HTML page.

The extrahead template block is empty by default. See the Sphinx templating documentation for details.




回答2:


Another method:

Use the script_files setting in your overridden layout.html file.




回答3:


And a third approach, which doesn't involve templates:

Call add_javascript in the setup function in your Sphinx project's conf.py:

# conf.py

# ... other settings ...

def setup(app):
    # (create a setup() function if you don't already have one;
    # or add to the existing setup() ...)
    app.add_javascript("mathjax-config.js")

Create the file "mathjax-config.js" in your _static source directory. (Check the conf.py html_static_path setting to verify the static directories, or define one if needed.) Sphinx will copy it into the output directory during the build.

There's also an add_stylesheet method for css files. And both of them can take either relative paths to your static source dirs, or full urls to external resources.



来源:https://stackoverflow.com/questions/9444342/adding-a-javascript-script-tag-some-place-so-that-it-works-for-every-file-in-sph

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