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 the tikzpicture
environment, 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:
- Wrap it in a complete LaTeX document
- Compile it to PDF
- Convert the PDF to PNG
- 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 a wrapper-header.tex
file 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:
- File Organization: All your TikZ diagrams should be in a directory named
tex-images
, along with thewrapper-header.tex
file. - Reading Files: The script reads each .tex file in the directory, skipping the wrapper template and any non-tex files.
- 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
- Compilation: Uses pdflatex to compile each wrapped file into a PDF, with all output files staying in the tex-images directory.
- Conversion: Uses ImageMagick's convert command to create high-resolution (300 DPI) PNGs from each PDF.
- Packaging: Creates a zip file containing all the generated PNGs, which you can then download from Overleaf.
Usage
To use this system:
- Create a
tex-images
directory in your Overleaf project - Add the wrapper-header.tex file with your desired preamble
- Place all your TikZ diagrams in separate .tex files in this directory
- Add the script to your latexmkrc file
- Run a full compilation
- Download the resulting tikz-exports.zip file
The PNGs will be high-quality and ready for web use, presentations, or documentation.