54

I'm finally trying out TikZ and am already infected with its programmatic philosophy. I would like to be able to do something like

every node/.style = {math mode}

(which is wrong) so that I can omit the $...$ inside the text of my nodes. This is not just for convenience; without it, the equally appealing setting

every node/.style = {font=\scriptstyle}

is wrong (the \scriptstyle goes outside the math mode), and I can certainly find a use for math nodes set in small text. I know I can get this, sort of, with the matrix library (using a matrix of math nodes), but I am resisting using matrices. I really like the idea of relational positioning and I hate all the empty &'s that complex use of matrices always requires.

So, is this possible? How does matrix of math nodes achieve its effect?

8
  • Please close this. Or at least, could an admin make it CW? I do not deserve any reputation for asking it.
    – Ryan Reich
    Commented Nov 17, 2010 at 0:12
  • 3
    @Ryan: You definitely deserve reputation from this! I was wondering how to do something similar (with fixed text) the other day, and I wouldn't have known the answer without this question (I didn't think of matrix of math nodes). Just because it's easy to answer, doesn't mean it's bad. Commented Nov 17, 2010 at 0:25
  • 3
    Another day, another thing learned about TikZ.
    – Caramdir
    Commented Nov 17, 2010 at 4:43
  • 3
    @Ryan: Great question! Commented Nov 17, 2010 at 9:43
  • 2
    Okay, I have to admit that after two years, the popular appeal of this question has proven to me that it is not stupid.
    – Ryan Reich
    Commented Jul 25, 2012 at 15:36

2 Answers 2

41

In the matrix library file, at texmf/tex/generic/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarymatrix.code.tex (what you get with \usetikzlibrary{matrix}, we can find the definition of matrix of math nodes (in my version, it's on lines 74–81):

% Same as a matrix of nodes, but switch on math mode in each cell
\tikzstyle{matrix of math nodes}=[%
  matrix of nodes,
  nodes={%
   execute at begin node=$,%
   execute at end node=$%
  }%
]

So you can just use execute at begin node/execute at end node. Interestingly, these keys don't seem to be documented, at least in my version of the manual. If you want a math mode style, then you can write \tikzset{math mode/.style = {execute at begin node=$, execute at end node=$}}; then just including math mode in your options list will do what you want (no need for every node/.style, since the option implicitly affects all nodes).

These options can do more than just math, too; consider the following code:

\documentclass{amsart}
\usepackage{tikz}

\begin{document}
  \begin{center}\begin{tikzpicture}
    \begin{scope}[execute at begin node=$, execute at end node=$]
      \node at (-1.5,-0.375) {\mathfrak{A} \models \varphi_i} ;
      \node at (-1.5,-0.875) {0 \in \mathbb{N}} ;
    \end{scope}

    \begin{scope}[ color                 = blue
                 , execute at begin node = $\displaystyle
                 , execute at end node   = $]
      \node at (+1.5, 0.00) {\sum_{i=0}^\infty \frac{1}{2^n} = 2} ;
      \node at (+1.5,-1.25) {n! = \prod_{i=1}^n i} ;
    \end{scope}

    \begin{scope}
      [ execute at begin node=\textcolor{red}{\textbf{Important notice:} },
      , execute at end node={{} --- \textit{The Management}} ]
      \node at (0,-2.5) {\TeX{} is very powerful.} ;
      \node at (0,-3.0) {Ti\textit{k}Z is a useful graphics language.} ;
    \end{scope}
  \end{tikzpicture}\end{center}
\end{document}

This produces the following picture:

Sample use of the above options.

The option, as we can see, can be used for more than just $...$; it works while specifying other options, or with arbitrary text. (Note that, if specifying arbitrary text, be careful with your leading/trailing spaces; it can be tough to get them to appear.) It doesn't work with \[...\] (probably because that enters vmode), but $\displaystyle...$ works fine.


Edit: As Ryan points out, it's worth clarifying that the TeX which is an argument to font is inserted before the TeX which is an argument to execute at begin node, which is relevant in that using the above style to get math nodes, font=\scriptstyle doesn't work, since it goes outside the $...$. (You need execute at begin node=$\scriptstyle, execute at end node=$.)

4
  • It is definitely not documented, though the similar style execute at begin cell (for matrices) is. Not that I would have thought to look there for math mode.
    – Ryan Reich
    Commented Nov 17, 2010 at 0:31
  • Since, as you say, your answer says "the same thing" as mine, you won't object if I accept it.
    – Ryan Reich
    Commented Nov 17, 2010 at 8:07
  • 1
    This is not a criticism of your answer, but I feel like I should record the unfortunate fact that one still cannot use font = \scriptstyle even with this method. This is because tikz installs the font before the at begin node material (as would make sense, if indeed this material were text to prefix the node and not formatting code). One must use execute at begin node = \scriptstyle after creating a "math node" in the above manner.
    – Ryan Reich
    Commented Nov 22, 2010 at 13:08
  • @Ryan: Good point, I added that to the answer. Commented Nov 22, 2010 at 22:18
12

I probably shouldn't have asked this in the first place, as the most cursory glance into tikzlibrarymatrix.code.tex answers it instantly. For posterity (since it is not obvious to me, as a beginner) I record the answer to my second question.

\tikzstyle{matrix of math nodes}=[%
  matrix of nodes,
  nodes={%
   execute at begin node=$,%
   execute at end node=$%
  }%
]

So, to answer my first question, one should just use execute at begin node and execute at end node to wrap the text in $'s.

1
  • Oh, I didn't see your answer—mine pretty much says the same thing :-) Commented Nov 17, 2010 at 0:24

You must log in to answer this question.

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