[Tex/LaTex] resize a TikZ picture to have certain dimensions (width, height)

heightscalingstandalonetikz-pgfwidth

Suppose that I have made aTikZ picture using the standalone class, is there an easy way to determine some fixed output dimension of this picture, say width=16cm, height=12cm? This could be useful in the case I want to export this picture to a presentation, which only accepts PNG, hence when I need to convert the vector image to a raster image.

Example code of one of my TikZ pictures:

\documentclass[tikz]{standalone}
\usepackage{tikz-network}
\usepackage{tikz}


\begin{document}


\begin{tikzpicture}[scale=0.9]

\Vertex[x=0,y=1,label=\textbf{Genesis},shape=rectangle,size=3,color=blue!80!black,Math,fontsize=\large]{G1}

\Vertex[x=6,y=1,label=\textbf{Block 1},size=3,shape=rectangle,color=orange,Math,fontsize=\large]{b1}
\Text[x=0,y=0.5]{Index:0}
\Text[x=0,y=0]{Hash:a0}
\Vertex[x=12,y=1,label=\textbf{Block 2},size=3,shape=rectangle,color=orange,Math,fontsize=\large]{b2}
\Text[x=6,y=0.5]{Index:1}
\Text[x=6,y=0]{Hash:eaKvl}
\Vertex[x=18,y=1,label=\textbf{Block 3},size=3,shape=rectangle,color=orange,Math,fontsize=\large]{b3}

\Text[x=12,y=0.5]{Index:2}
\Text[x=12,y=0]{Hash:yezgLg1}

\Text[x=18,y=0.5]{Index:3}
\Text[x=18,y=0]{Hash:adLc2eZ}
\Edge[Direct=True,color=black,lw=2,bend=0](G1)(b1)
\Edge[Direct=True,color=black,lw=2,bend=0](b1)(b2)
\Edge[Direct=True,color=black,lw=2,bend=0](b2)(b3)

\end{tikzpicture}

\end{document}

Example of the generated image

Best Answer

If you want to create PNGs from your standalone file in a certain size, you can use the png class option to do this. It has a size and a density option to select the resulting image size. The density option is AFAIK the important one here.

From the standalone v1.2 manual, page 17:

density: Sets the density in dots-per-inch (dpi). Can be a single numerical value or ‘〈X〉x〈Y〉’. Default: 300
size: Sets the size of the image. Can be a single numerical value or ‘〈X〉x〈Y〉’. If empty the size is determined by the density setting and the size of the PDF. Default: empty

It's used like \documentclass[png={size=...,density=...}]{standalone}.


Otherwise if you want to resize the picture for any other reason to a certain width and height you can just use the adjustbox package around the tikzpicture. Note that you need then remove the tikz option of the standalone class, otherwise every (outer) tikzpicture will be turned into a page automatically, which clashes with the surrounding environment then.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{adjustbox}
\begin{document}

\begin{adjustbox}{width=16cm, height=12cm, keepaspectratio}
\begin{tikzpicture}
\node at (5,5) {some tikz content} ;
\draw (0,0) -- (10, 10);
\end{tikzpicture}
\end{adjustbox}%

\end{document}

If you have the need of multiple pages in the document use the multi=ENVIRONMENTNAME class option and wrap each one in that environment as mentioned in the manual. Note that the tikz option is just short for multi=tikzpicture and also loads the tikz package for you.

\documentclass[multi=PAGE]{standalone}
\usepackage{tikz}
\usepackage{adjustbox}
\begin{document}

\begin{PAGE}%
\begin{adjustbox}{width=16cm, height=12cm, keepaspectratio}
\begin{tikzpicture}
\node at (5,5) {some tikz content} ;
\draw (0,0) -- (10, 10);
\end{tikzpicture}
\end{adjustbox}%
\end{PAGE}

\begin{PAGE}%
\begin{adjustbox}{width=16cm, height=12cm, keepaspectratio}
\begin{tikzpicture}
\node at (5,5) {some other tikz content} ;
\draw (0,10) -- (10, 0);
\end{tikzpicture}
\end{adjustbox}%
\end{PAGE}

\end{document}