Hi!
This is my first post to this group. I have been using AsciiDoc for a while
and I'm actually writing more documents now than I did before because it's
so much easier with AsciiDoc. More fun as well :-)
I wanted better looking and scalable Latex math so I hacked the Latex
filter a bit. I have tried the different options for getting math formulas
into documents and the filter approach seem to be the most portable one if
one wants to generate multiple document formats.
The first annoying problem I encountered was that images did not regenerate
when I changed the DPI. This was simple to fix by just including the dpi
value in the generated MD5-sum.
The generated PNG images does not scale well when zooming in on a web page
for example. This is why I have now added support for generating SVG
images, which are scalable. The dvisvgm utility is used to do that.
Generating PNG images is still the default unless the "imgfmt" attribute is
set to "svg". I also added a "latex-imgfmt" attribute that do the same
thing. The thought was that the former (the shorter one) can be used to
change the format of one image while the latter (the longer one) can be set
globally. I don't know if this is the right thing to do. Please tell me if
there is a better way of doing it.
After applying the supplied patch the filename of the filter script should
be changed from latex2png.py to latex2img.py. I have changed all references
in both the script file and in the conf file.
Usage may look like this:
:latex-imgfmt: svg
[latex]
--
\begin{equation*}
X(\omega) &= \sum_{-\infty}^\infty x(n) e^{-j \omega n}
\end{equation*}
--
I hope you like it :-)
Regards,
Tobias
--
You received this message because you are subscribed to the Google Groups
"asciidoc" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/asciidoc.
For more options, visit https://groups.google.com/groups/opt_out.
diff -r 7863b0a0ab0d -r 8d6acce76909 filters/latex/latex-filter.conf
--- a/filters/latex/latex-filter.conf Wed Nov 27 21:59:21 2013 +1300
+++ b/filters/latex/latex-filter.conf Sun Dec 15 16:34:13 2013 +0100
@@ -9,10 +9,10 @@
# When the filter output image is data-uri encoded write it to the indir
# (instead of the outdir) so that encoder can find it.
ifndef::data-uri[]
-latex-style=template="latex-block",subs=(),posattrs=("style","target","dpi"),filter='latex2png.py -m{verbose? -v}{dpi? -D {dpi}} -o "{outdir={indir}}/{imagesdir=}{imagesdir?/}{target}" -'
+latex-style=template="latex-block",subs=(),posattrs=("style","target","dpi"),filter='latex2img.py -m{verbose? -v}{dpi? -D {dpi}}{imgfmt? -f {imgfmt}} -o "{outdir={indir}}/{imagesdir=}{imagesdir?/}{target}" -'
endif::data-uri[]
ifdef::data-uri[]
-latex-style=template="latex-block",subs=(),posattrs=("style","target","dpi"),filter='latex2png.py -m{verbose? -v}{dpi? -D {dpi}} -o "{indir={outdir}}/{imagesdir=}{imagesdir?/}{target}" -'
+latex-style=template="latex-block",subs=(),posattrs=("style","target","dpi"),filter='latex2img.py -m{verbose? -v}{dpi? -D {dpi}}{imgfmt? -f {imgfmt}} -o "{indir={outdir}}/{imagesdir=}{imagesdir?/}{target}" -'
endif::data-uri[]
[blockdef-open]
@@ -25,4 +25,18 @@
template::[latex-filter-style]
[latex-block]
-template::[filter-image-blockmacro]
+template::[latex-filter-image-blockmacro]
+
+[latex-filter-image-blockmacro]
+# Synthesize missing target attribute for filter generated file names.
+# The tag split | ensures missing target file names are auto-generated
+# before the filter is executed, the remainder (the [image-blockmacro])
+# is excuted after the filter to ensure data URI encoding comes after
+# the image is created.
+# This template replaces the filter-image-blockmacro template so that
+# we can generate SVG images from LaTeX code.
+{target%}{counter2:target-number}
+{imgfmt%}{set2:imgfmt:{latex-imgfmt=png}}
+{target%}{set2:target:{docname}__{target-number}.{imgfmt}}
+|
+template::[image-blockmacro]
diff -r 7863b0a0ab0d -r 8d6acce76909 filters/latex/latex2png.py
--- a/filters/latex/latex2png.py Wed Nov 27 21:59:21 2013 +1300
+++ b/filters/latex/latex2png.py Sun Dec 15 16:34:13 2013 +0100
@@ -1,29 +1,31 @@
#!/usr/bin/env python
'''
NAME
- latex2png - Converts LaTeX source to PNG file
+ latex2img - Converts LaTeX source to PNG file
SYNOPSIS
- latex2png [options] INFILE
+ latex2img [options] INFILE
DESCRIPTION
This filter reads LaTeX source text from the input file
INFILE (or stdin if INFILE is -) and renders it to PNG image file.
Typically used to render math equations.
- Requires latex(1), dvipng(1) commands and LaTeX math packages.
+ Requires latex(1), dvipng(1) and/or dvisvgm(1) commands and LaTeX math
+ packages.
OPTIONS
-D DPI
- Set the output resolution to DPI dots per inch. Use this option to
- scale the output image size.
+ Set the output resolution for PNG images to DPI dots per inch. Use
+ this option to scale the output PNG image size.
-o OUTFILE
The file name of the output file. If not specified the output file is
- named like INFILE but with a .png file name extension.
+ named like INFILE but with an extension matching the chosen output
+ image format, .png for PNG images and .svg for SVG images.
-m
- Skip if the PNG output file is newer that than the INFILE.
+ Skip if the output image file is newer than the INFILE.
Compares timestamps on INFILE and OUTFILE. If
INFILE is - (stdin) then compares MD5 checksum stored in file
named like OUTFILE but with a .md5 file name extension.
@@ -40,7 +42,7 @@
Print program version number.
SEE ALSO
- latex(1), dvipng(1)
+ latex(1), dvipng(1), dvisvgm(1)
AUTHOR
Written by Stuart Rackham, <[email protected]>
@@ -62,7 +64,7 @@
import os, sys, tempfile, md5
-VERSION = '0.1.0'
+VERSION = '0.2.0'
# Include LaTeX packages and commands here.
TEX_HEADER = r'''\documentclass{article}
@@ -114,8 +116,10 @@
if os.system(cmd):
raise EApp, 'failed command: %s' % cmd
-def latex2png(infile, outfile, dpi, modified):
- '''Convert LaTeX input file infile to PNG file named outfile.'''
+def latex2img(infile, outfile, imgfmt, dpi, modified):
+ '''
+ Convert LaTeX input file infile to image file named outfile.
+ '''
outfile = os.path.abspath(outfile)
outdir = os.path.dirname(outfile)
if not os.path.isdir(outdir):
@@ -128,7 +132,7 @@
if infile == '-':
tex = sys.stdin.read()
if modified:
- checksum = md5.new(tex).digest()
+ checksum = md5.new(tex + imgfmt + str(dpi)).digest()
md5_file = os.path.splitext(outfile)[0] + '.md5'
if os.path.isfile(md5_file) and os.path.isfile(outfile) and \
checksum == read_file(md5_file,'rb'):
@@ -151,12 +155,19 @@
try:
# Compile LaTeX document to DVI file.
run('latex %s' % texfile)
- # Convert DVI file to PNG.
- cmd = 'dvipng'
- if dpi:
- cmd += ' -D %s' % dpi
- cmd += ' -T tight -x 1000 -z 9 -bg Transparent --truecolor -o "%s" "%s" ' \
- % (outfile,dvifile)
+ if imgfmt == 'svg':
+ # Convert DVI file to SVG.
+ cmd = 'dvisvgm'
+ cmd += ' --no-fonts'
+ cmd += ' --scale=1.2'
+ cmd += ' -o "%s" "%s"' % (outfile,dvifile)
+ else:
+ # Convert DVI file to PNG.
+ cmd = 'dvipng'
+ if dpi:
+ cmd += ' -D %s' % dpi
+ cmd += ' -T tight -x 1000 -z 9 -bg Transparent --truecolor'
+ cmd += ' -o "%s" "%s" ' % (outfile,dvifile)
run(cmd)
finally:
os.chdir(saved_pwd)
@@ -173,11 +184,12 @@
print_stderr(msg)
print_stderr('\n'
'usage:\n'
- ' latex2png [options] INFILE\n'
+ ' latex2img [options] INFILE\n'
'\n'
'options:\n'
' -D DPI\n'
' -o OUTFILE\n'
+ ' -f FORMAT\n'
' -m\n'
' -v\n'
' --help\n'
@@ -188,20 +200,22 @@
global verbose
dpi = None
outfile = None
+ imgfmt = 'png'
modified = False
import getopt
- opts,args = getopt.getopt(sys.argv[1:], 'D:o:mhv', ['help','version'])
+ opts,args = getopt.getopt(sys.argv[1:], 'D:o:mhvf:', ['help','version'])
for o,v in opts:
if o in ('--help','-h'):
print __doc__
sys.exit(0)
if o =='--version':
- print('latex2png version %s' % (VERSION,))
+ print('latex2img version %s' % (VERSION,))
sys.exit(0)
if o == '-D': dpi = v
if o == '-o': outfile = v
if o == '-m': modified = True
if o == '-v': verbose = True
+ if o == '-f': imgfmt = v
if len(args) != 1:
usage()
sys.exit(1)
@@ -209,13 +223,16 @@
if dpi and not dpi.isdigit():
usage('invalid DPI')
sys.exit(1)
+ if not imgfmt in {'png', 'svg'}:
+ usage('Invalid image format. Valid values are "png" or "svg".')
+ sys.exit(1)
if outfile is None:
if infile == '-':
usage('OUTFILE must be specified')
sys.exit(1)
- outfile = os.path.splitext(infile)[0] + '.png'
+ outfile = os.path.splitext(infile)[0] + '.' + imgfmt
# Do the work.
- latex2png(infile, outfile, dpi, modified)
+ latex2img(infile, outfile, imgfmt, dpi, modified)
# Print something to suppress asciidoc 'no output from filter' warnings.
if infile == '-':
sys.stdout.write(' ')