In the background of md document, mistune is used to convert html, and in the front desk, MathJax.js is used to visualize LaTex formula. It is found that mistune converts to <em> or </em>, which leads to many formula visualization failures. Later, it was found that the package could be extended, and a subclass was extended with reference to official documents. When backstage conversion from md to html, nothing was done when encountering the $wrapped part.
LaTex visualization has been converted to html or SVG (example: http://maxiang.info/), which I prefer, but I haven't found the available python package. Who saw it and told me?
1. md file with LaTex code
$$ \varGamma(x)=\frac{\int_{\alpha}^{\beta}g(t)(x-t)^2\text{d}t}{\phi(x)\sum_{i=0}^{N-1}\omega_i}\tag{2} $$ $\alpha+\beta=\gamma$
2. python will convert md to LaTex
import re from mistune import Renderer, Markdown, InlineLexer # define new sub class #Let mistune not process the LaTex code between $$and $$$in the background, and send the js to the front desk to process it into mathematical formulas. class LaTexRenderer(Renderer): #def LaTex(self, alt, link): def LaTex(self, text): return '$$%s$$' % (text) class LaTexInlineLexer(InlineLexer): def enable_LaTex(self): # add LaTex rules self.rules.LaTex = re.compile( r'\$\$' # $$ r'([\s\S]+?)' # *** r'\$\$(?!\])' # $$ ) # Add LaTex parser to default rules # you can insert it some place you like # but place matters, maybe 3 is not good self.default_rules.insert(3, 'LaTex') def output_LaTex(self, m): text = m.group(1) #alt, link = text.split('|') # you can create an custom render # you can also return the html if you like #return self.renderer.LaTex(alt, link) return self.renderer.LaTex(text) # renderer = LaTexRenderer() inline = LaTexInlineLexer(renderer) # enable the feature inline.enable_LaTex() markdown = Markdown(renderer, inline=inline) # the end of sub class #Read md files fpath="../data/Python/ReadMe.markdown"; fr=open(fpath, 'r', encoding="utf-8") text=fr.read() fr.close() #md to html(leave LaTex alone) md=markdown(text) rs=md; #html to LaTex js3='<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>'; js3+='<script src="../static/js/showLaTex.js"></script>\n\n'; rs=rs+js3; fw=open("index4.html",'w',encoding="utf-8") fw.write(rs); fw.close(); print("end", len(rs));
The showLaTex.js file is the settings file:
/* * name showLaTex.js * Depending on MathJax.js * varsion: v0.1 *ES6*/ let isMathjaxConfig = false; // Prevent repetitive calls to Config, resulting in performance degradation const initMathjaxConfig = () => { if (!window.MathJax) { return; } window.MathJax.Hub.Config({ showProcessingMessages: false, //Close js loading process information messageStyle: "none", //Not displaying information jax: ["input/TeX", "output/HTML-CSS"], tex2jax: { inlineMath: [["$", "$"], ["\\(", "\\)"]], //In-line formula selector displayMath: [["$$", "$$"], ["\\[", "\\]"]], //Intra-paragraph formula selector skipTags: ["script", "noscript", "style", "textarea", "pre", "code", "a"] //Avoid certain labels }, "HTML-CSS": { availableFonts: ["STIX", "TeX"], //Optional font showMathMenu: false //Close right-click menu display } }); isMathjaxConfig = true; // }; if (isMathjaxConfig === false) { // If: MathJax is not configured initMathjaxConfig(); } // If the third parameter is not passed in, the entire document is rendered window.MathJax.Hub.Queue(["Typeset", MathJax.Hub]); // Because Vuejs are used, specify # app to increase speed //window.MathJax.Hub.Queue(["Typeset", MathJax.Hub, document.getElementById('app')]);