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}
Aspirina
  • 143
  • using Linux, after the convert you can use file or exiv2 as in http://stackoverflow.com/questions/4670013/fast-way-to-get-image-dimensions-not-filesize and parse the output from that. – Marijn Apr 05 '17 at 14:40
  • @Marijn But this would return the image depth in order to compute the baseline? – Thiago Lewin Apr 05 '17 at 14:43
  • @ThiagoLewin assuming the depth is the same as the height, then yes :) – Marijn Apr 05 '17 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 http://tex.stackexchange.com/questions/40977/confused-with-tex-terminology-height-depth-width – Aspirina Apr 05 '17 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). – egreg Apr 05 '17 at 17:37
  • @egreg Do you know if there is another way to get the image depth or baseline without using dvipng? – Thiago Lewin Apr 05 '17 at 17:54
  • using the standalone class and running it through pdflatex and then convert (imagemagick) on the PDF might be a solution. – daleif Apr 07 '17 at 15:04
  • @daleif But doing this way, can I read the depth or the baseline of the image? – Thiago Lewin Apr 07 '17 at 16:44
  • Not really. Once the pdf is made, that information is lost. – daleif Apr 07 '17 at 17:08
  • @daleif Okay, but do you know if there is a way to get this information? – Thiago Lewin Apr 07 '17 at 17:18
  • Nope. I get why you need it, but you need some really deep control to access that. Perhaps some of the box logging can be processed to give information relevant to the image. – daleif Apr 07 '17 at 19:32

1 Answers1

5

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
gernot
  • 49,614