Selaa lähdekoodia

Merge pull request #415 from barrysteyn/master

LaTeX extensions for MathJax
Justin Mayer 10 vuotta sitten
vanhempi
commit
1c7bb6b4b6
3 muutettua tiedostoa jossa 34 lisäystä ja 20 poistoa
  1. 23 16
      render_math/Readme.md
  2. 10 3
      render_math/math.py
  3. 1 1
      render_math/mathjax_script_template

+ 23 - 16
render_math/Readme.md

@@ -42,27 +42,34 @@ settings file.
 
 The dictionary can be set with the following keys:
 
- * `align`: controls how displayed math will be aligned. Can be set to either
-`left`, `right` or `center`. **Default Value**: `center`.
- * `indent`: if `align` not set to `center`, then this controls the indent
-level. **Default Value**: `0em`.
- * `show_menu`: a boolean value that controls whether the mathjax contextual 
-menu is shown. **Default Value**: True
- * `process_escapes`: a boolean value that controls whether mathjax processes escape 
-sequences. **Default Value**: True
- * `latex_preview`: controls the preview message users are seen while mathjax is
-rendering LaTex. If set to `Tex`, then the TeX code is used as the preview 
-(which will be visible until it is processed by MathJax). **Default Value**: `Tex`
- * `color`: controls the color of the mathjax rendered font. **Default Value**: `black`
- * `linebreak_automatic`: Mathjax will try to *intelligently* break up any displayed math
+ * `align`: [string] controls how displayed math will be aligned. Can be set to either
+`'left'`, `'right'` or `'center'`. **Default Value**: `'center'`.
+ * `indent`: [string] if `align` not set to `'center'`, then this controls the indent
+level. **Default Value**: `'0em'`.
+ * `show_menu`: [boolean] controls whether the mathjax contextual menu is shown.
+**Default Value**: `True`
+ * `process_escapes`: [boolean] controls whether mathjax processes escape sequences.
+**Default Value**: `True`
+ * `latex_preview`: [string] controls the preview message users are shown while mathjax is
+rendering LaTex. If set to `'Tex'`, then the TeX code is used as the preview 
+(which will be visible until it is processed by MathJax). **Default Value**: `'Tex'`
+ * `color`: [string] controls the color of the mathjax rendered font. **Default Value**: `'inherit'`
+ * `linebreak_automatic`: [boolean] If set, Mathjax will try to *intelligently* break up displayed math
 (Note: It will not work for inline math). This is very useful for a responsive site. It
-is turned off by default due to it potentially being CPU expensive. **Default Value**: False
+is turned off by default due to it potentially being CPU expensive. **Default Value**: `False`
+ * `tex_extensions`: [list] a list of [latex extensions](http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions)
+accepted by mathjax. **Default Value**: `[]` (empty list)
 
-For example, in settings.py, the following would make math render in blue and
-displaymath align to the left:
+#### Settings Examples
+Make math render in blue and displaymath align to the left:
 
     MATH_JAX = {'color':'blue','align':left}
 
+Use the [color](http://docs.mathjax.org/en/latest/tex.html#color) and
+[mhchem](http://docs.mathjax.org/en/latest/tex.html#mhchem) extensions:
+    
+    MATH_JAX = {'tex_extensions': ['color.js','mhchem.js']}
+
 #### Resulting HTML
 Inlined math is wrapped in `span` tags, while displayed math is wrapped in `div` tags.
 These tags will have a class attribute that is set to `math` which 

+ 10 - 3
render_math/math.py

@@ -49,8 +49,9 @@ def process_settings(pelicanobj):
     mathjax_settings['show_menu'] = 'true'  # controls whether to attach mathjax contextual menu
     mathjax_settings['process_escapes'] = 'true'  # controls whether escapes are processed
     mathjax_settings['latex_preview'] = 'TeX'  # controls what user sees while waiting for LaTex to render
-    mathjax_settings['color'] = 'black'  # controls color math is rendered in
+    mathjax_settings['color'] = 'inherit'  # controls color math is rendered in
     mathjax_settings['linebreak_automatic'] = 'false'  # Set to false by default for performance reasons (see http://docs.mathjax.org/en/latest/output.html#automatic-line-breaking)
+    mathjax_settings['tex_extensions'] = ''  # latex extensions that can be embedded inside mathjax (see http://docs.mathjax.org/en/latest/tex.html#tex-and-latex-extensions)
 
     # Source for MathJax: Works boths for http and https (see http://docs.mathjax.org/en/latest/start.html#secure-access-to-the-cdn)
     mathjax_settings['source'] = "'//cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'"
@@ -90,8 +91,14 @@ def process_settings(pelicanobj):
         if key == 'color' and isinstance(value, basestring):
             mathjax_settings[key] = value
         
-        if key == 'linebreak_automatic' and isinstance(value, basestring):
-            mathjax_settings[key] = value
+        if key == 'linebreak_automatic' and isinstance(value, bool):
+            mathjax_settings[key] = 'true' if value else 'false'
+
+        if key == 'tex_extensions' and isinstance(value, list):
+            # filter string values, then add '' to them
+            value = filter(lambda string: isinstance(string, basestring), value)
+            value = map(lambda string: "'%s'" % string, value)
+            mathjax_settings[key] = ',' + ','.join(value)
 
     return mathjax_settings
 

+ 1 - 1
render_math/mathjax_script_template

@@ -6,7 +6,7 @@ if (!document.getElementById('mathjaxscript_pelican_#%@#$@#')) {{
     mathjaxscript[(window.opera ? "innerHTML" : "text")] =
         "MathJax.Hub.Config({{" +
         "    config: ['MMLorHTML.js']," +
-        "    TeX: {{ extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'], equationNumbers: {{ autoNumber: 'AMS' }} }}," +
+        "    TeX: {{ extensions: ['AMSmath.js','AMSsymbols.js','noErrors.js','noUndefined.js'{tex_extensions}], equationNumbers: {{ autoNumber: 'AMS' }} }}," +
         "    jax: ['input/TeX','input/MathML','output/HTML-CSS']," +
         "    extensions: ['tex2jax.js','mml2jax.js','MathMenu.js','MathZoom.js']," +
         "    displayAlign: '{align}'," +