Recently, I have been having some issues with TikZ in LaTeX. I want to convert my PhD dissertation, written in LaTeX, to .docx using pandoc. I am able to use lua filters to have pandoc convert TikZ images, thanks to help here.
However, a small problems remains. In the .docx file, the TikZ images are not centered. Nor are my captions included: it seems like pandoc simply ignores \begin{figure} whereafter I have included these commands.
Question
How to give a caption to and how to center TikZ figures in a .docx file converted with pandoc?
I am probably looking right over the solution, but as I am inexperienced with programming I do not know how to fix this.
Here is my .tex file:
\documentclass[a4paper,12pt]{book}
\usepackage{graphicx}
\usepackage[english]{babel}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\draw circle(5cm);
\end{tikzpicture}
\caption{Admire my circle!}
\end{figure}
\end{document}
Here is my .lua file, taken from here, which functions as a filter:
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
--- 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 tmpdir = "."
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\usetikzlibrary{arrows.meta,positioning}\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 " .. "-colorspace RGB " .. outfile)
os.execute("pdftoppm -png " .. tmp .. ".pdf " .. "> " .. outfile)
os.remove(tmp .. ".tex")
os.remove(tmp .. ".pdf")
os.remove(tmp .. ".log")
os.remove(tmp .. ".aux")
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
Here is the command I enter in the terminal:
pandoc -s --from latex+raw_tex --lua-filter=tikz.lua test.tex -o test.docx
tex4ht: http://www.tug.org/tex4ht/ – DG' Oct 27 '20 at 09:47