4

I generate PNG images from latex code to display in web/mobile applications. The general flow is:

  1. Client makes an API call with the formula I should render
  2. I wrap the formula in a preamble and run latex on it
  3. From the generated .dvi I run dvipng with --depth

This process gives me a PNG image and the depth information and has been working fine for a while. Now I want to add the ability to render chemistry formulas, so I added chemfig package, and this is where the issue arises.

There are some answers over the internet like here and here advising to use pdflatex with chemfig and then another tool to convert from PDF to PNG, once just using dvipng won't produce correct images.

Generated with dvipng

Generated with pdflatex + convert

So I can render formula properly. The question is: is there a way to get image depth in this second flow?

The tex snippet to generate the images is:

\documentclass[12pt]{article} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{cancel} \usepackage{color} \usepackage{chemfig} \usepackage{yhmath} % for \wideparen \renewcommand*\familydefault{\sfdefault} \usepackage[active,textmath,tightpage]{preview} \begin{document}$\displaystyle \chemfig{A-B}$\end{document} 

EDIT: Using the suggested solution to write the information to another file I've got an empty file. Adding \immediate to writes it is not empty anymore, but all values are 0.0pt.

The full source is

\documentclass[12pt]{standalone} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{cancel} \usepackage{color} \usepackage{chemfig} \usepackage{yhmath} % for \wideparen \renewcommand*\familydefault{\sfdefault} \usepackage[active,textmath,tightpage]{preview} \setatomsep{2.0 em} % 'Fixed Bond Length' \makeatletter \global\let\tikz@ensure@dollar@catcode=\relax \makeatother \newsavebox\frm \sbox\frm{$\displaystyle \chemfig{A-B} $} \newwrite\frmdims \immediate\openout\frmdims=\jobname.dims \immediate\write\frmdims{depth: \the\dp\frm} \immediate\write\frmdims{height: \the\ht\frm} \immediate\write\frmdims{width: \the\wd\frm} \immediate\closeout\frmdims \begin{document} \usebox\frm \end{document} 
11
  • using Linux, after the convert you can use file or exiv2 as in stackoverflow.com/questions/4670013/… and parse the output from that. Commented Apr 5, 2017 at 14:40
  • @Marijn But this would return the image depth in order to compute the baseline? Commented Apr 5, 2017 at 14:43
  • @ThiagoLewin assuming the depth is the same as the height, then yes :) Commented Apr 5, 2017 at 14:45
  • 1
    @Marijn depth is the distance from the bottom of the image to the baseline. An entire description can be seen at tex.stackexchange.com/questions/40977/… Commented Apr 5, 2017 at 14:47
  • 1
    chemfig uses TikZ, which does not render in DVI files because it uses specials not understood by any DVI viewer (and by dvipng). Commented Apr 5, 2017 at 17:37

1 Answer 1

5
+50

You can measure height, width, and depth within LaTeX and supply the information in an extra file. After pdflatexing the following document,

\documentclass{standalone} \usepackage{chemfig} \newsavebox\frm \sbox\frm{$\displaystyle\chemfig{A-B}$} \newwrite\frmdims \openout\frmdims=\jobname.dims \write\frmdims{depth: \the\dp\frm} \write\frmdims{height: \the\ht\frm} \write\frmdims{width: \the\wd\frm} \begin{document} \usebox\frm \end{document} 

you get, in addition to the pdf file, a text file with extension dims that contains the required information.

depth: 0.0pt height: 6.83331pt width: 37.29172pt 
5
  • Thanks for your answer! I trying to use it, but my .dims file is empty. I'm still trying to make it right, but you have any ideas why this can be happening? Commented Apr 10, 2017 at 16:15
  • @Aspirina What does the log file say? Any error messages? How does the source file look like? Commented Apr 10, 2017 at 16:17
  • I edited my question with more info. Thanks again for your help. Commented Apr 10, 2017 at 16:30
  • 1
    @Aspirina The culprit is the preview package. Why do you load it? Commented Apr 10, 2017 at 16:40
  • good catch! It was a legacy from our previous approach. It is working now. Thanks again. Commented Apr 10, 2017 at 16:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.