Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Configure mathjax in sphinx config
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez committed Mar 29, 2022
1 parent 43474c9 commit b90316d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
35 changes: 21 additions & 14 deletions src/sage/docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
import os
import sphinx
from sage.env import SAGE_DOC_SRC, SAGE_DOC, SAGE_SRC, THEBE_DIR, PPLPY_DOCS, MATHJAX_DIR
from sage.env import SAGE_DOC_SRC, SAGE_DOC, THEBE_DIR, PPLPY_DOCS, MATHJAX_DIR
from sage.misc.latex_macros import sage_mathjax_macros
import sage.version
from sage.misc.sagedoc import extlinks
import dateutil.parser
Expand All @@ -12,18 +13,24 @@
import sphinx.ext.intersphinx as intersphinx
from IPython.lib.lexers import IPythonConsoleLexer, IPyLexer


# General configuration
# ---------------------

# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = ['sage_docbuild.ext.inventory_builder',
'sage_docbuild.ext.multidocs',
'sage_docbuild.ext.sage_autodoc',
'sphinx.ext.todo',
'sphinx.ext.extlinks',
'IPython.sphinxext.ipython_directive',
'matplotlib.sphinxext.plot_directive']
extensions = [
"sage_docbuild.ext.inventory_builder",
"sage_docbuild.ext.multidocs",
"sage_docbuild.ext.sage_autodoc",
"sphinx.ext.todo",
"sphinx.ext.extlinks",
# Mathjax integration
# https://www.sphinx-doc.org/en/master/usage/extensions/math.html#module-sphinx.ext.mathjax
"sphinx.ext.mathjax",
"IPython.sphinxext.ipython_directive",
"matplotlib.sphinxext.plot_directive",
]

# This code is executed before each ".. PLOT::" directive in the Sphinx
# documentation. It defines a 'sphinx_plot' function that displays a Sage object
Expand Down Expand Up @@ -241,12 +248,12 @@ def set_intersphinx_mappings(app, config):
html_common_static_path = [os.path.join(SAGE_DOC_SRC, 'common', 'static'),
THEBE_DIR, 'static']

# We use MathJax to build the documentation.
extensions.append('sphinx.ext.mathjax')
mathjax_path = 'MathJax.js?config=TeX-AMS_HTML-full,../mathjax_sage.js'

from sage.misc.latex_macros import sage_mathjax_macros
html_theme_options['mathjax_macros'] = sage_mathjax_macros()
# Configure MathJax
mathjax3_config = {
"tex": {
"macros": sage_mathjax_macros()
}
}

mathjax_relative = os.path.basename(MATHJAX_DIR)

Expand Down
45 changes: 25 additions & 20 deletions src/sage/misc/latex_macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
contain '\newcommand' lines for each of the entries in ``macros``.
"""


def produce_latex_macro(name, *sample_args):
r"""
Produce a string defining a LaTeX macro.
Expand Down Expand Up @@ -111,34 +112,35 @@ def convert_latex_macro_to_mathjax(macro):
- ``macro`` - LaTeX macro definition
See the web page
http://www.mathjax.org/docs/1.1/options/TeX.html for a
https://docs.mathjax.org/en/latest/input/tex/macros.html for a
description of the format for MathJax macros.
EXAMPLES::
sage: from sage.misc.latex_macros import convert_latex_macro_to_mathjax
sage: convert_latex_macro_to_mathjax('\\newcommand{\\ZZ}{\\Bold{Z}}')
'ZZ: "\\\\Bold{Z}"'
('ZZ', '\\Bold{Z}')
sage: convert_latex_macro_to_mathjax('\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}')
'GF: ["\\\\Bold{F}_{#1}",1]'
('GF', ['\\Bold{F}_{#1}', 1])
"""
left_bracket = macro.find('[')
right_bracket = macro.find('[')
left_bracket = macro.find("[")
right_bracket = macro.find("[")
if left_bracket >= 0:
right_bracket = macro.find(']')
num_args = macro[left_bracket+1:right_bracket]
right_bracket = macro.find("]")
num_args = int(macro[left_bracket + 1 : right_bracket])
else:
num_args = 0
start_name = macro.find('{') + 1 # add one to go past the backslash
end_name = macro.find('}')
name = macro[start_name+1:end_name]
start_defn = macro.find('{', end_name)
end_defn = macro.rfind('}')
defn = macro[start_defn+1: end_defn].replace('\\', '\\\\')
start_name = macro.find("{") + 1 # add one to go past the backslash
end_name = macro.find("}")
name = macro[start_name + 1 : end_name]
start_defn = macro.find("{", end_name)
end_defn = macro.rfind("}")
defn = macro[start_defn + 1 : end_defn]
if num_args == 0:
return name + ': "' + defn + '"'
return name, defn
else:
return name + ': ["' + defn + '",' + str(num_args) + ']'
return name, [defn, num_args]


# To add a new macro for use in the Sage documentation, add a list or
# tuple to the following list. Each list (or tuple) should have the
Expand Down Expand Up @@ -176,6 +178,7 @@ def convert_latex_macro_to_mathjax(macro):
# mathbf vs mathbb. See latex.py for more information.
sage_configurable_latex_macros = [r"\newcommand{\Bold}[1]{\mathbf{#1}}"]


def sage_latex_macros():
r"""
Return list of LaTeX macros for Sage. This just runs the function
Expand All @@ -193,15 +196,17 @@ def sage_latex_macros():

def sage_mathjax_macros():
r"""
Return list of MathJax macro definitions for Sage as
JavaScript. This feeds each item output by
:func:`sage_latex_macros` to
Return Sage's macro definitions for usage with MathJax.
This feeds each item output by :func:`sage_latex_macros` to
:func:`convert_latex_macro_to_mathjax`.
EXAMPLES::
sage: from sage.misc.latex_macros import sage_mathjax_macros
sage: sage_mathjax_macros()
['ZZ: "\\\\Bold{Z}"', 'NN: "\\\\Bold{N}"', ...
{'Bold': ['\\mathbf{#1}', 1], 'CC': '\\Bold{C}', ...
"""
return [convert_latex_macro_to_mathjax(m) for m in sage_latex_macros()]
return dict(
[convert_latex_macro_to_mathjax(m) for m in sage_latex_macros()]
)

0 comments on commit b90316d

Please sign in to comment.