horrible_katex_hack/
lib.rs

1#![doc(html_favicon_url = "\">
2<script defer src=\"https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.js\" integrity=\"sha384-2BKqo+exmr9su6dir+qCw08N2ZKRucY4PrGQPPWU1A7FtlCGjmEGFqXCv5nyM5Ij\" crossorigin=\"anonymous\"></script>
3<script>
4document.addEventListener(\"DOMContentLoaded\", function () {
5	let to_do = [];
6	for (let e of document.getElementsByTagName(\"code\")) {
7		if (e.classList.contains(\"language-math\")) {
8			to_do.push(function () {
9				let x = document.createElement('p');
10				katex.render(e.innerText, x, {displayMode: true, throwOnError: false});
11				e.parentNode.parentNode.replaceChild(x, e.parentNode);
12			});
13		} else {
14			let n = e.nextSibling; let p = e.previousSibling;
15			if (n && p && /^\\$/.test(n.data) && /\\$$/.test(p.data)) {
16				to_do.push(function () {
17					let n = e.nextSibling; let p = e.previousSibling;
18					let x = document.createElement('span');
19					katex.render(e.innerText, x, {throwOnError: false});
20					e.parentNode.replaceChild(x, e);
21					n.splitText(1); n.remove();
22					p.splitText(p.data.length - 1).remove();
23				});
24			}
25		}
26	}
27	for (let f of to_do) f();
28});
29</script>
30<link rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/npm/katex@0.10.1/dist/katex.min.css\" integrity=\"sha384-dbVIfZGuN1Yq7/1Ocstc1lUEm+AT+/rCkibIcC/OmWo5f0EA48Vf8CytHzGrSwbQ\" crossorigin=\"anonymous")]
31
32//! This crate is an example of using $`\LaTeX`$ math with rustdoc.
33//!
34//! This demo abuses the `#[doc(html_favicon_url = ..)]` attribute to inject
35//! a KaTeX script into the generated documentation.
36//!
37//! This way, it works both on docs.rs and with `cargo doc` without extra settings.
38//!
39//! # Usage
40//!
41//! Look at the source of [`lib.rs`][1] of this crate, and copy the doc attribute
42//! containing the `<link>` and `<script>` tags.
43//!
44//! Then, write ``$`\frac 1 2 + 3`$`` for inline math, which renders as $`\frac 1 2 + 3`$.
45//!
46//! Or, write
47//!
48//! ````markdown
49//! ```math
50//! \int_{-\infty}^\infty f(x)\,dx
51//! ```
52//! ````
53//!
54//! for display math, which renders as:
55//!
56//! ```math
57//! \int_{-\infty}^\infty f(x)\,dx
58//! ```
59//!
60//! [1]: https://github.com/m-ou-se/rust-horrible-katex-hack/blob/master/src/lib.rs