17

I'm guessing this has been done in a thread before, but I've just spent two hours reading various threads and cannot get my head around a very basic question.

Long story short: I'm trying to create dynamic equations/diagrams in a web browser for a homework generator. So far I've been using MathJax and loving it for the equation side of things. It's very simple as I just need to include a single line of Javascript code and then I'm good to start coding:

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({tex2jax: {inlineMath: [['|','|'], ['\\ 
(','\\)']]}});
</script>
<script type="text/javascript"
  src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX- AMS-MML_HTMLorMML">
</script>

<?
$x = rand(2,5);
echo "x|^2| = ".pow($x,2).", so x = |\pm|$x";
?>

And it correctly turns that into the equation form on the browser.

Now, my goal is to do the same sort of thing with TikZ so I can create triangles and so forth on the fly using code like:

<?
$vertex1 = rand(2,5);
$vertex2 = rand(2,5);
?> 
\documentclass[12pt, border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) node[anchor=north]{$A$}
  -- (<? echo $vertex1; ?>,0) node[anchor=north]{$C$}
  -- (<? echo $vertex1; ?>,<? echo $vertex2; ?>) node[anchor=south]{$B$}
  -- cycle;
\end{tikzpicture}
\end{document}

So my first question, which I think is answered "no" but is worth checking anyhow - is there a simple way to call TikZ in Javascript as with MathJax? Because that would be amazing.

If not, how can I get to a place where I can actually use TikZ from HTML/Javascript? I believe that some websites show TikZ code, but I cannot find any documentation on this (everything just assumes I'm using a LaTeX program that can summon TikZ with a simple \usepackage{tikz} call, which I don't think MathJax can do).

For now, I'm running my scripts on a localhost my Macbook via MAMP, but eventually I would like to move it to an actual web server - but if it comes down to needing to install it at a root level, I can look into that. Just need some clear explanations since I'm really new to the technical side of LaTeX.

Alex Gold
  • 315
  • 2
  • 10

1 Answers1

16

Yes, you can run TikZ in JavaScript via https://github.com/kisonecat/tikzjax and get SVG output. After loading the appropriate CSS and JavaScript, it converts

<script type="text/tikz">
  \begin{tikzpicture}
    \draw (0,0) circle (1in);
  \end{tikzpicture}
</script>

into the expected SVG.

TikZJax works by actually running TeX itself in the browser. With web2js the Pascal source of e-TeX is compiled down to WebAssembly; running under node, the latex format is loaded (without all the hyphenation data), and

\documentclass[margin=0pt]{standalone}
\def\pgfsysdriver{pgfsys-ximera.def}
\usepackage{tikz}

is executed. Then memory is dumped; by reloading the dumped memory in the browser, it is possible to very quickly get to a point where TikZ has been loaded. (This amounts to playing the virtex and initex game, but in the browser.) By using an SVG driver for PGF along with dvi2html, the DVI output is converted to an SVG.

All of this really happens in the browser! If you go to http://tikzjax.com/ and open the web console, you'll see the familiar output

This is e-TeX, Version 3.14159265-2.6 (preloaded format=latex 2019.2.22)
kisonecat
  • 261
  • 2
  • 3
  • That is a great start! I took a look and had some success. Do you know if there is any way to capture additional \usepackage requests when using this? I was able to create any tikz construction I found except for those using additional packages, which is unfortunately a decent constraint at this point. – Alex Gold Feb 24 '19 at 07:58
  • Yes, if you have say TeXlive installed on your own machine, you can generate a coredump with all your desired packages preloaded (which is valuable because preloading packages is MUCH faster). To do this, run initex.js from https://github.com/kisonecat/web2js Another (slower) option is to add the desired files to the fake filesystem and ship those files to the browser. – kisonecat Feb 24 '19 at 14:09
  • To clarify (this is not a strong area for me), this would be a coredump that I'd make on my own machine and then upload to my server to access there? Or is that assuming that I'm running the server from my own machine in the first place? – Alex Gold Feb 24 '19 at 17:57
  • That's right -- a memory dump (made after a bunch of locally installed packages are loaded) can be made on your own computer, and then uploaded somewhere. The memory dump is small compared to a whole TeX install. I should make this easier... – kisonecat Feb 24 '19 at 21:01
  • Ah, that sounds brilliant! Is there a walkthrough somewhere showing it more step-by-step? I do casual web design but I have nothing in this realm, so I don't even know what software to use. You mentioned TeXlive, which appears to be only Windows. I'm on a Mac, but would MacText 2018 have the same functionality? That's where TeXlive directed mac users. – Alex Gold Feb 25 '19 at 04:16
  • @Alex Gold Did you ever suceeded with all your desired packages preloaded in a coredump? I'm trying to do this: [TeX SX: kisonecat/web2js or tikzjax: Can anyone explain how to change `tex.web` to get eTeX?](https://tex.stackexchange.com/questions/500370/kisonecat-web2js-or-tikzjax-can-anyone-explain-how-to-change-tex-web-to-get-e) and [TeX SX: How to make a LaTeX memory dump based on e-TeX in a JavaScript environment?](https://tex.stackexchange.com/questions/499669/how-to-make-a-latex-memory-dump-based-on-e-tex-in-a-javascript-environment?) – CarpeDiemKopi Jul 17 '19 at 17:40
  • @Alex Gold: and [git kisonecat/web2js issue: eTeX instead of TeX => ! You have to increase POOLSIZE.](https://github.com/kisonecat/web2js/issues/1) – CarpeDiemKopi Jul 17 '19 at 17:42
  • @CarpeDiemKopi I honestly haven't had a chance to try it out; I know very little of this whole area, so it's going to take some time to figure out how it all connects. It's on my list to come back to, though, and I'll check those links as well - I'll post an update when I do get a chance to try it for real! – Alex Gold Jul 19 '19 at 03:36
  • @Alex Gold: Marcel Krüger provided a solution! And I'm currently working on a workflow which implements this solution. I want TikZJax/LaTeX with at least these packages: Calculator, Calculus, TikZ and CircuiTikZ. – CarpeDiemKopi Jul 19 '19 at 23:22
  • @CarpeDiemKopi I see a post by Marcel Krüger on one of your links, but even what he's doing is way beyond me. I know the basic web languages (HTML, Javascript, PHP, CSS), but anything with installing software onto services is beyond my experience. I'm hoping to find an honest step-by-step for a layman somewhere, but so far everything I've found is written for people who already know what they're doing. Puts me at a bit of a standstill until I have a lot of hours to learn the basics in that area. – Alex Gold Jul 25 '19 at 15:15
  • 1
    kisonecat here. I'm traveling now, but I'm eager to put together an easier-to-use version of all this (certainly by the end of August). – kisonecat Jul 28 '19 at 21:21
  • @kisonecat Any updates on that front? Sounds very useful! – Alex Gold Oct 12 '19 at 21:39
  • Did someone get this working? I get this error: Access to fetch at 'http://tikzjax.com/ef253ef29e2f057334f77ead7f06ed8f22607d38.wasm' from origin 'http://127.0.0.1:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. – evolved Apr 25 '21 at 22:10
  • 1
    I probably don't have CORS set up correctly on that domain. Do you have a way you could host those assets? – kisonecat Apr 27 '21 at 18:27