Let's assume I have a TikZ drawing:
- there are two rectangles of different heights
- next to each rectangle there is some text to be added (e.g. A and Z)
- the upper text has to be scalable in the way that it matches the steps of the diagram (here 3 steps or half)
- the lower text has to match the height of the rectangle it ends up next to exactly
- all of the text should be left-aligned at exactly the same grid line
- the whole drawing should be scalable (text and shapes)
My rather clumsy solution was to keep changing the font size until I've reached a result I deemed more or less satisfactory (you can see that it's still far from perfect). Is there any way to calculate this so that I can set a font size of n grid steps so that I can set A to be exactly 6 steps high, for instance?
This is how far I've come:
\documentclass{article}
\RequirePackage{lmodern}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[thick,scale=1, every node/.style={transform shape}]
\draw[step=1cm,gray,thin] (-1,-1) grid (10,10);
\fill[gray] (0,0) -- (4,0) -- (4,2) -- (0,2) -- (0,0);
\draw[red, ultra thick,|<->|] (5,0) -- (5,2);
\node[font={\fontsize{83}{0}\selectfont\bfseries}] at (7,1) {Z};
\fill[gray] (0,3) -- (4,3) -- (4,9) -- (0,9) -- (0,3);
\draw[green, ultra thick,|<->|] (5,3) -- (5,6);
\node[font={\fontsize{124}{0}\selectfont\bfseries}] at (7,4.45) {A};
\end{tikzpicture}
\end{document}
And this is how it looks like:
Update: Further research
Before everything else I'll have to give credit to AndréC for spreading the word and attracting other experts to post comments and answers – thank you!
I have received several very helpful comments which have given me the right key words to further my research.
Apparently, TeX does only know the size of a bounding box of a glyph, but not the size of the glyph itself. I have learned this from a commenter who's opinion I highly regard. I did my due diligence and tried to both confirm and refute her statement. Yet, the more I searched the more it was confirmed (here, here, and there) that there will always be some trial and error involved when it comes to the measurement and exact placement of glyphs/text inside the grid.
\node[font={\fontsize{124}{0}\selectfont\bfseries}] at (7,4.45) {A};
. The problem with this solution: I have to tinker with fontsize and placement until the letter A fits the grid exactly. If I want to resize A to be four grid steps high instead of three like in the picture, I have to start tinkering again. What I wanted to know is whether there is a possibility to set A to be exactly one (or two, or three etc.) grid boxes high.draw,inner sep=0pt,
to your nodes, then you will see the bounding box tex is seeing. So some manual fiddling is unavoidable. But you don't have to do it for every size: useanchor=south west
oranchor=base west
, then you can simply multiple the fontsize by a factor, or use scalebox.