LaTeX forum ⇒ Graphics, Figures & TablestikZ | Font Scaling in new Command Topic is solved

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

tikZ | Font Scaling in new Command

Postby php1ic » Mon Sep 17, 2012 10:44 pm

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.

  1. \documentclass{article}
  2.  
  3. \usepackage{tikz}
  4. \usepackage{fix-cm}
  5. \usepackage[T1]{fontenc}
  6. \usepackage[active,tightpage]{preview}
  7. \PreviewEnvironment{tikzpicture}
  8. \setlength\PreviewBorder{0.5em}
  9.  
  10. %Set scale factor
  11. \newcommand{\nsize}[0]{5}
  12.  
  13. %Set how round the corners are (0=square)
  14. \newcommand{\rc}[0]{40}
  15.  
  16. %Setup command to draw box and text
  17. \newcommand{\nucleus}[5]{
  18. \filldraw[draw=black,thick,fill=#1,rounded corners=\rc] (#2,#3) rectangle +(1,1)
  19. +(0.5,0.75) node {\fontsize{40}{48} \selectfont #4}
  20. +(0.5,0.4) node {\fontsize{72}{96} \selectfont #5};
  21. }
  22.  
  23. \begin{document}
  24.  
  25. \begin{tikzpicture}[scale=\nsize]
  26.  
  27. \nucleus{green}{0}{0}{208}{Pb}
  28. %\draw[step=0.1,gray,very thin] (-0.05,-0.05) grid (1.05,1.05);
  29.  
  30. \end{tikzpicture}
  31.  
  32. \end{document}
Last edited by php1ic on Mon Sep 24, 2012 11:17 pm, edited 1 time in total.

User avatar
Stefan Kottwitz
Site Admin
Posts: 9553
Joined: Mon Mar 10, 2008 9:44 pm

Postby Stefan Kottwitz » Tue Sep 18, 2012 10:46 am

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

  1. \begin{tikzpicture}[scale=2, transform shape]
  2. ...
  3. \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

Postby php1ic » Wed Sep 19, 2012 10:45 pm

Thanks Stefan,

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

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

Postby php1ic » Mon Sep 24, 2012 11:16 pm

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
  1. \documentclass{article}
  2.  
  3. \usepackage{tikz}
  4. \usepackage[T1]{fontenc}
  5. \usepackage[active,tightpage]{preview}
  6. \PreviewEnvironment{tikzpicture}
  7. \setlength\PreviewBorder{0.5em}
  8.  
  9. %Set scale factor
  10. \newcommand{\nsize}[0]{5}
  11. %Set how round the corners are (0->1=square->circle)
  12. \newcommand{\nround}[0]{0.5}
  13.  
  14. %Construct the variable to apply the necessary rounding
  15. \pgfmathsetmacro{\rc}{\nround*\nsize*10*sqrt(2)}
  16.  
  17. %Setup command to draw box and text
  18. \newcommand{\nucleus}[5]{
  19. \filldraw[draw=black,thick,fill=#1,rounded corners=\rc] (#2,#3) rectangle +(1,1)
  20. +(0.5,0.76) node[anchor=mid] {#4}
  21. +(0.5,0.25) node[anchor=mid] {\LARGE #5};
  22. }
  23.  
  24. \begin{document}
  25.  
  26. \begin{tikzpicture}[scale=\nsize, transform shape]
  27.  
  28. \nucleus{green}{0}{0}{208}{Pb}
  29. %\draw[step=0.1,gray,very thin] (-0.05,-0.05) grid (1.05,1.05);
  30.  
  31. \end{tikzpicture}
  32.  
  33. \end{document}

Thanks for looking, thinking and commenting.


Return to “Graphics, Figures & Tables”

Who is online

Users browsing this forum: No registered users and 7 guests