Discussion:
[Dvipng] dvipng and chemfig
s***@openmailbox.org
2015-03-17 19:25:00 UTC
Permalink
dvipng does not produce correct output. The default LaTeX
template in Anki looks like this (I have added the chemfig bits):

```latex
\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\usepackage{chemfig}
\begin{document}
\chemfig{A-B}
\end{document}
```

and to use that Anki runs

latex tmp.tex
dvipng -D 200 -T tight tmp.dvi -o tmp.png

which produces broken output:

http://imgur.com/EHkPSwM

which is similar to what you get if you just open the .dvi in
evince. This is unfortunate considering many (well, at least
Anki) treat dvipng as a complete solution.

For the record, here's a workaround: If you don't want to let go
of latex, then convert to postscript and then to pdf

latex tmp.tex
dvips tmp.dvi
ps2pdf tmp.ps

or just embrace modernity and simplicity and run pdflatex

pdflatex tmp.tex

then convert crop the pdf and convert to png

pdfcrop tmp.pdf tmp-cropped.pdf
pdftoppm -png -r 200 tmp-cropped.pdf tmp

and the final file is tmp-1.png:

http://imgur.com/yxYGdpZ

Is there any chance dvipng will be patched or is this a feature?
Also, do you have better workaround? dvipng's -T tight option is
very handy.
Jan-Åke Larsson
2015-03-17 22:23:58 UTC
Permalink
Hi,
You are not alone to want to generate pngs from tikz/pgf graphics, in
this case from chemfig.

Dvipng cannot do this because pgf sometimes mixes DVI commands and raw
Postscript drawing. Dvipng tries to draw the Postscript using
ghostscript, but these small snippets of Postscript are not enough to
produce a complete graphic in ghostscript. Also, dvipng cannot know
where point (the DVI cursor) is after such a draw operation. Dvipng does
tell you what goes wrong:

dvipng warning: PostScript environment contains DVI commands
dvipng warning: No image output from inclusion of raw PostScript

There are two possibilities to rectify this: either write a dvipng
driver for pgf, or write an internal Postscript interpreter for dvipng.
Both of these are huge tasks that I do not have time for.

The third alternative is to use another tool. Dvipng was written for
speed, competing with ghostscript (slow startup mainly) and ImageMagic
(ditto). Pdftoppm uses poppler and seems fast on my machine, but these
days, everything is. Anyhow, for single images I'd go

\documentclass[12pt]{standalone}
\usepackage{chemfig}
\begin{document}
\chemfig{A-B}
\end{document}

pdflatex tmp.tex
pdftoppm -png -r 200 tmp.pdf tmp

This crops single pages, but I have not found a way to correctly crop
several pages out-of-the-box. Except with dvipng -T tight. Modern, eh?

/JÅ
Post by s***@openmailbox.org
dvipng does not produce correct output. The default LaTeX
```latex
\documentclass[12pt]{article}
\special{papersize=3in,5in}
\usepackage[utf8]{inputenc}
\usepackage{amssymb,amsmath}
\pagestyle{empty}
\setlength{\parindent}{0in}
\usepackage{chemfig}
\begin{document}
\chemfig{A-B}
\end{document}
```
and to use that Anki runs
latex tmp.tex
dvipng -D 200 -T tight tmp.dvi -o tmp.png
http://imgur.com/EHkPSwM
which is similar to what you get if you just open the .dvi in
evince. This is unfortunate considering many (well, at least
Anki) treat dvipng as a complete solution.
For the record, here's a workaround: If you don't want to let go
of latex, then convert to postscript and then to pdf
latex tmp.tex
dvips tmp.dvi
ps2pdf tmp.ps
or just embrace modernity and simplicity and run pdflatex
pdflatex tmp.tex
then convert crop the pdf and convert to png
pdfcrop tmp.pdf tmp-cropped.pdf
pdftoppm -png -r 200 tmp-cropped.pdf tmp
http://imgur.com/yxYGdpZ
Is there any chance dvipng will be patched or is this a feature?
Also, do you have better workaround? dvipng's -T tight option is
very handy.
_______________________________________________
Dvipng mailing list
https://lists.nongnu.org/mailman/listinfo/dvipng
Loading...