# Scale TikZ nodes and shapes for a given canvas size

I have the following code to produce an hexagon coverage area:

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathreplacing} % for expanding waves
\usetikzlibrary{decorations.markings}

\tikzset{%
terminal/.style = {draw, shape = circle , thick, radius = 2cm},
area/.style = {draw, shape = regular polygon, regular polygon sides = 6, thick, minimum width = 10cm},
basestation/.style = {draw, shape = dart, shape border rotate = 90, thick, minimum width = 1cm, minimum height = 1cm},
transmission/.style = {decorate, decoration = {expanding waves, angle = 7, segment length = 4}, thick},
label/.style = {font=\footnotesize}
}

\usepackage{tikzscale}
\usepackage{adjustbox}

\usepackage{filecontents}
\usepackage{silence}
\WarningFilter{latex}{Overwriting file}
\WarningFilter{latex}{Tab has been converted to Blank Space}

\newcommand*{\FILE}{test}

\begin{filecontents*}{\FILE.tikz}
\begin{tikzpicture}
\draw [help lines, step = 0.5cm] (-5,-5) grid (5,5);
\node [area] at (0,0) {};
\node [basestation] (ENB) at (0,-0.5) {eNB}; % not exactly at center

\node [terminal] (UE2) at (-0.5,2) {UE};
\node [terminal] (UE3) at (2.5,-1) {UE};

\draw [blue, transmission] (ENB.north) -- (UE3) node [midway] (celllink1) {};
\draw [blue, transmission] (ENB.north) -- (UE2) node [midway] (celllink2) {};

\node [label] (celllinktext1) at (1, -1.5) {Cellular Link};
\node [label, align = center] (celllinktext2) at (-2, -1) {Cellular\\ Link};

\path [out = 90, in = 210] (celllinktext1) edge (celllink1);
\path [out = 90, in = 180] (celllinktext2) edge (celllink2);
\end{tikzpicture}
\end{filecontents*}

\newlength\WFIG
\setlength{\WFIG}{252.0pt} % column width

\begin{document}
%\includegraphics[width=0.5\WFIG]{\FILE.tikz}
%\resizebox{0.5\WFIG}{!}{\input{\FILE.tikz}}
\begin{adjustbox}{width=0.5\WFIG}
\input{\FILE.tikz}
\end{adjustbox}
\input{\FILE.tikz}
\end{document}


I have drawn that picture to a one-column document. Now I want to use that picture for a two-column document. Therefore I am trying to properly scale it. I have tried the \adjustbox, \scalebox, and tikzscale (this does not work for this case). There are some approaches explained in How to scale a tikzpicture to \textwidth and Scale TikZ figure to linewidth when relative positioning used and TikZ: Expand width of each picture to given size. However I am not sure what is the best approach, if there is a best approach.

I also would like for the font to remain as much as possible the same (a possible solution for this would be to draw all text in a foreground layer that is unscalable, but I don't know if it even possible).

Any thoughts on this?!

-
I don’t think the standalone class is the best to show a twocolumn problem. Better would be \documentclass[twocolumn]{article}. Now you can access the column width with \linewidth or \columnwidth. –  Qrrbrbirlbel Mar 31 '13 at 23:07
Since I want a standalone picture, I set the picture width with \newlength\WFIG \setlength{\WFIG}{252.0pt} as you can see in my MWE. –  cacamailg Mar 31 '13 at 23:15
Use \columnwidth (or a fraction of it) to adjust the width: \begin{adjustbox}{width=\columnwidth} \input{\FILE.tikz} \end{adjustbox} –  Gonzalo Medina Mar 31 '13 at 23:18
@GonzaloMedina I know that, to set the width, I emulate the \columnwidth with \WFIG that I put in my MWE. The main question is to proper scale the current picture, or have an alternative method to define the nodes coordinates, so that I could use an TikZ command, that would properly fit the picture to the canvas size. I thought something like tikzscale package, but it doesn't work here. –  cacamailg Mar 31 '13 at 23:40

## 1 Answer

I am not certain if I've understood the question or not. Assuming that you plan to use this in a two column document, I've switched to article as suggested in comments. However, the code for the image is still taken from an external file. The scaling is determined by setting a PGF key to the required value. It is 1 by default. To change this use

\pgfkeyssetvalue{cacamailg/picture-scale}{<value>}


In the code below, I use 0.8 as an example but you can equally use, say, 2 or whatever.

To ensure that the node shapes are scaled, we set transform shape. To avoid the text being scaled, we override this in the style applied to the labelling nodes.

Note that I've renamed your styles since it is asking for trouble to use something like label which TikZ already uses for, well..., for labels.

In the following example, a full-width figure* is shown first with the default scale factor of 1. A scaled-down version is then used in a regular figure which appears within a column.

\documentclass[twocolumn]{article}
\usepackage{tikz,kantlipsum}
\usetikzlibrary{shapes.geometric,positioning,calc,decorations.pathreplacing,decorations.markings}

\tikzset{%
my terminal/.style = {draw, shape = circle , thick, radius = 2cm},
my area/.style = {draw, shape = regular polygon, regular polygon sides = 6, thick, minimum width = 10cm},
basestation/.style = {draw, shape = dart, shape border rotate = 90, thick, minimum width = 1cm, minimum height = 1cm},
transmission/.style = {decorate, decoration = {expanding waves, angle = 7, segment length = 4}, thick},
my label/.style = {font=\footnotesize, transform shape=false},
}

\usepackage{filecontents}

\begin{filecontents*}{\jobname.tikz}
\begin{tikzpicture}[scale=\pgfkeysvalueof{cacamailg/picture-scale}, transform shape]
\draw [help lines, step = 0.5cm] (-5,-5) grid (5,5);
\node [my area] at (0,0) {};
\node [basestation] (ENB) at (0,-0.5) {eNB}; % not exactly at center

\node [my terminal] (UE2) at (-0.5,2) {UE};
\node [my terminal] (UE3) at (2.5,-1) {UE};

\draw [blue, transmission] (ENB.north) -- (UE3) node [midway] (celllink1) {};
\draw [blue, transmission] (ENB.north) -- (UE2) node [midway] (celllink2) {};

\node [my label] (celllinktext1) at (1, -1.5) {Cellular Link};
\node [my label, align = center] (celllinktext2) at (-2, -1) {Cellular\\ Link};

\path [out = 90, in = 210] (celllinktext1) edge (celllink1);
\path [out = 90, in = 180] (celllinktext2) edge (celllink2);
\end{tikzpicture}
\end{filecontents*}

\pgfkeyssetvalue{cacamailg/picture-scale}{1}

\begin{document}
\begin{figure*}
\centering
\input{\jobname.tikz}
\caption{Wide figure}
\end{figure*}

\kant[1-10]
\begin{figure}
\centering
\pgfkeyssetvalue{cacamailg/picture-scale}{.8}
\input{\jobname.tikz}
\caption{Narrow figure}
\end{figure}
\kant[11-20]
\end{document}


First version, scale=1:

Second version, scale=.8 (80%):

-
I need to test better if it is working. But it seems it is. Thanks –  cacamailg 16 hours ago