# tikz .pic scaling with text

I am learning more about tikz .pic while experimenting with boxes with text inside them and then trying to rescale them. (for this example, I could create one png file and then includegraphics it, but I would rather understand how to do this properly in tikz.) or maybe .pic is not the right tool at all. in any case, they are all beginner questions.

here is a simple example:

\documentclass[border=3mm,12pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
testboxtext/.pic={
\draw[black, fill=gray] (0,0) rectangle (60,40);
\draw[fill=white] (20,20) rectangle (25,35) node[pos=0.5,rotate=90] { some text };
}
}

\begin{document}

\begin{tikzpicture}
\draw[step=1, gray, very thin] (-1,-1) grid (101,101);

\pic[scale=1] at (10,10) {testboxtext};
\pic[scale=0.25] at (10,60) {testboxtext};
\pic[scale=0.5, rotate=90] at (50,60) {testboxtext};
\end{tikzpicture}

\end{document}


so now we have three blocks with some text inside, obviously pretty difficult to see right now.

• how is the scale=1 pic font size determined? I can stick fontsizing commands inside the testboxtext, but this defeats the purpose. In some sense, I would want to tell it a scale relative to the box, like 80% of the box size.

• the scale=0.25pic has a font size different relative to the box. how does one best request proportional scaling?

and modestly related

• can one tell tikz to shrink text inside the box until it nicely fits? this means with line breaks, etc. so, if the text is very long, it would select a small font size (and break lines).

/iaw

• – JMP Apr 29 '16 at 21:47

Scaling in general does not affect fontsize, but just the size of the elements drawn by tikz. Fontsize is always defined by your documentclass, if you don't explicitly override their setting. Consider the following example:

\documentclass[tikz, border=2mm]{standalone}
%
\begin{document}
%
\begin{tikzpicture}
\draw (0,0) rectangle (2,1) node [midway] {Test};
\draw [gray, dashed] (0.5,0.25) rectangle (1.5,0.75) (-1,-0.5) rectangle (3,1.5);
\end{tikzpicture}
%
\begin{tikzpicture}[scale=0.5]
\draw (0,0) rectangle (2,1) node [midway] {Test};
\draw[white] (-3,-1.5) rectangle (5,2.5);% just for scaling the standalone page correctly
\end{tikzpicture}
%
\begin{tikzpicture}[scale=2]
\draw (0,0) rectangle (2,1) node [midway] {Test};
\end{tikzpicture}
\end{document}


Here I draw in the first tikzpicture three rectangles and insert text in the middle. The inner and the outer rectangles marked gray and dashed would correspond to a scaling of 0.5 and 2 respectively of the black solid one. I then take the definition of the solid rectangle and draw them in the next two tikzpictures with scaling 0.5 and 2 and as you can see, just the size of the ractangle changes, but the fontsize stays the same.

So the consequence is, that scaling the fontsize dependent on the tikz elements doesn't really work, but you can define the elements with respect to the fontsize, i.e. in units of em (1 em is actually the font size)

Then you could for example say:

\documentclass[tikz, border=2mm]{standalone}
%
\begin{document}
%
\begin{tikzpicture}
\draw (0em,0em) rectangle (4em,2em) node [midway] {Test};
\draw [gray, dashed] (1em,0.5em) rectangle (3em,1.5em) (-2em,-1em) rectangle (6em,3em);
\end{tikzpicture}
%
\begin{tikzpicture}[scale=0.5]
\draw (0em,0em) rectangle (4em,2em) node [midway] {Test};
\draw[white] (-6em,-3em) rectangle (10em,5em);% just for scaling the standalone page correctly
\end{tikzpicture}
%
\begin{tikzpicture}[scale=2]
\draw (0em,0em) rectangle (4em,2em) node [midway] {Test};
\end{tikzpicture}
\end{document}


So in the first picture we again define a rectangle, now of double the fontsize in height and four times the fontsize in width. The dashed, gray ones agian would correspond to scaling 2 and 0.5. When we now scale the initial rectangle by 0.5 (second picture), we know then, that it ends up with fontsize in height and double the fontsize in width, and as you see it snugly fits. Whereas a scaling of 2 gives us four times fontsize height and eight times in width. So if you want to fit your font into your boxes, you actually have to fit the boxes to the font!

And last but not least the evidence, that a change in fontsize also changes the the drawings accordingly, which where defined in units of em. I draw an outer rectangle of size 2 cm by 1 cm in comparison:

\documentclass[tikz, border=2mm]{standalone}
%
\begin{document}
%
\begin{tikzpicture}
\draw (0em,0em) rectangle (2em,1em) node [midway] (a) {Test};
\node[draw, shape=rectangle, minimum width= 2cm, minimum height=1cm, anchor=center] at (a) {};
\end{tikzpicture}
%
{\huge
\begin{tikzpicture}
\draw (0em,0em) rectangle (2em,1em) node [midway] (a) {Test};
\node[draw, shape=rectangle, minimum width= 2cm, minimum height=1cm, anchor=center] at (a) {};
\end{tikzpicture}
}
%
{\tiny
\begin{tikzpicture}
\draw (0em,0em) rectangle (2em,1em) node [midway] (a) {Test};
\node[draw, shape=rectangle, minimum width= 2cm, minimum height=1cm, anchor=center] at (a) {};
\end{tikzpicture}
}
\end{document}


• thanks. I removed the "rotate" part of the question to mark it answered. makes sense to keep it separate. alas, if I have to add em to all the measures, how do I use foreach now? \foreach \sp in {1,...,8} { \draw[fill=yellow] (10em,5em*\sp) rectangle (18em,5.8em*(\sp+1)) node[midway] { \small box \sp }; } – ivo Welch Apr 29 '16 at 2:43
• Place your calculations in curly braces to tell tikz, it needs to calculate here something: \foreach \sp in {1,...,8} { \draw[fill=yellow] (10em,{5em*\sp}) rectangle (18em,{5.8em*(\sp+1)}) node[midway] { \small box \sp }; }; – JMP Apr 29 '16 at 7:33