Compiling LATEX to computer algebra-enabled HTML5

Bernard Parisse
Institut Fourier
UMR 5582 du CNRS
Université de Grenoble I

2017

Abstract: This document explains how to create or modify an existing LATEX document with commands enabling computations in the HTML5 output: when the reader opens the HTML5 output, he can run a computation in his browser, or modify the command to be executed and run it. This is done by combining different softwares: hevea[6] for compilation to HTML5, giac.js for the CAS computing kernel (itself compiled from the C++ Giac[10] library with emscripten[11]), and a modified version[9] of itex2MML[3] for fast and nice rendering in MathML in browsers that support MathML.

Contents

Index

  • giacinputmath, 2.2
  • giaclink, 2.2
  • giaconload, 2.2
  • giacprog, 2.2
  • giacslider, 2.2

  • install, 2.1

1  Introduction

Combining LATEX rendering quality and CAS computing is not new:

  1. math softwares provide converters to export data to a LATEX file, or provide automated computations in a way similar to the way bibtex provides bibliography, like sagetex ([2]).
  2. some softwares handle both LATEX-like rendering and computation, for example texmacs ([5]), lyx ([7]), Jupyter notebook ([8]).

However, in the first case, the reader can not modify the CAS commandlines, and in the second case the data format is not standard LATEX (the writer can not start from an existing document) and requires additional software to be installed on the reader device or a net access to a server to run the computations.

The solution presented here is new in that the writer will edit a standard LATEX file, add a few easy to learn commands like \giacinputmath{factor(x^10-1)} or \giacinput{plot(sin(x))} and compile it to produce a HTML5+MathML document. The reader can see the document in any browser (it’s optimized for Firefox), without installation, and he can modify computation commandlines and run them on his own computer.

If you are reading this file in PDF format, it is highly recommended to open the HTML5/Mathml version in order to test interactivity and look at the LATEX source

2  User manual

2.1  Installation on the writer computer

If you have done a standard Unix install of Giac/Xcas 1.5 or greater, copy files from /usr/share/giac/doc (or a variation) in your LATEX working directory with a command like:

cd /usr/share/giac/doc
cp giac.tex giacfr.tex giac.js giacwasm.* hevea.sty mathjax.sty ~/Tex

Install the hevea package for your distribution (hevea -version should return 2.30 or higher).

Installation by hand: install

The files giac.tex (or the French version giacfr.tex) giac.js, giacwasm.js, giacwasm.wasm, hevea.sty, mathjax.sty must be copied in the LATEX working directory.

On an Internet connected linux box, the following shell script will install the tools required for HTML5/MathML output :

#! /bin/bash
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/giac.tex
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/giacfr.tex
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/giac.js
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/giacwasm.js
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/giacwasm.wasm
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/hevea.sty
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/mathjax.sty
wget http://hevea.inria.fr/distri/hevea-2.31.tar.gz
tar xvfz hevea-2.31.tar.gz
cd hevea-2.31
make
sudo make install
cd ..
wget https://www-fourier.ujf-grenoble.fr/~parisse/giac/heveatomml.tgz
tar xvfz heveatomml.tgz
cd heveatomml/src
make
sudo make install
cd ../..

2.2  On the writer side

We now assume that the installation is done. The writer opens a LATEX file with his usual editor. He must add in the preamble the following lines

\makeindex
\input{giac.tex}
\giacmathjax

For interactive CAS LATEX commands support, the writer should add

\begin{giacjshere}
\tableofcontents
\printindex

just after \begin{document} and

\end{giacjshere}

just before \end{document}. Add \giacpython inside begin/end{giacjshere} if you want to enable Python syntax compatibility.

Printing the table of contents and index before the first LATEX section command is recommended, otherwise the HTML output Table and Index buttons will not link correctly.

The rest of the source file is standard LATEX except that

Once the source file is written, it is compiled to HTML5 with the command
hevea2mml sourcefile.tex
The HTML output and the giac.js files should be in the same directory on the web server. Index and bibliography should be processed with makeindex and bibhva.

If a PDF output is desired, the command icas from a Giac/Xcas installation should be used instead of pdflatex because it will run all CAS commands, output them in a temporary LATEX file, and run pdflatex on the output (this was inspired by the pgiac script from Jean-Michel Sarlat [4]). The temporary file name is obtained by adding a _ at the end of the initial file name (without the .tex extension). Therefore, if you have an index and or citations, you should run makeindex and bibtex on the file name with _ appended. For bibtex citations in the HTML files, you should run bibhva. For example, the PDF version of this document is available here.

2.3  On the reader side

The reader’s browser opens an HTML5+MathML file (linking to the JavaScript giac.js). The MathML is rendered natively on Firefox or Safari, while Chrome or Internet Explorer will automatically load MathJax to render MathML (this is of course noticeably slower if the document is large). Computations are run by the reader’s browser (the CAS is JavaScript code). This is slower than native code but faster than net access to a server and it does not require setting up a specific server for computations.

2.4  More examples

2.4.1  Trace (2-d graph)

This example illustrates with a slider that the evolute of a curve is the envelope of the normals to the curve, here the curve is an ellipsis and the envelop an astroid. The list of normals L is initialized empty at load-time.

L:=[]

onload
Now move the slider:

=
Not evaled

2.4.2  Cone section (3-d graph)

CC is a cone of center the origin, axis of direction (0,0,1)(0,0,1), and angle π6\frac{\pi}{6}, PP is a plane of equation z=my+3z=my+3. mm is controlled by the slider, when mm moves the intersection is an ellipsis or hyperbola (limit value is a parabola).

=
Not evaled

2.4.3  Dunford decomposition (CAS)

A program computing the Dunford decomposition of a matrix with Newton method. It is parsed at load-time (giaconload environment).

function dunford(A)
  local U,p,q,q1,j,d,n;
  U:=A;
  n:=nrows(U);
  p:=charpoly(U);
  q:=p/gcd(p,p'); // square free part
  q1:=q';
  for (j:=1;j<=n;j:=2*j){
    d:=inv(horner(q1,U))*horner(q,U); // Newton step
    if (d==0*d) return U,A-U;
    U:=U-d;
  }
  return U,A-U;
end:;

onload
Example : we define JJ an almost diagonal matrix and AA a similar matrix and we check the Dunford decomposition of AA. J=(2 0 0 0 1 1 0 0 1),P=(1 0 0 2 1 0 3 4 1),A=PJP 1J=\left(\begin{array}{ccc} 2 & 0 & 0 \\ 0 & 1 & 1 \\ 0 & 0 & 1 \end{array}\right) , \quad P=\left(\begin{array}{ccc} 1 & 0 & 0 \\ 2 & -1 & 0 \\ 3 & 4 & 1 \end{array}\right), \quad A=PJP^{-1}



2.4.4  Slopefield

This will display the slopefield of an ordinary differential equation dydt=y+cos(t)\frac{dy}{dt}=-y+cos(t) and one solution corresponding to an initial condition y(0)y(0) that the user may modify with the slider.

=
Not evaled

2.4.5  Gröbner basis (CAS)

The CAS kernel can compute non-trivial Gröbner basis. Of course, the JavaScript version is significantly slower than the native Giac/Xcas kernel.

kat7:=[-x1+2*x8^2+2*x7^2+2*x6^2+2*x5^2+2*x4^2+2*x3^2+2*x2^2+x1^2,
 -x2+2*x8*x7+2*x7*x6+2*x6*x5+2*x5*x4+2*x4*x3+2*x3*x2+2*x2*x1,
 -x3+2*x8*x6+2*x7*x5+2*x6*x4+2*x5*x3+2*x4*x2+2*x3*x1+x2^2,
 -x4+2*x8*x5+2*x7*x4+2*x6*x3+2*x5*x2+2*x4*x1+2*x3*x2,
 -x5+2*x8*x4+2*x7*x3+2*x6*x2+2*x5*x1+2*x4*x2+x3^2,
 -x6+2*x8*x3+2*x7*x2+2*x6*x1+2*x5*x2+2*x4*x3,
 -x7+2*x8*x2+2*x7*x1+2*x6*x2+2*x5*x3+x4^2,
 -1+2*x8+2*x7+2*x6+2*x5+2*x4+2*x3+2*x2+x1]:;

onload
Basis over /16777213\mathbb{Z}/16777213

Basis over \mathbb{Q}

2.4.6  Logo turtle

def polyg(n,l):
  for j in range(n):
    avance l
    tourne_gauche 360/n

onload

efface;
for n in range(3,11):
  polyg(n,20)



2.4.7  Fractals

def fra1(X,Y,Nmax):
  w=2.7/X
  h=-1.87/Y
  res1=makelist(-ceil(X*Y/2)-1)
  res2=copy(res1)
  Y=Y-1
  for y in range(ceil(Y/2)+1):
    c = -2.1+i*(h*y+0.935)
    for x in range(X):
      z = 0
      for j in range(Nmax):
        if abs(z:=z**2+c)>2:
           break
      res1.append(pixon(x,y,2079+126*j)) # 5100*j+512
      res2.append(pixon(x,Y-y,2079+126*j))
      c = c+w;
  return res1,res2

onload

3  How this is done

The LATEX \giac... commands are defined in giac.tex. For example \giacinput is defined like this:

\newcommand{\giacinput}[2][style="width:400px;font-size:large"]{
\ifhevea
\@print{<textarea onkeypress="UI.ckenter(event,this,1)" }
\@getprint{#1>#2}
\@print{</textarea><button onclick="previousSibling.style.display='inherit';var tmp=UI.caseval(previousSibling.value);tmp=UI.rmquote(tmp); nextSibling.innerHTML='&nbsp;'+tmp;UI.render_canvas(nextSibling);">ok</button><span></span><br>}
\else
\lstinline@#2@
\fi
}

If hevea compiles the command, the \ifhevea part is active, and the command will output an HTML5 <textarea> element and a OK <button>, with a callback to JavaScript code that will evaluate the CAS command inside the textarea
var tmp=UI.caseval(previousSibling.value)
and fill the next HTML5 <span> field with the result of the CAS command.

The CAS evaluation is performed by a call to giaceval in the UI.caseval code (defined in giac.tex), where giaceval is a global JavaScript variable assigned at page load-time from the Module interface created by compiling Giac/Xcas with the C++ to JavaScript compiler emscripten. The CAS code being in JavaScript, it can be run on every JavaScript-enabled browser. It will be faster on browsers that have support for asm.js (asmjs.org) like Mozilla Firefox: numerical computations are 1 to 2 times slower than native code, while exact computations are 2 to 10 times slower than native code (the main reason being that JavaScript has currently no 64 bits integer type).

For a PDF output, if pdflatex is run on the tex file, giac commands will be written verbatim, but they will not be processed. The icas command from the Giac/Xcas package will filter all giac commands, process them and output the result in math mode in a temporary LATEX file. If the answer is a 2-d graph output, icas will output a pdf file on the hard disk and output a corresponding \includegraphics command in the temporary LATEX file. After that, the temporary file will be processed by pdflatex.

4  Conclusion

The current version of icas and giac.tex are already usable to easily produce HTML interactive CAS-enabled document from LATEX documents. They may be completed in future versions depending on user requests. For example, online courses might have commands to enable student exercises answers auto-check.

Acknowledgements
Thanks to Luc Maranget and Yannick Chevalier for fixing bugs in mathjax-enabled hevea. Thanks to Renée De Graeve and Murielle Stepec who have tested preliminary versions of this compilation method.

References

[1]
Yannick Chevallier. Hevea: LaTeX to HTML5 compiler, fork for MathJax support. https://github.com/YannickChevalier/hevea-mathjax, 2017.
[2]
Dan Drake. SageTex. https://www.ctan.org/pkg/sagetex, 2009.
[3]
Jacques Distler. LaTeX to MathML converter. golem.ph.utexas.edu/ distler/blog/itex2MML.html, 2016.
[4]
Jean-Michel Sarlat. pgiac. http://melusine.eu.org/syracuse/giac/pgiac/, 2011.
[5]
Joris van der Hoeven. Texmacs. http://www.texmacs.org/, 2017.
[6]
Luc Maranget. Hevea: LaTeX to HTML5 compiler (unstable version). http://hevea.inria.fr/distri/unstable/, 2017.
[7]
Matthias Ettrich. Lyx. https://ww.lyx.org, 2012.
[8]
NumFOCUS Foundation. The Jupyter Notebook. jupyter.org, 2017.
[9]
Bernard Parisse. LaTeX to Mathml converter, fork for hevea output support. www-fourier.ujf-grenoble.fr/ parisse/giac/heveatomml.tgz, 2017.
[10]
Bernard Parisse and Renée De Graeve. Giac/Xcas Computer Algebra System. www-fourier.ujf-grenoble.fr/ parisse/giac.html, 2017.
[11]
Alon Zakai. Emscripten: A C/C++ to Javascript compiler. kripken.github.io/emscripten-site/, 2017.