Compile LaTeX document to PNG automatically

I want to export chemical lewis structures as transparent PNG-files in order to use them in Word documents.

So I want to achieve whats described in this post. I just can't get it to work because my understanding of LaTeX is very limited.

This is my code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{verbatim}
\pagenumbering{gobble}
\usepackage{chemfig}
\usepackage[version=4]{mhchem}
\renewcommand*\printatom[1]{\sffamily{#1}}
\newcommand*\forcelen[1]{#1/\CF@atom@sep}
\newcommand{\pol}[2][red]{$\color{#1} \delta^{#2}$}

\begin{document}

\chemfig{
\charge{135:5pt=\pol{+}}{Si}
(-[0]\charge{0=\|,90=\|,270=\|,0:9pt=\pol[blue]{-}}{Br})
(-[2]\charge{0=\|,90=\|,180=\|,90:8pt=\pol[blue]{-}}{Br})
(-[4]\charge{90=\|,180=\|,270=\|,180:8pt=\pol[blue]{-}}{Br})
(-[6]\charge{0=\|,180=\|,270=\|,270:7pt=\pol[blue]{-}}{Br})}

\end{document}


This is what it looks like:

When I use \documentclass{standalone} it crops the document too close:

In the post mentioned above the OP wrote:

There is a border class option which sets the border around the content (the default is 0.5bp). This option excepts either one (border for all sides), two (left/right, top/bottom) or four values (left, bottom, right, top).

I have no clue how to implement the border though.

Regarding the PNG Output another user wrote this Shell-Script but I don't know how to use it. Where do I need to save the script and where do I place the code to call it in the LaTeX document?

• \documentclass[border=1mm]{standalone} adds 1mm to each side. May 15, 2021 at 11:38

You can try to use dvisvgm to convert your document into an .svg file, and then if you need into a .png.

You have to download this then you can create a .bat file (converter.bat for example) in the same folder as your .tex file, then put the following code :

@echo off
path=C:\Program Files\MiKTeX 2.9\miktex\bin\x64;%path%;
dvisvgm --pdf --no-fonts myfile.pdf myflie.svg
@echo on


Then you can run it after create your pdf file, and then you go.

• Thanks this works perfectly. And it's even better since SVG is supported in MS Office applications. Do you by any chance know how to increase the border when I use "\documentclass{standalone}"? May 15, 2021 at 13:45
• I found it here: tex.stackexchange.com/questions/123880/… May 15, 2021 at 13:56

You must use an external tool, like convert, as is mentioned in the post that you cite. The border option is very easy if you want then same margin at the four borders:

\documentclass[border=15mm]{standalone}


Latex can call automatically that external tool. For example, taking the example in this other question:

\documentclass[convert={density=300,size=1080x800,outext=.png}, border=15mm]{standalone}


Note that you must have installed convert program, and that you have to call latex with -shell-escape option.

A different approach is to use the tikz and its external library.

\documentclass[a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{external}
\tikzset{
{}%
{; convert -density 300 -transparent white "\image.pdf" "\image.png"},
/pgf/images/external info,
}
\tikzexternalize[prefix=dir/]

\begin{document}

A figure to export
% Optional: you can set the filename of the following picture:
%\tikzsetnextfilename{filename}
\begin{tikzpicture}
\node[text width=\linewidth]
{%
Your latex code here: $x=1$
};
\end{tikzpicture}

Another figure

\begin{tikzpicture}
\node[text width=\linewidth]
{%
Other latex code here: $x^2=1$
};
\end{tikzpicture}
\end{document}


In this example the the directory dir must exist. Then pdflatex --shell-escape generete two png files:

dir/test-figure0.png
dir/test-figure1.png


The filename of each picture can be set with \tikzsetnextfilename{filename}