rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust

Home Page:https://rust-lang.github.io/mdBook/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Embed LaTeX from pdflatex as SVG

MultisampledNight opened this issue · comments

This is not really a bug or the like, just a possible way of embedding LaTeX in mdbook as an SVG which actually responds to theme changes:

Instructions

  1. Install the texlive suite (or something else providing pdflatex), pdf2svg, Python and mdbook-cmdrun.

  2. Add [preprocessor.cmdrun] in book.toml if it isn't in there already.

  3. Get my weird conversion script which takes care of some mdbook glue and put it into the book's root folder.

  4. Insert this anytime to include a LaTeX document (replace whatever.tex with the actual filename to use, relative from the directory the markdown files reside in)

    <div style="display: flex; flex-direction: row; justify-content: center">
      <!-- cmdrun python ../latex-to-svg.py whatever.tex -->
    </div>
  5. (optional) Adjust your LaTeX file to limit output to contents only, avoiding linenumbers and page padding and the like

    % somewhere in the preamble
    \usepackage[active, tightpage]{preview}
    \setlength\PreviewBorder{0.1cm} % modify for padding changes (should probably be done in the svg itself, but eh)
    \usepackage[mag=1700]{geometry} % modify for scale changes
    
    % ...
    
    \begin{document}
    \begin{preview}
    
    % actual document here
    
    \end{preview}
    \end{document}

Notes

This is hacky at its best and unreliable at its worst, but it worked in my case for including a context-free grammar already written in LaTeX while automatically respecting the currently chosen mdbook theme.

Yes, emitting the entire SVG directly into the markdown is intentional. With ![]() I couldn't get the SVG in the "shadow DOM" (???) to make use of mdbook's theme.

Originally I asked this on the Rust Community Server and pinged @Dylan-DPC to ask how this might be achievable at all, but they only responded with me needing to try it out. Afterwards they suggested me to open an issue since mdbook doesn't have any communication channels, which I'm doing here (you're doing a great job, dpc <3).

I'd be happy for any kinds of improvements or suggestions.

Why not mdbook-katex?

Not really doing the job for more than just equations, I fear.