Use standalone class...
Most of the time, I useĀ OverleafĀ (Online LaTeX editor) to draw my illustrations and I choose Standalone as a documentclass. This class allows us to get an output file that only contains the picture with no extra (or specified) white margins. Here is an example of an equation with 0.2cm white margin:Ā
\documentclass[border=0.2cm]{standalone} \usepackage{tikz} \begin{document} \begin{tikzpicture} \node at (0,0) {\Huge $e^{i\pi}=-1$}; \end{tikzpicture} \end{document}
LaTeX to PNG
Converting LaTeX to PNG can be easily done in Overleaf as follows:
Ā Ā 1. In the main folder, create a file and name it latexmkrcĀ without extension
Ā Ā 2. Write the following line code in the latexmkrc file:Ā Ā
END { system('convert -density 300 output.pdf myImage.png'); }
Ā 3. Compile your document
Ā 4. Ā You get a png file in the tab logs and output filesĀ with the same name specified in the line code (myImage.png).
Let's go deeper!
The idea is to create a configuration file in Overleaf project that contains just a single line:Ā
END { system('convert -density 300 output.pdf myImage.png'); }
The file is namedĀ latexmkrcĀ as shown below:
After a successful compilation of the main LaTeX file, we get a png file in the tab logs and output filesĀ with the same name specified in the line code (myImage.png).Ā
The line command calls a Perl function system(...) which excutes a program calledĀ convert. The latter does the conversion of the pdf file (output.pdf) at 300 dpi (specified using -density 300) to a PNG file with the chosen name myImage.png.Ā
In this post, we will highlight some useful options and the reader may refer to ImageMagick's online help for a complete version.
Resize the generated image file
In order to change the size of the generated image file, we can add -resizeĀ as follows:
END {system('convert -density 600 -resize 20% output.pdf myImage.png');}
This will produce an image with a size 20% of the original image and at 600 dpi.
Change the background color
Ā By default, the generated image has a transparent background. In order to change its color (for example to yellow), we add -background yellow -flatten to the previous line code:
END { system('convert -density 600 -background yellow -flatten output.pdf myImage.png');}Ā
How to export a JPEG image from LaTeX
This can be done by changing the name of the image file from myImage.pngĀ Ā to myImage.jpeg. That's all!
END {system('convert -density 600 -background yellow -flatten output.pdf myImage.jpeg'); }
Convert a specific page of a document to an ImageĀ
Until now, we have considered standalone class to export TikZ illustrations to an image instead of a PDF file.
Suppose we have a document with many pages and we Ā would like to convert one page to an image. In this case, we modify output.pdfĀ to output[numberOfPage].pdf.Ā
Here is an example of exporting page 2 of the PDF document to a PNG image with a nameĀ page2:
END { system('convert -density 600 -background white -flatten output[2].pdf page2.png'); }
The very first sentence should say āI will teachā š
Each time I start writing a tutorial, I learn something new. For that, I prefer to say āwe will learnā instead of āI will teachā š.
Ah, thereās āweā at the beginning, not āIā ā my mistake. And I agree, the best way to learn is to teach š
Thanks š