What I want to do is to convert a .tex file, containing several TikZ figures to a .docx-file using pandoc. I have tried to follow the pandoc documentation and to use lua filters to achieve this. Problem: whenever I use the lua filter, a file is generated that contains only the title of my .tex file and all the rest of the file is omitted. I do not see the TikZ figures either.
This is my input for pandoc in the commandline:
pandoc --from latex+raw_tex --lua-filter=tikz.lua -s file.tex -o test.docx
My lua filter file (tikz.lua) is changed from the original filter suggested by pandoc, on the basis of the advice given in this post. I have adopted the suggested changes there one-on-one to this file:
local function file_exists(name)
local f = io.open(name, 'r')
if f ~= nil then io.close(f); return true
else return false end
end
function RawBlock(el)
-- Don't alter element if it's not a tikzpicture environment
if not el.text:match'^\\begin{tikzpicture}' then
return nil
-- Alternatively, parse the contained LaTeX now:
-- return pandoc.read(el.text, 'latex').blocks
end
local fname = pandoc.sha1(el.text) .. ".png"
if not file_exists(fname) then
tikz2image(el.text, fname)
end
return pandoc.Para({pandoc.Image({}, fname)})
end
--- Create a standalone LaTeX document which contains only the TikZ picture.
--- Convert to png via Imagemagick.
local function tikz2image(src, outfile)
local tmp = os.tmpname()
local tmpdir = string.match(tmp, "^(.*[\\/])") or "."
local f = io.open(tmp .. ".tex", 'w')
f:write("\\documentclass{standalone}\n")
-- include all packages needed to compile your images
f:write("\\usepackage{tikz}\n\\usepackage{stanli}\n")
f:write("\\begin{document}\n")
f:write(src)
f:write("\n\\end{document}\n")
f:close()
os.execute("pdflatex -output-directory " .. tmpdir .. " " .. tmp)
os.execute("convert " .. tmp .. ".pdf " .. outfile)
os.remove(tmp .. ".tex")
os.remove(tmp .. ".pdf")
os.remove(tmp .. ".log")
os.remove(tmp .. ".aux")
end
pdflatex and pdf2svg are both installed, as well as ImageMagick.
In short, I should be good to go but something goes wrong and all I see is my title. Any advice would be really welcome!
Here is my code:
\documentclass{book}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
\begin{tikzpicture}
\draw (0,0) -- (4,0);
\end{tikzpicture}
\end{document}
When rendering as a .pdf file in TexStudio, the TikZ figure shows up. When using pandoc, nothing happens.
Hopefully you can spot something I have missed!
tikz.lua
as well