TransWikia.com

Compile LaTeX document to PNG automatically

TeX - LaTeX Asked by user363808 on August 10, 2021

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:

enter image description here

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

enter image description here

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?

2 Answers

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 FilesMiKTeX 2.9miktexbinx64;%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.

Correct answer by Boubou0909 on August 10, 2021

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{
  external/system call/.add=
  {}%
  {; 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}

Answered by Sergio Llorente on August 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP