Open in CoCalc with one click!
1
# Markdown mode for Maxima
2
def maxima_md(s):
3
import re
4
#print s
5
# insert calls to tex around expressions separated by $ or ; or as a single expression per line
6
s=re.compile(r'((?:(?![\n\$;]).)+)([\$;\n]?)',re.M+re.S).sub(r'tex(\1,false)\2',s,count=0)
7
#print s
8
t = maxima.eval(s,split_lines=true, reformat=false)
9
# cleanup generated LaTeX for MathJax
10
t=re.compile(r'\$\$').sub('\n$$\n',t,count=0)
11
t=re.compile(r'\\\\').sub(r'\\',t,count=0)
12
t=re.compile(r'\\cos').sub(r'\\mathrm{cos}',t,count=0)
13
t=re.compile(r'\\sin').sub(r'\\mathrm{sin}',t,count=0)
14
t=re.compile(r'\\_').sub(r'_',t,count=0)
15
t=re.compile(r'\\begin{verbatim}').sub(r'$$',t,count=0)
16
t=re.compile(r'\\end{verbatim}').sub(r'$$',t,count=0)
17
# strip non-escaped string quotes
18
t=re.compile(r'[^\\]"([^"]*?)').sub(r'\1',t,count=0)
19
# remove escape chars before quotes
20
t=re.compile(r'\\"').sub('"',t,count=0)
21
# Maybe this just text output? Indent as a preformatted code block
22
t=re.compile(r'\n\s*([^\$].*)').sub(r' \1',t,count=0)
23
# hide input and output numbers (Sage makes it impossible to use them anyway.)
24
t=re.compile(r'\(\%o[0-9]+\)').sub(r'',t)
25
t=re.compile(r'\(\%i[0-9]+\) ').sub(r'',t)
26
#print "'",t,"'"
27
md(t, hide=False)