Extracting Image from latex

I want to show chemical reactions on a website so i want to just get image from LaTeX code.
I know how to add images to latex but don't know how to get image from LaTeX.

Example I want to get images of

\chemname{\chemfig{CH_3-CH(-[2]CH_3)-CH_2-Cl}}{Isobutyl chloride}


which look like this

• Hi, i think the standalone class will do exactly what you want. :-) – Johannes_B Jun 27 '15 at 13:03
• @Johannes_B Would you mind explaining abit with details ? i don't get what you are saying :( – user80946 Jun 27 '15 at 13:05
• didn't you just do exactly that to ask this question? – David Carlisle Jun 27 '15 at 13:06
• @DavidCarlisle yes but that image is from google :P . – user80946 Jun 27 '15 at 13:07
• @DavidCarlisle or i can make pdf from latexcode and take the screenshot and post here. but is there any option that directly give images on compiling or nearly 1 step process ? – user80946 Jun 27 '15 at 13:08

Package preview was written for this purpose (to generate PNG images to be included in the emacs editor).

\documentclass{article}
\usepackage{chemfig}

% A fixed \chemname, which takes the width of the name into account
\newcommand*{\ChemName}[2]{%
\begingroup
\sbox0{#1}%
\sbox2{#2}%
\ifdim\wd2>\wd0 %
\leavevmode
\hbox to \wd2{\hss\chemname{#1}{#2}\hss}%
\else
\chemname{#1}{#2}%
\fi
\endgroup
}

\usepackage[active, tightpage]{preview}
\PreviewMacro[[!!]\chemname
\PreviewMacro[!]\chemfig
\PreviewMacro[!!]\ChemName

\begin{document}
\chemname{\chemfig{CH_3-CH(-[2]CH_3)-CH_2-Cl}}{Isobutyl chloride}
\chemfig{R-C(-[:-30]OH)=[:30]O}
\ChemName{\chemfig{CH_3CH_2Cl}}{Ethyl Chlorid}
\end{document}


The document catches \chemname and \chemfig and generates two pages with tight bounding boxes. The PDF file can then be converted to bitmaps by ImageMagick's convert or Ghostscript, for example.

convert -density 300 test.pdf test-%02.png


Generates test-00.png, test-01.png, and test-02.png:

Package preview takes the bounding box as reported from TeX. If a macro \chemname prints outside the box, when the name is longer than the formula, then the result is cropped. Macro \ChemName measures the two widths and increases the bounding box if necessary.

• when i try convert it says failed to execute command gswin32c.exe like that?? – user80946 Jun 27 '15 at 14:01
• any solutions of this problem? – user80946 Jun 27 '15 at 14:47
• @user80946 You have installed ghostscript? – Heiko Oberdiek Jun 27 '15 at 15:02
• I have installed imagemagicks binaries – user80946 Jun 27 '15 at 15:04
• should i need to install ghostscript as well? – user80946 Jun 27 '15 at 15:04

Make a very minimal tex-file in which just the chemfig code is.

% this is file isobutylChloride
\chemfig{CH_3-CH(-[2]CH_3)-CH_2-Cl}
\endinput


Now, input that file in a standalone document and compile it with the -shell-escape flag. A png file will be created on the fly, a conversion program is needed to be installed, though.

\documentclass[convert=true]{standalone}
\usepackage{chemfig}
\begin{document}
\input{isobutylChloride}
\end{document}


In your real (big) document, you can include the high-quality pdf, the compilation will be faster as well.

There are other options to the standalone class, every little detail is available in the package documentation.

• It creates pdf file ? how to get images? – user80946 Jun 27 '15 at 13:33
• From the answer: A png file will be created on the fly, a conversion program is needed to be installed, though. – Johannes_B Jun 27 '15 at 13:35
• is it necessary to create one more file here isobutylChloride ..isn't it possible to do all things in same file ? – user80946 Jun 27 '15 at 13:37
• Yes, see Heikos answer. – Johannes_B Jun 27 '15 at 13:43
• I am guessing that you have a huge document. Having the definition outside seems to be the best you can do. You could also use tikzexternalize, that catches every changes to your tikz-pictures automatically. Transformation to png would be done by hand then. As we don't have much information about your setup, this is the best i can tell. – Johannes_B Jun 27 '15 at 13:46