pandoc-ext / diagram

Generate diagrams from embedded code; supports Mermaid, Dot/GraphViz, PlantUML, Asymptote, and TikZ.
MIT License
53 stars 9 forks source link

does this work with "pandoc --from latex"? #5

Closed jwaldmann closed 2 months ago

jwaldmann commented 1 year ago

Hi. I can run the diagram.lua filter on input-tikz.md from your test directory. Now I want to apply it to produce html from a latex document. I am adding --from latex, but the output contains Unknown environment 'tikzpicture'.

pandoc --from latex --to slidy \
--variable "slidy-url:...my-local-installation.../Slidy2" \
--standalone \
--mathjax="...my-local-installation.../MathJax-3.2.2/es5/tex-mml-chtml.js" \
--slide-level=2 \
--lua-filter=...my-local-installation.../diagram/diagram.lua 

with

$ pandoc --version
pandoc 3.1
Features: -server +lua
Scripting engine: Lua 5.4
tarleb commented 1 year ago

The intended use for this filter is with Markdown, which should probably be stated more clearly in the README. However, I can see how raw LaTeX input would be useful even with Markdown, so I'll consider adding support for this.

Workaround: run the below filter before diagram.lua (untested):

function RawBlock (raw)
  local tikz = raw.text:match '\\begin%{tikzpicture%}.*\\end%{tikzpicture%}'
  if tikz then
    return pandoc.CodeBlock(tikz, {class="tikz"})
  end
end
jwaldmann commented 1 year ago

Thanks for the quick response. I cannot make your code work. I put it in a file diagram/tikz.lua, then

pandoc --verbose  --lua-filter=diagram/tikz.lua --lua-filter=diagram/diagram.lua --standalone --embed-resources  pic.tex

with this in pic.tex

\documentclass{article}
\usepackage{pgf}
\usepackage{tikz}
\usetikzlibrary{automata}
\usetikzlibrary {arrows.meta,automata,positioning}
\begin{document}
\begin{tikzpicture} [      ]
  \node[state,initial,accepting]  (q_0) {$q_0$};
  \path[->] (q_0) edge [loop left] node        {$b$} ();
\end{tikzpicture}
\end{document}

but it gives me

[INFO] Could not load include file pgf.sty at pic.tex line 2 column 17
[INFO] Could not load include file tikz.sty at pic.tex line 3 column 18
[INFO] Skipped '\usetikzlibrary{automata}' at pic.tex line 4 column 26
[INFO] Skipped '\usetikzlibrary {arrows.meta,automata,positioning}' at pic.tex line 5 column 51
[INFO] Skipped '\begin{tikzpicture} [      ]
    \node[state,initial,accepting]  (q_0) {$q_0$};
    \path[->] (q_0) edge [loop left] node        {$b$} ();
  \end{tikzpicture}' at pic.tex line 7 column 20
[INFO] Running filter diagram/tikz.lua
[INFO] Completed filter diagram/tikz.lua in 3 ms
[INFO] Running filter diagram/diagram.lua
[INFO] Completed filter diagram/diagram.lua in 3 ms

I am getting empty output with

$ pandoc -t native  pic.tex

while I expected something like

$ pandoc -t native  input-tikz.md
...
    , Str "figure."
    ]
, CodeBlock
    ( "" , [ "tikz" ] , [] )
...

Your filter assumes we get a RawBlock? There is none. In my real use case, tikz is often inside math mode, the AST looks like this

    , [ Para
          [ Str "Bsp:"
          , Space
          , Math
              InlineMath
              "A=  \\scalebox{0.8}{\n      \\begin{tikzpicture}\n        [node distance=2cm\n   
tarleb commented 1 year ago

Ah yes: pandoc will only produce a RawBlock when called with --from=latex+raw_tex. But even then we still have a problem, because the \usetikzlibrary commands are not passed to the diagram generator unless they are added inside the tikzpicture environment. We'll need to add an option that allows to set additional packages globally.

I'm not sure yet about TikZ in math. I'll probably treat it as out-of-scope for this filter, as the TeXMath package used in pandoc doesn't support images in math anyway. But it might be possible to use it with something like math2svg.

jwaldmann commented 1 year ago

Thanks! I will look into math2svg.