Graphics, Figures & TablestikZ | Font Scaling in new Command

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
php1ic
Posts: 192
Joined: Wed Jan 28, 2009 8:17 pm

tikZ | Font Scaling in new Command

Post by php1ic »

Hello,

I've created a new command that uses tikZ to draw a box at of a certain colour at x,y and add centered text of non-standard (bigger than \Huge) size. I made the following MWE using the random scale=5 but when I change it the fonts and rounded corners are 'wrong'.

I cannot seem to get round the fact that the \fontsize is outside of the tikzpicture environment so doesn't scale with everything else. I tried using transform canvas={scale=??} but that didn't work.

I basically want everything to scale if I change the value. Any ideas?

For context I have a code that will create between 1 and 4500 instance of the box so would like to easily set the scale for a small, 1/3rd page, portrait picture or a full landscape one.

Code: Select all

\documentclass{article}

\usepackage{tikz}
\usepackage{fix-cm}
\usepackage[T1]{fontenc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0.5em}

%Set scale factor
\newcommand{\nsize}[0]{5}

%Set how round the corners are (0=square)
\newcommand{\rc}[0]{40}

%Setup command to draw box and text
\newcommand{\nucleus}[5]{
\filldraw[draw=black,thick,fill=#1,rounded corners=\rc] (#2,#3) rectangle +(1,1)
+(0.5,0.75) node {\fontsize{40}{48} \selectfont #4}
+(0.5,0.4) node {\fontsize{72}{96} \selectfont #5};
}

\begin{document}

\begin{tikzpicture}[scale=\nsize]

\nucleus{green}{0}{0}{208}{Pb}
%\draw[step=0.1,gray,very thin] (-0.05,-0.05) grid (1.05,1.05);

\end{tikzpicture}

\end{document}
Last edited by php1ic on Mon Sep 24, 2012 11:17 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

tikZ | Font Scaling in new Command

Post by Stefan Kottwitz »

Generally, it can be good to add the option transform shape, when scaling:

Code: Select all

\begin{tikzpicture}[scale=2, transform shape]
  ...
\end{tikzpicture}
In this case, I would not use the \fontsize commands with absolute values in the TikZ picture.

Stefan
LaTeX.org admin
php1ic
Posts: 192
Joined: Wed Jan 28, 2009 8:17 pm

tikZ | Font Scaling in new Command

Post by php1ic »

Thanks Stefan,

Using the standard font sizes solved the scaling issue.

Code: Select all

%Setup command to draw box and text
\newcommand{\nucleus}[5]{
\filldraw[draw=black,thick,fill=#1,rounded corners=\rc] (#2,#3) rectangle +(1,1)
+(0.5,0.76) node[anchor=mid] {#4}
+(0.5,0.25) node[anchor=mid] {\LARGE #5};
}
I have read the pgf manual but can't find the section that explains the logic of the rounded corners, any tips? As with the fonts it seems to be an absolute value that doesn't scale.
php1ic
Posts: 192
Joined: Wed Jan 28, 2009 8:17 pm

tikZ | Font Scaling in new Command

Post by php1ic »

By trail and error, a unit square can be converted to a circle by using rounded corners=10*sqrt(2). From there is was a simple case of reading about pgfmath and implementing ;)

Working example is below, only \nsize and \nround need to be changed in the pre-amble

Code: Select all

\documentclass{article}

\usepackage{tikz}
\usepackage[T1]{fontenc}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{0.5em}

%Set scale factor
\newcommand{\nsize}[0]{5}
%Set how round the corners are (0->1=square->circle)
\newcommand{\nround}[0]{0.5}

%Construct the variable to apply the necessary rounding
\pgfmathsetmacro{\rc}{\nround*\nsize*10*sqrt(2)}

%Setup command to draw box and text
\newcommand{\nucleus}[5]{
\filldraw[draw=black,thick,fill=#1,rounded corners=\rc] (#2,#3) rectangle +(1,1)
+(0.5,0.76) node[anchor=mid] {#4}
+(0.5,0.25) node[anchor=mid] {\LARGE #5};
}

\begin{document}

\begin{tikzpicture}[scale=\nsize, transform shape]

\nucleus{green}{0}{0}{208}{Pb}
%\draw[step=0.1,gray,very thin] (-0.05,-0.05) grid (1.05,1.05);

\end{tikzpicture}

\end{document}
Thanks for looking, thinking and commenting.
Post Reply