Do you know the Riemann zeta function?
Especially, for s=2 we have \zeta(2) = \pi^2/6. The \zeta(s) is implemented in SpecialFunctions.jl. Indeed,
julia> using SpecialFunctions
julia> ? # switch to help mode.
help?> zeta
search: zeta Meta eta beta lbeta ncbeta let get! detach zero
zeta(s, z)
Generalized zeta function defined by
\zeta(s, z)=\sum_{k=0}^\infty \frac{1}{((k+z)^2)^{s/2}},
where any term with k+z=0 is excluded. For \Re z > 0,
this definition is equivalent to the Hurwitz zeta
function \sum_{k=0}^\infty (k+z)^{-s}.
The Riemann zeta function is recovered as
\zeta(s)=\zeta(s,1).
External links: Riemann zeta function
(https://en.wikipedia.org/wiki/Riemann_zeta_function),
Hurwitz zeta function
(https://en.wikipedia.org/wiki/Hurwitz_zeta_function)
─────────────────────────────────────────────────────────
zeta(s)
Riemann zeta function
\zeta(s)=\sum_{n=1}^\infty \frac{1}{n^s}\quad\text{for}\quad s\in\mathbb{C}.
External links: Wikipedia
(https://en.wikipedia.org/wiki/Riemann_zeta_function)
julia>
Yeah, the LaTeX syntax
\zeta(s)=\sum_{n=1}^\infty \frac{1}{n^s}\quad\text{for}\quad s\in\mathbb{C}.
corresponds to the formula.
Since many people in the scientific domain have a LaTeX parser in their eyes or their brains, “we don’t say we can’t understand it,” but it is less readable.
If we want to see these docstrings more intuitively, we need to navigate the following page, which requires an internet connection.
https://specialfunctions.juliamath.org/stable/functions_list/#SpecialFunctions.zeta-Tuple{Number}
Is there any more intuitive way to visualize docstrings, including equations written in LaTeX? Let’s find out.
Quarto + Typst → PDF
Let’s consider the following script:
import REPL # This requires to allow us to collect all docstring related to zeta
using Markdown
using SpecialFunctions
using quarto_jll
run(`rm -f example.pdf example.qmd`)
frontmatter = """
---
format:
typst:
keep-typ: false
margin:
x: 1cm
y: 1cm
mainfont: "Hiragino Mincho ProN"
fontsize: "24pt"
---
"""
md = Markdown.MD(@doc zeta).content[begin]
io = IOBuffer()
println(io, frontmatter)
for t in md.content
println(io, t)
end
str = String(take!(io))
output_text = replace(str, r"```math\s*(.*?)\s*```"m => s"$$\1$$", r"``(.*?)``" => s"$\1$")
write("example.qmd", output_text)
run(`$(quarto()) render example.qmd --to typst`)
Here, we used quarto_jll.quarto()
, which provides the quarto
command. Quarto is an open-source scientific and technical publishing system. https://quarto.org/
The Typst allows to generate PDF fast compared to pandoc.
Bang!!! We could render a PDF containing Julia docstrings that explain the zeta
function. Are you satisfied?
PDF + tdf + Terminal (supports Sixel Graphics) →
We are greedy. Do we want to display the generated PDF nicely on your terminal? So, how do we do that?
Recently, GitHub - itsjunetime/tdf: A tui-based PDF viewer is released. A tui-based PDF viewer written in Rust. Le’ts compile it using cargo
:
git clone https://github.com/itsjunetime/tdf.git
cd tdf
cargo build --release
These commands will create a binary target/release/tdf
. The tdf
binary is self-contained. We can move or install it to your preferred directory. For instance, since I installed Julia using juliaup, the environment variable PATH should contain ~/.juliaup/bin
.
So, I decided to run the mv
command:
mv target/release/tdf ~/.juliaup/bin
(Maybe we should have run cargo insatll --git https://github.com/itsjunetime/tdf.git
???)
$ tdf --help
Usage: <file> [-r <r_to_l>] [-m <max_wide>] [-h]
Arguments:
<file> PDF file to read
Options:
-r, --r-to-l <r_to_l> Display the pdf with the pages starting at the right hand size and moving left and
adjust input keys to match
-m, --max-wide <max_wide> The maximum number of pages to display together, horizontally, at a time
-h, --help Prints help
Commands:
help Print this message or the help of the given subcommand(s)
Let’s run the following command:
$ tdf example.pdf
If your terminal supports sixel graphics, the tdf
will convert the PDF as an image and display it on your terminal nicely .
Conclusion
We used Julia, and the Rust ecosystem to enable intuitive visualisation.
import REPL # allow us collect all docstring related to zeta
using Markdown
using SpecialFunctions
using quarto_jll
run(`rm -f example.pdf example.qmd`)
frontmatter = """
---
format:
typst:
keep-typ: false
margin:
x: 1cm
y: 1cm
mainfont: "Hiragino Mincho ProN"
fontsize: "24pt"
---
"""
md = Markdown.MD(@doc zeta).content[begin]
io = IOBuffer()
println(io, frontmatter)
for t in md.content
println(io, t)
end
str = String(take!(io))
output_text = replace(str, r"```math\s*(.*?)\s*```"m => s"$$\1$$", r"``(.*?)``" => s"$\1$")
write("example.qmd", output_text)
run(`$(quarto()) render example.qmd --to typst`)
#=
git clone https://github.com/itsjunetime/tdf.git
cd tdf
cargo build --release
mv target/release/tdf ~/.juliaup/bin
=#
run(`tdf example.pdf`)
Your feedback is highly appreciated.
Have a nice Julian day!