What is offered by this crate
The crate provides include_display_mode_tex! macro that allows to embed tex formulae
in the documentation generated by rustdoc via cargo doc --no-deps.
Requirements
- Nightly compiler
#![feature(proc_macro_span)] is absolutely necessary
to imitate the behavior of include_str.
Running rustup default nightly in the terminal will install the nightly version of the tools (cargo, rustc, and so on). Also, it will switch the corresponding commands to use the nightly version. If you want to go back to the stable version, issue the rustup default stable command. Credit to O'Reilly.
What is LaTeX?
LaTeX is a language for typesetting documents, especially scientific papers, and a document preparation system.
Example of .tex code
% ...
\glossaryentry
\begin{adjustwidth}
: \\
: \\
:
\begin{adjustwidth} \leavevmode
\begin{framed}
For two \hyperlink and of the same \hyperlink , the \beingdefined (or ) is a \hyperlink of the same \hyperlink as the operands, with elements given by
\begin{equation*}\end{equation*}
Source: .
\end{framed}
\begin{framed}
Let and be \hyperlink with entries in . The \beingdefined is defined by for all , . \\ \vspace
Source: .
\end{framed}
\end{adjustwidth}
\end{adjustwidth} \vspace
% ...
Output

Example on docs.rs
Now and future
In its current implementation include_display_mode_tex! macro merely turns
the contents of .tex files into Markdown with raw LaTeX formulae. For the formulae to be displayed as such and not LaTeX syntax, Markdown with raw LaTeX must be rendered with some library, such as
KaTeX or
MathJax. Such approach burdens the crate
with extra complexity of .cargo config and the requirement to build the documentation via
cargo doc --no-deps instead of cargo doc but it works.
There is also katex crate that theoretically can allow
to render HTML when the documentation is generated. A PR with such functionality will be very
welcome (though feature-gated for backward compatibility).
Setting up the crate
The following steps will allow to render .tex included via include_display_mode_tex!
with KaTeX renderer:
- Create
.cargodirectory in the crate root (the directory containingCargo.toml) - In
.cargo, addconfig.tomlwith the following contents:
[]
= [ "--html-in-header", "./src/html/docs-header.html" ]
- Add these two lines to your
Cargo.toml
[]
= [ "--html-in-header", "./src/html/docs-header.html" ]
- Add
include_display_mode_texas[dependency], not[dev-dependency] - Create
./src/htmldirectory (where./src/htmlis a relative path from the crate root) - In
./src/htmladddocs-header.htmlwith the following contents:
Code example
use include_display_mode_tex::include_display_mode_tex;
#[doc = include_display_mode_tex!("./tex/example.tex")]
# let s = 0;
Notice that the path is relative not to the crate root but to the call site (just like
for core::include_str) and that
the documentation must be built with
cargo doc --no-deps
Sources of inspiration
Other include* macros:
Special thanks to victe for providing
rust-latex-doc-minimal-example