我正在使用Sphinx写一些笔记。我在笔记中使用Mathjax扩展数学。数学的默认大小比我想要的大一点。在Mathjax页面上,我发现可以通过将以下脚本添加到HTML文件来更改该大小。
MathJax.Hub.Config({
"HTML-CSS": {scale: 90}
});
所以,我尝试在.rst
文件中添加以下内容:
.. raw:: html
<script type="text/javascript" >
MathJax.Hub.Config({
"HTML-CSS": {
scale: 90
}
});
</script>
==========
Objective
==========
To change math size \\( \\alpha \\).
上述功能非常适合特定.rst
文件中的数学运算。但我想为许多不同的.rst文件执行此操作,这些文件都是同一个sphinx文档的一部分。是否可以在不将上述脚本添加到每个.rst
文件的情况下执行此操作?
感谢您阅读本文,如果您能提供帮助,我们将不胜感激。
答案 0 :(得分:16)
这可以通过模板完成:
在Sphinx项目目录中创建名为templates
的文件夹。
在conf.py中,添加
templates_path = ["templates"]
在templates
目录中,使用以下内容创建名为layout.html
的文件:
{% extends "!layout.html" %}
{%- block extrahead %}
<script type="text/javascript">
MathJax.Hub.Config({
"HTML-CSS": {
scale: 90
}
});
</script>
{% endblock %}
<script>
元素将包含在每个生成的HTML页面的<head>
中。
默认情况下,extrahead
模板块为空。有关详细信息,请参阅Sphinx templating documentation。
答案 1 :(得分:3)
另一种方法:
使用已覆盖的layout.html
文件中的script_files设置。
答案 2 :(得分:3)
第三种方法,不涉及模板:
在Sphinx项目add_javascript
中的setup
函数中调用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")
在_static源目录中创建文件“mathjax-config.js”。 (检查conf.py html_static_path
设置以验证静态目录,或者在需要时定义一个。)Sphinx将在构建期间将其复制到输出目录中。
还有一个用于css文件的add_stylesheet
方法。并且它们都可以采用静态源目录的相对路径,或外部资源的完整URL。
答案 3 :(得分:1)
在Sphinx 3.0及更高版本中,添加简短的JavaScript配置摘要的最简单方法是在conf.py设置函数中调用app.add_js_file(None, body="...JS code...")
。示例:
i = 0, 1, 2, ...
使用这种方法,您无需创建单独的静态JS文件。
({# In your Sphinx project's conf.py:
# 1. Add whatever JS code you need as a string constant.
# (This example is from the original question.)
MATHJAX_CONFIG_JS = """
MathJax.Hub.Config({
"HTML-CSS": {scale: 90}
});
"""
# 2. Create a setup() function if you don't already have one.
# (If you do, just add to your existing setup() function.)
def setup(app):
# 3. Tell Sphinx to add your JS code. Sphinx will insert
# the `body` into the html inside a <script> tag:
app.add_js_file(None, body=MATHJAX_CONFIG_JS)
参数是在Sphinx 3.0中添加的;使用早期版本,您可以
仍将add_js_file()
与静态JS文件一起使用-请参阅
earlier answer。对于任何比
简短的配置代码段,还是最好使用外部文件。)
答案 4 :(得分:0)
仅conf.py
配置的最简单解决方案可能是使用MathJax扩展的配置值mathjax_config
(自1.8
起可用)。
mathjax_config
的值将传递到MathJax.Hub.Config()
。
根据您的具体情况,将以下内容添加到conf.py
:
mathjax_config = {
"HTML-CSS": {"scale": 90},
}