skip to content

Convert tikz Generated PDF to Image

2 min read

# latex
# tikz
Not in series

Use class standalone and ImageMagick

Create .tex File with Class standalone

\documentclass[tikz,border=2pt]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw (0,0) circle (1);
\end{tikzpicture}
\end{document}

Compile to PDF

pdflatex main.tex

or

xelatex main.tex

or

latexmk -xelatex main.tex

or

latexmk -pdflatex main.tex

Command-Line Tools of ImageMagick

ImageMagick® is a free, open-source software suite, used for editing and manipulating digital images. It can be used to create, edit, compose, or convert bitmap images, and supports a wide range of file formats, including JPEG, PNG, GIF, TIFF, and Ultra HDR.

convert -density 300 main.pdf pic1.png

Parameter:

  • -density 300: Set the resolution to 300

  • Format of output file can be .png , .jpg, .svg, etc.

Use dvisvgm to Export to SVG

Reference: TEX Wiki dvisvgm

Sample .tex File that can Present Kanji

\ifdefined\kanjiskip
  \documentclass[uplatex]{jsarticle}
\else
  \ifdefined\pdfoutput
    \documentclass[latex]{bxjsarticle}
    \usepackage[whole]{bxcjkjatype}
  \else
    \documentclass[xelatex]{bxjsarticle}
    \usepackage{zxjatype}
    \setjamainfont{ipam.ttf}
  \fi
\fi
\usepackage[active,tightpage]{preview}
\begin{document}
\begin{preview}
 
吾輩は猫である。名前はまだ無い。
 
どこで生れたかとんと見当がつかぬ。
何でも薄暗いじめじめした所で
ニャーニャー泣いていた事だけは記憶している。
吾輩はここで始めて人間というものを見た。
 
\end{preview}
\end{document}

Convert upLaTeX Generated DVI File to SVG:

uplatex hoge.tex
dvisvgm hoge.dvi

Convert XeLaTeX Generated DVX File to SVG:

xelatex -no-pdf hoge.tex
dvisvgm hoge.xdv

Convert LaTeX Generated DVI File to SVG:

Convert DVI to EPS, then convert EPS to SVG:

latex hoge.tex
dvips -E -o hoge.eps hoge.dvi
dvisvgm -E hoge.eps

If you use dvisvgm to convert a DVI or XDV file to an SVG file, the text data will be preserved. If you use dvisvgm to convert an EPS file to an SVG file, the text data will be lost and you will not be able to copy the strings.

Use Class tikzexternalize to Export to PNG Automatically

Source Code Sample

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{external}
\tikzexternalize[mode=convert with system call]
\tikzset{external/system call={pdflatex \tikzexternalcheckshellescape -halt-on-error -interaction=batchmode -jobname "\image" "\texsource" && convert -density 300 "\image.pdf" "\image.png"}}
 
\begin{document}
\begin{tikzpicture}
  \draw (0,0) circle (1);
\end{tikzpicture}
 
Second Picture: \tikz \fill (0,0) circle(5pt);
\end{document}

Compile

pdflatex -shell-escape main.tex

Output Files:

  • main-figure0.png:

  • main-figure1.png: