# Building Julia documentation using Typst

**URL:** https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498
**Category:** Internals & Design
**Tags:** documenter
**Created:** [April 15, 2023, 12:03am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498 "2023-04-15T00:03:43Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 15, 2023, 12:03am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/1 "2023-04-15T00:03:43Z")

</div>

I just wrote a Typst backend for `Documenter.jl` and managed to build the latest Julia documentation.

> **[juliadoc.pdf](https://drive.google.com/file/d/1IlqEI5b4Qc0CjuJ5u0l0Ma_MymRpWcrD/view?usp=sharing)**
>
> Google Drive file.

Footnotes are not supported yet, while math equations are kept as is since Typst uses a different grammar and there is no translator available at the moment.

A big advantage is the compilation speed. On my computer, the whole documentation can be compiled within ~13 min. And I believe there is still much room for improvement.

Official Typst does not support syntax highlighting for Julia yet, I have made a [PR](https://github.com/typst/typst/pull/771), but it is currently blocked by upstream packages. There are still some issues with syntax highlighting, e.g., in some cases, all code after `#` are taken as comments.

### Resources:

- [My Documenter.jl fork](https://github.com/lucifer1004/Documenter.jl/tree/typst-release)
  - [TypstWriter.jl](https://github.com/lucifer1004/Documenter.jl/blob/typst-release/src/typst/TypstWriter.jl)

---

<div class="post-metadata">

### Author: ![miguelraz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miguelraz/32/631_2.png) [@miguelraz](https://discourse.julialang.org/u/miguelraz)
#### Post date: [April 15, 2023, 3:49am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/2 "2023-04-15T03:49:25Z")

</div>

Awesome stuff!  
Don’t know how this initiative will turn out but I’m glad to see Typst take flight.

Quick nit:  
this line will allocate furiously.

> <https://github.com/lucifer1004/Documenter.jl/blob/99239f837a63adf503ef3643aa0d12ba29aa7341/src/typst/TypstWriter.jl#L126>

I think I would try

```julia
header_text = string("#extended_heading(level: ", depth, " within-block: false, [", title," )])\n"

```

Because interpolation always copies. (If the copies are actually very heavy I’d stick the assignment in both places and inside the most nested loop, but YMMV.)

That said… I look forward to the compilation not taking ~13mins 😅 Happy perf hunting!

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 15, 2023, 4:22am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/3 "2023-04-15T04:22:06Z")

</div>

Actually, 13mins is already much faster than the LaTeX backend. But there is definitely much room for perf hunting.

Also, the bottleneck is the final compilation (.typ → .pdf), and the first step (MarkdownAST → .typ) is fast enough.

---

<div class="post-metadata">

### Author: ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)
#### Post date: [April 16, 2023, 5:54pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/4 "2023-04-16T17:54:56Z")

</div>

Would it be possible to take advantage of Typst’s incremental compilation abilities and launch the compilation before the `.typ` document is finalized?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [April 16, 2023, 6:56pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/5 "2023-04-16T18:56:56Z")

</div>

Sorry if this is a daft question: what is the point of another backend (in addition to those that already exist)? These all output the same documents? Don’t they?

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [April 16, 2023, 8:19pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/6 "2023-04-16T20:19:13Z")

</div>

> [@PetrKryslUCSD](#):
>
> what is the point of another backend

> [@lucifer1004](#):
>
> A big advantage is the compilation speed.

.

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 16, 2023, 11:50pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/7 "2023-04-16T23:50:36Z")

</div>

This is unnecessary since the generation of the .typ file is much faster than the compilation.

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 16, 2023, 11:52pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/8 "2023-04-16T23:52:00Z")

</div>

Compilation of the PDF documentation took a lot of time in Julia’s CI. This is the reason why I started this experiment.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [April 17, 2023, 12:55am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/9 "2023-04-17T00:55:07Z")

</div>

Interesting. Why exactly is it that Typst is much faster than Latex to produce PDF?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [April 17, 2023, 1:16am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/10 "2023-04-17T01:16:00Z")

</div>

because it’s not LaTeX (doesn’t go through TeX at all)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [April 17, 2023, 1:51am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/11 "2023-04-17T01:51:42Z")

</div>

I realize that. But why is the compilation into PDF faster?

---

<div class="post-metadata">

### Author: ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)
#### Post date: [April 17, 2023, 2:42am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/12 "2023-04-17T02:42:47Z")

</div>

> [@PetrKryslUCSD](#):
>
> Why exactly is it that Typst is much faster than Latex to produce PDF?

## Why is LaTeX so slow?

> - Takes about **40 minutes** on my computer.
> - It takes about **30 minutes** in GitHub action.

> If all tex files are exported and compiled 3 times in draft mode using `lualatex` , it only takes about 4 minutes.  
> This means that about 90% of the time is spent on _generating code highlights via the minted macro package._

> <https://github.com/JuliaLang/julia/issues/48896>
>
> \## build step
> \*\*deps\*\*
> \- LaTeX: https://www.latex-project.org/get/
> \- \[pygment…ize\](https://pygments.org/): \`pip install Pygments\`
> 
> \`\`\`sh
> make docs
> make -C doc pdf # Takes a looooong time
> \`\`\`
> 
> \<details\>
> \<summary\>logs\</summary\>
> 
> \`\`\`
> $ make -C doc pdf
> make: Enter the directory “/cygdrive/v/julia/doc”
> /cygdrive/v/julia/deps/tools/jlchecksum "/cygdrive/v/julia/deps/srccache/UnicodeData-13.0.0.txt"
> cp "/cygdrive/v/julia/deps/srccache/UnicodeData-13.0.0.txt" UnicodeData.txt
> Building PDF documentation.
> /cygdrive/v/julia/usr/bin/julia --startup-file=no --color=yes \`cygpath -w /cygdrive/v/julia/doc/make.jl\` -- pdf linkcheck= doctest= buildroot=\`cygpath -w /cygdrive/v/julia\` texplatform= revise=
> ┌ Warning: The active manifest file has dependencies that were resolved with a different julia version (1.9.0-DEV). Unexpected behavior may occur.
> └ @ V:\\julia\\doc\\Manifest.toml:0
> \[ Info: SetupBuildDirectory: setting up build directory.
> \[ Info: Doctest: skipped.
> \[ Info: ExpandTemplates: expanding markdown templates.
> \[ Info: CrossReferences: building cross-references.
> \[ Info: CheckDocument: running document checks.
> \[ Info: Populate: populating indices.
> \[ Info: RenderDocument: rendering document.
> \[ Info: LaTeXWriter: creating the LaTeX file.
> \[ Info: LaTeXWriter: using latexmk to compile tex.
> \`\`\`
> \</details\>
> 
> 
> \## issue
> \- Takes about \*\*40 minutes\*\* on my computer.
> windows 10 + cygwin + MiKTeX (Updated to 2023-03-04)
> \- It takes about \*\*30 minutes\*\* in GitHub action.
> ref: \[Julia PDF builds (nightly)\](https://github.com/JuliaLang/docs.julialang.org/actions/runs/4328282863/jobs/7557803696)
> 
> I think compile time \< 10min is acceptable.
> 
> If all tex files are exported and compiled 3 times in draft mode using \`lualatex\`, it only takes about 4 minutes.
> This means that about 90% of the time is spent on generating code highlights via the minted macro package.
> 
> \> Note: "draft mode" means that there is no code highlighting, all code is black. The rest of the document remains unchanged.
> \>
> \> cmd x3: \`lualatex -interaction=batchmode -halt-on-error -shell-escape -recorder "TheJuliaLanguage.tex"\`
> 
> 
> \## Possible solution
> 
> —— using \`pythontex\` package
> https://github.com/gpoore/pythontex
> 
> \> I answered this question, and in the process did some informal benchmarking of pythontex vs. minted. Summary: on my system, 40 simple uses of \\mint take about 12 s to compile, and 80 uses needs about 23 s. With pythontex, 40 equivalent commands need 3.5 s, 80 uses is almost the same, and you only get to around 23 s for 8000 uses. All the pythontex numbers are actually overestimates for typical use, since highlighted results are cached. 
> \> —— https://tex.stackexchange.com/a/61342

* * *

## Why typst is fast?

> - Fast compile times thanks to incremental compilation
> 
> - **Performance through Incrementality:** All Typst language features must accommodate for incremental compilation. Luckily we have [`comemo`](https://github.com/typst/comemo/), a system for incremental compilation which does most of the hard work in the background.  
> [GitHub - typst/typst: A markup-based typesetting system that is powerful and easy to learn.](https://github.com/typst/typst#design-principles)

More incremental compilation-related descriptions can be found in `ARCHITECTURE.md` by searching for `Incremental`:  
[https://github.com/typst/typst/blob/main/ARCHITECTURE.md](https://github.com/typst/typst/blob/main/ARCHITECTURE.md)

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [April 17, 2023, 4:36am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/13 "2023-04-17T04:36:32Z")

</div>

May be i am misunderstanding, but it sounded as if right now no coloring of code was done with Typst. And, the source code coloring with Pygments was the reason the build took so long. So how will things be better with Typst once one throws the syntax coloring in?

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 17, 2023, 7:02am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/14 "2023-04-17T07:02:51Z")

</div>

Typst uses [syntect](https://github.com/trishume/syntect) to do syntax highlighting, which is faster.

---

<div class="post-metadata">

### Author: ![bilderbuchi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bilderbuchi/32/13562_2.png) [@bilderbuchi](https://discourse.julialang.org/u/bilderbuchi)
#### Post date: [April 17, 2023, 9:14am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/15 "2023-04-17T09:14:01Z")

</div>

Did you benchmark that? Even the author seems to expect the speed of syntect and pygments to be comparable: [Performance comparison · Issue #372 · trishume/syntect · GitHub](https://github.com/trishume/syntect/issues/372#issuecomment-917669128)

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 17, 2023, 9:25am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/16 "2023-04-17T09:25:23Z")

</div>

No, I have not benchmarked the two tools myself. There are cases where `syntect` has big advantages. For example,

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/c/fc402f96b482172410c305ccfca251154ee21193.png)

`bat` uses `syntect` for syntax highlighting.

---

<div class="post-metadata">

### Author: ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)
#### Post date: [April 17, 2023, 10:06am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/17 "2023-04-17T10:06:28Z")

</div>

Just confirmed that the bottleneck is not syntax highlighting for typst. After turning off the outline, header, and footers (which are all implemented by myself), the compilation time is only 49s.

---

<div class="post-metadata">

### Author: ![togo59](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/togo59/32/4138_2.png) [@togo59](https://discourse.julialang.org/u/togo59)
#### Post date: [April 18, 2023, 12:47pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/18 "2023-04-18T12:47:20Z")

</div>

Another way of doing something? So what? That’s the whole point of progress, to explore new tools, find out their shortcomings, cure them if poss. and then we all have a choice: get rid or adopt! Brilliant! Long-live lateral exploration! 😀

---

<div class="post-metadata">

### Author: ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)
#### Post date: [April 19, 2023, 7:33pm UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/19 "2023-04-19T19:33:47Z")

</div>

Faster is very nice. But another advantage that is even more important for me is that Typst math syntax is a lot more readable uncompiled. Examples: there are no backslashes, no curly-braces, and `a/b` turns into a pretty fraction automatically. Why is that important?

Many docstrings include LaTeX to render nicely in the documentation. But often it is the only representation of some key mathematical relation, and it is really hard to read when you get the docstring in the REPL (or with mouse-over from a language server).

So math that both compiles pretty and is readable uncompiled for docstrings is something I would really love, and something Typst brings to the table.

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [April 20, 2023, 3:28am UTC](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498/20 "2023-04-20T03:28:24Z")

</div>

> [@TheLateKronos](#):
>
> Many docstrings include LaTeX to render nicely in the documentation. But often it is the only representation of some key mathematical relation, and it is really hard to read when you get the docstring in the REPL (or with mouse-over from a language server).

That’s why I try to render the LaTeX in the REPL:

```julia

julia> function f end
f (generic function with 0 methods)

julia> @doc """
       Some function

       ``\\frac{1}{2}\\lvert\\alpha\\rvert + \\beta^2 - \\varphi``
       """ f
f

help?> f
search: f fd for fma fld fit fld1 fill fdio fit! frexp foldr foldl flush floor float first fill! fetch fldmod

  Some function

  ¹/₂|α| + β² − φ

```

[Next page](https://discourse.julialang.org/t/building-julia-documentation-using-typst/97498.md?page=2)
