latex.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # -*- coding: utf-8 -*-
  2. """
  3. Latex Plugin For Pelican
  4. ========================
  5. This plugin allows you to write mathematical equations in your articles using Latex.
  6. It uses the MathJax Latex JavaScript library to render latex that is embedded in
  7. between `$..$` for inline math and `$$..$$` for displayed math. It also allows for
  8. writing equations in by using `\begin{equation}`...`\end{equation}`.
  9. Installation
  10. ------------
  11. To enable, ensure that `latex.py` is put somewhere that is accessible.
  12. Then use as follows by adding the following to your settings.py:
  13. PLUGINS = ["latex"]
  14. Be careful: Not loading the plugin is easy to do, and difficult to detect. To
  15. make life easier, find where pelican is installed, and then copy the plugin
  16. there. An easy way to find where pelican is installed is to verbose list the
  17. available themes by typing `pelican-themes -l -v`.
  18. Once the pelican folder is found, copy `latex.py` to the `plugins` folder. Then
  19. add to settings.py like this:
  20. PLUGINS = ["pelican.plugins.latex"]
  21. Now all that is left to do is to embed the following to your template file
  22. between the `<head>` parameters (for the NotMyIdea template, this file is base.html)
  23. {% if article and article.latex %}
  24. {{ article.latex }}
  25. {% endif %}
  26. Usage
  27. -----
  28. Latex will be embedded in every article. If however you want latex only for
  29. selected articles, then in settings.py, add
  30. LATEX = 'article'
  31. And in each article, add the metadata key `latex:`. For example, with the above
  32. settings, creating an article that I want to render latex math, I would just
  33. include 'Latex' as part of the metadata without any value:
  34. Date: 1 sep 2012
  35. Status: draft
  36. Latex:
  37. Latex Examples
  38. --------------
  39. ###Inline
  40. Latex between `$`..`$`, for example, `$`x^2`$`, will be rendered inline
  41. with respect to the current html block.
  42. ###Displayed Math
  43. Latex between `$$`..`$$`, for example, `$$`x^2`$$`, will be rendered centered in a
  44. new paragraph.
  45. ###Equations
  46. Latex between `\begin` and `\end`, for example, `begin{equation}` x^2 `\end{equation}`,
  47. will be rendered centered in a new paragraph with a right justified equation number
  48. at the top of the paragraph. This equation number can be referenced in the document.
  49. To do this, use a `label` inside of the equation format and then refer to that label
  50. using `ref`. For example: `begin{equation}` `\label{eq}` X^2 `\end{equation}`. Now
  51. refer to that equation number by `$`\ref{eq}`$`.
  52. Template And Article Examples
  53. -----------------------------
  54. To see an example of this plugin in action, look at
  55. [this article](http://doctrina.org/How-RSA-Works-With-Examples.html). To see how
  56. this plugin works with a template, look at
  57. [this template](https://github.com/barrysteyn/pelican_theme-personal_blog).
  58. """
  59. from pelican import signals
  60. latexScript = """
  61. <script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type= "text/javascript">
  62. MathJax.Hub.Config({
  63. config: ["MMLorHTML.js"],
  64. jax: ["input/TeX","input/MathML","output/HTML-CSS","output/NativeMML"],
  65. TeX: { extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"], equationNumbers: { autoNumber: "AMS" } },
  66. extensions: ["tex2jax.js","mml2jax.js","MathMenu.js","MathZoom.js"],
  67. tex2jax: {
  68. inlineMath: [ [\'$\',\'$\'] ],
  69. displayMath: [ [\'$$\',\'$$\'] ],
  70. processEscapes: true },
  71. "HTML-CSS": {
  72. styles: { ".MathJax .mo, .MathJax .mi": {color: "black ! important"}}
  73. }
  74. });
  75. </script>
  76. """
  77. def addLatex(gen, metadata):
  78. """
  79. The registered handler for the latex plugin. It will add
  80. the latex script to the article metadata
  81. """
  82. if 'LATEX' in gen.settings.keys() and gen.settings['LATEX'] == 'article':
  83. if 'latex' in metadata.keys():
  84. metadata['latex'] = latexScript
  85. else:
  86. metadata['latex'] = latexScript
  87. def register():
  88. """
  89. Plugin registration
  90. """
  91. signals.article_generate_context.connect(addLatex)