Documentation/TikZ & Graphics/How to Export TikZ Diagrams to SVG or PNG
TikZ & Graphics

How to Export TikZ Diagrams to SVG or PNG

TikZ produces beautiful vector diagrams inside LaTeX, but you often need those diagrams as standalone SVG or PNG files — for websites, slides, Word documents, or README files. The process involves compiling a standalone TikZ document and converting the output to your target format. This tutorial covers the complete workflow for both SVG (vector) and PNG (raster) export. Bibby AI can export your TikZ diagrams directly to SVG and PNG from the editor, saving you the manual conversion pipeline.

Create a Standalone TikZ Document

Use the standalone document class to produce a tightly cropped PDF containing only the diagram:

% Save as diagram.tex
\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{arrows.meta, positioning}

\begin{document}
\begin{tikzpicture}[
    box/.style={draw, rounded corners, fill=blue!15, minimum width=2cm, minimum height=0.8cm},
    >={Stealth[length=2.5mm]}
]
    \node[box] (a) {Input};
    \node[box, right=2cm of a] (b) {Process};
    \node[box, right=2cm of b] (c) {Output};
    \draw[->] (a) -- (b);
    \draw[->] (b) -- (c);
\end{tikzpicture}
\end{document}

% Compile: pdflatex diagram.tex
% This produces diagram.pdf cropped to exactly the diagram size.

Convert to SVG and PNG

Use command-line tools to convert the standalone PDF to SVG (vector) or PNG (raster) format:

% === Convert to SVG (vector, best for web) ===
% Method 1: dvisvgm (best quality — compile with latex, not pdflatex)
% latex diagram.tex          → produces diagram.dvi
% dvisvgm diagram.dvi        → produces diagram.svg

% Method 2: pdf2svg (if you already have a PDF)
% pdflatex diagram.tex       → produces diagram.pdf
% pdf2svg diagram.pdf diagram.svg

% Method 3: Inkscape (also works)
% inkscape diagram.pdf --export-type=svg --export-filename=diagram.svg

% === Convert to PNG (raster, for slides/docs) ===
% Method 1: ImageMagick (control resolution with -density)
% convert -density 300 diagram.pdf diagram.png

% Method 2: Ghostscript (more control)
% gs -dNOPAUSE -dBATCH -sDEVICE=png16m -r300 \
%    -sOutputFile=diagram.png diagram.pdf

% Method 3: pdftoppm (from poppler-utils)
% pdftoppm -png -r 300 diagram.pdf diagram

Automate with a Makefile

Set up a Makefile to automatically build and export all your TikZ diagrams:

% Save this as Makefile in your project root:
%
% SOURCES = $(wildcard figures/*.tex)
% SVGS = $(SOURCES:.tex=.svg)
% PNGS = $(SOURCES:.tex=.png)
%
% all: $(SVGS) $(PNGS)
%
% %.pdf: %.tex
% 	pdflatex -output-directory=$(dir $<) $<
%
% %.svg: %.pdf
% 	pdf2svg $< $@
%
% %.png: %.pdf
% 	convert -density 300 $< $@
%
% clean:
% 	rm -f figures/*.pdf figures/*.svg figures/*.png figures/*.aux figures/*.log

% Then just run: make
% All .tex files in figures/ are compiled and exported to SVG + PNG.

% In your LaTeX document, you can still use the original TikZ:
\documentclass{article}
\usepackage{graphicx}
\begin{document}
% Use the exported PNG in non-LaTeX contexts,
% or include the original TikZ in your LaTeX paper.
\includegraphics[width=0.8\textwidth]{figures/diagram.png}
\end{document}

💡 Tips

  • Use the standalone document class with [tikz, border=5pt] for perfectly cropped diagram PDFs.
  • For SVG export, dvisvgm produces the best quality — but you must compile with 'latex' (DVI mode), not pdflatex.
  • Set PNG resolution to at least 300 DPI (-density 300) for print-quality output.
  • Bibby AI lets you right-click any TikZ diagram in the preview and export directly to SVG or PNG.

Try This in Bibby AI

Write LaTeX faster with AI auto-complete and instant compilation.

Start Writing Free

Related Tutorials

How to Export TikZ Diagrams to SVG or PNG | Bibby AI