0

I'd like to scale a TikZ picture based on font size surrounding the environment.

For example...

\documentclass{standalone}                                                                                                   
                                                                                                                             
\usepackage{tikz}      
                                                                                                                             
\begin{document}                                                                                                             
                                                             
{                                                                                                                            
\begin{tikzpicture}                                          
  \draw (-1,-1) rectangle (1,1);                                                                                             
  \node at (0,0) {Hello world};                      
\end{tikzpicture}                                                                                                            
}                                 
                                                                                                                             
{                                                                                                                            
\Huge                             
\begin{tikzpicture}                                                                                                          
  \draw (-1,-1) rectangle (1,1);                   
  \node at (0,0) {Hello world};                                                                                              
\end{tikzpicture}                                  
}                                                                                                                            
                                                   
{                                                                                                                            
\tiny                                                                                                                        
\begin{tikzpicture}      
  \draw (-1,-1) rectangle (1,1);                                                                                             
  \node at (0,0) {Hello world};                                                                                              
\end{tikzpicture}      
}                                                                                                                            
                                                                                                                             
\end{document}      

I expect the second and third of these images to look like the first one, but scaled. Is it possible to use the x= and y= keys on the TikZ picture environment to allow for scaling of the image relative to the current font size?


EDIT

After a little more work I came up with the following...

\documentclass{standalone}

\usepackage{tikz}
\usepackage{printlen}

\newlength{\textsizenow}
\makeatletter
\newcommand{\refreshtextsizenow}{%
  \setlength{\textsizenow}{\f@size pt}
  }
\makeatother

\newcommand{\example}{%
  \refreshtextsizenow
  \begin{tikzpicture}[x=\textsizenow,y=\textsizenow]
    \draw (-3,-3) rectangle (3,3);
    \node at (0,0) {Hello world};
  \end{tikzpicture}
  }

\begin{document}

{
\Huge
\example
}

{
\huge
\example
}

{
\LARGE
\example
}

{
\Large
\example
}

{
\large
\example
}

{
\normalsize
\example
}

{
\small
\example
}

{
\footnotesize
\example
}

{
\scriptsize
\example
}

{
\tiny
\example
}

\end{document}

It uses \f@size to set a length before each image is drawn. I then use the x=...,y=... keys to set the coordinate size. This seems to get close to what I am trying to do. However, if the code above is compiled, the horizontal spacing between the text and the sides shrink as the text size gets smaller. The vertical spacing seems somewhat consistent. I expect both the horizontal and vertical spacing to stay the same. Is this possible to fix?

7
  • 1
    I have two problems with your code: 1) It can‘t compile. Please fix it. 2) What does it display? How can a set of circles or their fragments yield something readable as text? A screenshot may clarify. Please EDIT your question accordingly. Thanks
    – MS-SPO
    Commented May 8, 2023 at 3:06
  • 1
    1. Done. 2. It displays an image. Doesn't matter what image. The image can be a rectangle for all it matters. It displays text within the \node at the end of the picture environment.
    – Logan
    Commented May 8, 2023 at 3:10
  • 1
    I have updated the code to be much simpler, but the question still remains the same.
    – Logan
    Commented May 8, 2023 at 3:20
  • 1
    The usual approach is using [scale=3, transform shape] together. scale scales, transform shape cares for other geometrical relationships. See the manual for details. To adjust font sizes the way you describe you may need some of the if-related packages. Try in your search engine: ctan if .
    – MS-SPO
    Commented May 8, 2023 at 5:13
  • 1
    Welcome to TeX.SX! Maybe you can just use em as unit in your TikZ environment which is a unit that is relative to the font size. Commented May 8, 2023 at 6:20

1 Answer 1

0

For font size dependent images, the following options can be used

\begin{tikzpicture}[x=1em,y=1em]
    \draw (-3,-3) rectangle (3,3);
    \node at (0,0) {Hello world};
\end{tikzpicture}

This sets the coordinate system so 1 is actually 1em. `em's are a unit whose value are relative to the current font size. Considerations such as font family must also be taken into account as kerning or symbol sizes may differ from one font family to another.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .