aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorMatthew Doty <matthew.wampler.doty@gmail.com>2019-04-16 00:39:03 -0400
committerJohn MacFarlane <jgm@berkeley.edu>2019-04-15 21:39:03 -0700
commit32e358bfe920b6a0da34c2f3df29092452f27983 (patch)
tree171d5dc764bc561772bcab0daa9052b462ccac2b /doc
parente409509a681ee75c6d3eec0b913d40c26aeaabb2 (diff)
downloadpandoc-32e358bfe920b6a0da34c2f3df29092452f27983.tar.gz
Improved sample lua tikz filter in lua-filters docs (#5445)
There are three changes: - It only processes elements which begin with \begin{tikzpicture} - It uses pdf2svg instead of imagemagick to preserve fidelity - The images produced have transparent backgrounds
Diffstat (limited to 'doc')
-rw-r--r--doc/lua-filters.md33
1 files changed, 21 insertions, 12 deletions
diff --git a/doc/lua-filters.md b/doc/lua-filters.md
index 017a5ddb9..4bc8bcb54 100644
--- a/doc/lua-filters.md
+++ b/doc/lua-filters.md
@@ -550,8 +550,8 @@ end
This filter converts raw LaTeX tikz environments into images. It
works with both PDF and HTML output. The tikz code is compiled
-to an image using `pdflatex`, and the image is converted (if
-necessary) from pdf to png format using ImageMagick's `convert`,
+to an image using `pdflatex`, and the image is converted from pdf
+to svg format using [`pdf2svg`](https://github.com/dawbarton/pdf2svg),
so both of these must be in the system path. Converted images
are cached in the working directory and given filenames based on
a hash of the source, so that they need not be regenerated each
@@ -563,7 +563,7 @@ local function tikz2image(src, filetype, outfile)
local tmp = os.tmpname()
local tmpdir = string.match(tmp, "^(.*[\\/])") or "."
local f = io.open(tmp .. ".tex", 'w')
- f:write("\\documentclass{standalone}\n\\usepackage{tikz}\n\\begin{document}\n")
+ f:write("\\documentclass{standalone}\n\\usepackage{xcolor}\n\\usepackage{tikz}\n\\begin{document}\n\\nopagecolor\n")
f:write(src)
f:write("\n\\end{document}\n")
f:close()
@@ -571,7 +571,7 @@ local function tikz2image(src, filetype, outfile)
if filetype == 'pdf' then
os.rename(tmp .. ".pdf", outfile)
else
- os.execute("convert " .. tmp .. ".pdf " .. outfile)
+ os.execute("pdf2svg " .. tmp .. ".pdf " .. outfile)
end
os.remove(tmp .. ".tex")
os.remove(tmp .. ".pdf")
@@ -580,9 +580,9 @@ local function tikz2image(src, filetype, outfile)
end
extension_for = {
- html = 'png',
- html4 = 'png',
- html5 = 'png',
+ html = 'svg',
+ html4 = 'svg',
+ html5 = 'svg',
latex = 'pdf',
beamer = 'pdf' }
@@ -596,13 +596,22 @@ local function file_exists(name)
end
end
+local function starts_with(start, str)
+ return str:sub(1, #start) == start
+end
+
+
function RawBlock(el)
- local filetype = extension_for[FORMAT] or "png"
- local fname = pandoc.sha1(el.text) .. "." .. filetype
- if not file_exists(fname) then
- tikz2image(el.text, filetype, fname)
+ if starts_with("\\begin{tikzpicture}", el.text) then
+ local filetype = extension_for[FORMAT] or "svg"
+ local fname = pandoc.sha1(el.text) .. "." .. filetype
+ if not file_exists(fname) then
+ tikz2image(el.text, filetype, fname)
+ end
+ return pandoc.Para({pandoc.Image({}, fname)})
+ else
+ return el
end
- return pandoc.Para({pandoc.Image({}, fname)})
end
```