Crates.io | rust-latex-doc-minimal-example |
lib.rs | rust-latex-doc-minimal-example |
version | 0.2.0 |
source | src |
created_at | 2020-05-08 08:57:33.878751 |
updated_at | 2020-05-09 11:18:49.058598 |
description | Minimal crate example for getting latex in docs. |
homepage | https://github.com/victe/rust-latex-doc-minimal-example |
repository | https://github.com/victe/rust-latex-doc-minimal-example |
max_upload_size | |
id | 238836 |
size | 888,786 |
Documenting code in Rust
is straightforward and appealing, but you can not write mathematical expressions.
The solution is to include in the final HTML doc page a script to parse the mathematical expressions when rendering the page.
We use Latex for the mathematical syntax and Katex for parsing the expressions in the final HTML file. The parser script get Katex
from CDN and configure escape characters to use in documentation code.
Now you can do this:
/// $$E = mc^2 $$
/// $$m = \frac{m_0}{\sqrt{1-\frac{v^2}{c^2}}}$$
fn energy(v: Velocity) -> Energy {...}
As a result, see the documentation page of this repo example at docs.rs.
rustdoc
has command-line arguments for including HTML in the header, before the content and after the content. We use --html-in-header
because we need to link to the CDN for getting Katex
.
For passing --html-in-header
to cargo doc
command there are two options:
Linux
RUSTDOCFLAGS="--html-in-header src/docs-header.html" cargo doc
Windows cmd
set RUSTDOCFLAGS=--html-in-header src\docs-header.html
cargo doc
Windows PowerShell
$env:RUSTDOCFLAGS="--html-in-header .\src\docs-header.html"
cargo doc
build.rustdocflags
config value in .cargo\config.toml
(in this Repo is configured)[build]
rustdocflags = [ "--html-in-header", "./src/docs-header.html" ]
The crate must be a lib
. If it is only a bin
, docs.rs do not build the documentation.
We need to add the header file ubication in the cargo manifest:
[package.metadata.docs.rs]
rustdoc-args = [ "--html-in-header", "./src/docs-header.html" ]
If your crate is a bin
or you don't want to publish your crate at crates.io, you can always publish the target/doc
directory at your convenience.
In this repo, we use github pages in the docs
directory. In this case, just use:
rm -rf docs/ && cargo clean --doc && cargo doc && cp -r target/doc/ docs/
If you publish your crate to crates.io set the documentation
entry in the cargo manifest to your final URL destination. In this crate, this entry should look like this:
# Cargo.toml
[package]
...
documentation = "https://victe.github.io/rust-latex-doc-minimal-example/rust_latex_doc_minimal_example/"
...