I've used Racket to generate more than one dialect of TeX and a lot of HTML, and it works quite well. Actually, just about any time I write non-trivial TeX I end up generating it from Racket.
Pollen is definitely worth looking at. Even if you don't use it directly (I haven't, other than small experiments), it has some good ideas that can be imitated.
For diagrams, there are (at least) two strategies I would consider:
- You can create diagrams with
pict
, metapict
, or other Racket libraries and use racket/draw
to render them to SVG for HTML or to PDF, PostScript, or Encapsulated PostScript for use from TeX.
- If TeX-based libraries work out better for creating these diagrams (even if you are generating the TeX from a Racket front-end), you can convert them to SVG using
dvisvgm
, which is part of most TeX distributions.
For diagrams that involve text, a particular factor to consider is whether the pipeline preserves the textual data all the way to the final output or whether it converts text to mere vector shapes. In rare cases, if your target can't handle handle the text rendering you need or if there are problematic font issues, you may actually want to convert text to shapes. Usually, though, all other things being equal, it's preferable, to preserve the textual data: it lets Ctrl-F work on text in diagrams, and it can also produce much smaller files to share one reference to a font instead of several copies of letter shapes.
I'm not completely sure where things stand on this at the moment. I know PDFs generated by Slideshow (which uses pict
), at least on systems where I've tried it, manage to preserve at least most text information. I can't recall off-hand having checked that this would be preserved in a TeX-generated PDF embedding the racket/draw
-generated PDF as a figure, not with racket/draws
PS and EPS renderers. I think racket/draw
-generated SVGs convert text to shapes, but I haven't checked recently. The answers may depend on the underlying versions of Cairo, Pango, etc. and on what TeX engine you use.
I've used dvisvgm
because the state-of-the-art system for typesetting medieval plainchant is essentially a DSL that compiles to LuaTeX. My understanding is that it now is possible to have dvisvgm
preserve text data: however, I have not tried this myself yet because, the last time I was tinkering with the insides of my rendering pipeline, the version which added support specifically for LuaTeX native font definitions had not yet made it into the TeXLive distribution I was using. There are various documented caveats reflecting the complexity of the font situation in general, especially across different TeX engines. On the other hand, dvisvgm
definitely is capable of rendering figures accurately by reducing text to shapes.
In case it's useful, on this page the modern notation (five-line staff, round noteheads, generated, unfortunately, with non-free software) preserves text as text, so e.g. "O come" is selectable, whereas further down the page the medieval notation (four-line staff, square notes, generated with an older dvisvgm
) does not, so e.g. "et nunc et semper" is not selectable.
Incidentally, I've found latexmk
useful to manage potentially needing to run LuaLaTeX several times.
For 3D diagrams in Racket, the only thing that comes to mind off-hand is pict3d
. I don't remember what it provides by way of rendering or for inserting 3D imagery from elsewhere, though, and the build is currently failing (maybe just because the tests take too long?).