Blog

How to Batch Convert TikZ Diagrams to PNG Files in Overleaf - Automation Guide

Wed Feb 05 2025 00:00:00 GMT+0000 (Coordinated Universal Time)

When working with LaTeX documents in Overleaf, you might create multiple TikZ diagrams that you also want to use elsewhere - perhaps in presentations, blog posts, or documentation. While these diagrams are embedded as PDFs in your LaTeX document, you might need them as PNG files for web use.

The Challenge

Typically, each TikZ diagram lives in its own .tex file containing just thetikzpictureenvironment, without a complete LaTeX document structure. For example:

\begin{tikzpicture} \node[draw] (A) {Concept A}; \node[draw, right=of A] (B) {Concept B}; \draw[->] (A) -- (B); \end{tikzpicture}

To convert this to a PNG, we need to:

  1. Wrap it in a complete LaTeX document
  2. Compile it to PDF
  3. Convert the PDF to PNG
  4. Do this for all diagrams in a directory

The Solution

We can automate this process using Overleaf's latexmkrc file. Our solution consists of two parts:

1. The Wrapper Template

First, we create awrapper-header.texfile in our tex-images directory that contains everything up to where the TikZ diagram should be inserted:

‍\documentclass[border=10pt]{standalone} \usepackage{tikz} % Add your custom TikZ settings and libraries here \begin{document}

2. The Automation Script

In our latexmkrc file, we add a Perl script that:

  • Reads all .tex files in the tex-images directory
  • Wraps each TikZ diagram in a complete LaTeX document
  • Compiles them to PDF
  • Converts them to PNG
  • Creates a zip file containing all PNGs

Here's the script with explanations:

‍*# Open the directory containing our TikZ files*` opendir(DIR, "./tex-images") or die "Cannot open directory: $!";

*# Read our wrapper template once* open(my $header, "<", "./tex-images/wrapper-header.tex") or die "Can't open wrapper: $!"; my $header_content = do { local $/; <$header> }; close($header);

*# Initialize array for tracking PNG files* my @png_files = ();

*# Process each .tex file* while (my $file = readdir(DIR)) { *# Skip special directories and non-tex files* next if ($file =~ /^.{1,2}$/); next unless ($file =~ /.tex$/); next if ($file eq 'wrapper-header.tex');

*# Get base filename without .tex extension* my $basename = $file; $basename =~ s/.tex$//;

*# Read the TikZ content* open(my $in, "<", "./tex-images/$file") or die "Can't open $file: $!"; my $tikz_content = do { local $/; <$in> }; close($in);

*# Create complete LaTeX document* open(my $out, ">", "./tex-images/$basename.wrapped.tex") or die "Can't create wrapped file: $!"; print $out $header_content; print $out $tikz_content; print $out "\n\end{document}\n"; close($out);

*# Compile to PDF* system "pdflatex -interaction=nonstopmode -shell-escape -output-directory=./tex-images ./tex-images/$basename.wrapped.tex";

*# Convert to PNG (300 DPI)* system "convert -density 300 ./tex-images/$basename.wrapped.pdf ./tex-images/$basename.png";

push @png_files, "./tex-images/$basename.png"; }

closedir(DIR);

*# Create final zip file containing all PNGs* if (@png_files) { my $png_list = join(" ", @png_files); system "zip -j tikz-exports.zip $png_list"; }`

How It Works

Let's break down each step:

  1. File Organization: All your TikZ diagrams should be in a directory namedtex-images, along with thewrapper-header.texfile.
  2. Reading Files: The script reads each .tex file in the directory, skipping the wrapper template and any non-tex files.
  3. Creating Complete Documents: For each TikZ file, it:
  • Takes the content from wrapper-header.tex
  • Adds the TikZ diagram code
  • Adds the \end{document} command
  • Saves this as a new .wrapped.tex file
  1. Compilation: Uses pdflatex to compile each wrapped file into a PDF, with all output files staying in the tex-images directory.
  2. Conversion: Uses ImageMagick's convert command to create high-resolution (300 DPI) PNGs from each PDF.
  3. Packaging: Creates a zip file containing all the generated PNGs, which you can then download from Overleaf.

Usage

To use this system:

  1. Create atex-imagesdirectory in your Overleaf project
  2. Add the wrapper-header.tex file with your desired preamble
  3. Place all your TikZ diagrams in separate .tex files in this directory
  4. Add the script to your latexmkrc file
  5. Run a full compilation
  6. Download the resulting tikz-exports.zip file

The PNGs will be high-quality and ready for web use, presentations, or documentation.

‍

Get in Touch

We speak English (native), German (native), and French. Let's discuss your project!