4

Very often I find myself defining styles for various drawing annotations. This is great for structuring figures. There is, however, a minor thing I hope I can improve. This stems from the fact that any relative dimensions (units of ex or em) are always interpreted by TikZ relative to the font size in effect at the "level" of the tikzpicture code itself.

If I need to change the font size of one annotation by using font=\tiny, for instance, I also need to manually tweak the inner sep value, because the default (or whatever is specified in the style definition) does not always look so nice with the changed font size.

If I could set up style definitions so that the value of font is used as the basis for any ex or em dimensions in inner sep, this problem could take care of itself. I would also consider a solution where the font and inner sep values are specified as arguments to a style if it allows this limitation to be overcome.

Here is an MWE:

\documentclass[tikz]{standalone}
\tikzset{
  my inner sep/.style={inner sep=#1}, % how to define this?
  every node/.style={anchor=south west},
}

\begin{document}
\begin{tikzpicture}
  \node[my inner sep=0.2em] (A) {A};
  \node[font=\tiny,my inner sep=0.2em] (B) {B};
  \foreach \node in {A,B} {
    \draw[blue,line width=0.01pt] % just to show the baselines
      (\node.south west) |- (\node.base) -| (\node.north east);
  }
\end{tikzpicture}
\end{document}

The current output is:

enter image description here

The blue hairlines are just to show the baseline of each node. As shown, the baselines are the same distance from the bottom of the nodes. A suitable definition of my inner sep will produce output with text B shifted down and to the left of its position shown above, because 0.2em in \tiny is less than 0.2em in \normalsize.

Is such a goal possible with the current TikZ architecture/order of operations? If so, how can it be done?

3
  • I assume that you know that if you put \tiny within the scope, you get the right result.
    – cfr
    Apr 29, 2015 at 3:14
  • @cfr: whoops, yes, I just had the scope in there for testing. Yes, the font setting in effect during the code (Is there a better way to express this? It's odd because there's really no "text" typeset there.) does apply correctly, but I want to base it off the font in-scope for the node text itself. Apr 29, 2015 at 3:18
  • If you have a style my tiny/.code={\tiny} then you can say \node [my tiny, inner sep=0.2em] .... So maybe you are trying to adapt the wrong thing? Maybe you want my font rather than my inner sep?
    – cfr
    Apr 29, 2015 at 3:24

1 Answer 1

3

From PGF 3.0 there is a node font key which sets the font for the node and is used as the font in node dimension calculations using em and ex. See section 17.4.2 ("Text Parameters: Font") in the PGF 3.0 manual.

\documentclass[tikz, border=5]{standalone}
\begin{document}
\begin{tikzpicture}[every node/.style={fill=gray!50, anchor=south west}]
  \node [inner sep=1em]                  at (0,0) (A) {A};
  \node [font=\tiny,      inner sep=1em] at (0,1) (B) {B};
  \node [node font=\tiny, inner sep=1em] at (0,2) (C) {C};
  \foreach \n in {A,B,C} {
    \draw[red, very thin, <->] (\n.base west) -- (\n.base east);
    \draw[red, very thin, <->] (\n.text |- \n.south) -- (\n.text |- \n.north);
  }
\end{tikzpicture}
\end{document}

enter image description here

3
  • Wow, much simpler than I thought. :-) Thanks Mark! Apr 29, 2015 at 9:40
  • Are there places it's preferable to use font instead of node font or any pitfalls I should be aware of? (Other than the split case discussed in the manual.) Apr 29, 2015 at 10:30
  • @PaulGessler I don't know actually. I think that font key retains the old functionality just for compatibility, so that older code using em and ex produces the same effect with the new PGF version. Apr 29, 2015 at 17:43

You must log in to answer this question.

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