Skip to content

Insert KaTeX autorender script into rustdoc

License

Notifications You must be signed in to change notification settings

termoshtt/katexit

Repository files navigation

Crate docs.rs

Insert KaTeX autorender script into rustdoc

Usage

#[cfg_attr(doc, katexit::katexit)]
/// We can write $\LaTeX$ expressions
///
/// Display style
/// -------------
///
/// $$
/// c = \\pm\\sqrt{a^2 + b^2}
/// $$
pub fn my_func() {}

See rendered result on docs.rs.

On nightly rustc, you can use #![katexit::katexit] for module level document:

#![cfg_attr(doc, feature(prelude_import, custom_inner_attributes))]
#![cfg_attr(doc, katexit::katexit)]
//! Module level document example with $\KaTeX$!

Be sure that cargo test will runs rustdoc for executing doc-test, and thus requires nightly compiler. See katexit-example-nightly for detail.

How it works

#[katexit] proc-macro inserts KaTeX autorender script as #[doc = "{script}"]. Since the markdown to HTML translator of rustdoc do not touch HTML partitions embedded in markdown, they will be passed as it is to the browser. The autorender script starts rendering the math expression enclosed by $ written in the document section when you open the page generated by rustdoc, i.e. this does not work offline.

Links