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?

  • Please close this. Or at least, could an admin make it CW? I do not deserve any reputation for asking it.
    – Ryan Reich
    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. Nov 17, 2010 at 0:25
  • 3
    Another day, another thing learned about TikZ.
    – Caramdir
    Nov 17, 2010 at 4:43
  • 3
    @Ryan: Great question! 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
    Jul 25, 2012 at 15:36

2 Answers 2


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,
   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:


    \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}} ;

    \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} ;

      [ 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.} ;

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=$.)

  • 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
    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
    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
    Nov 22, 2010 at 13:08
  • @Ryan: Good point, I added that to the answer. Nov 22, 2010 at 22:18

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,
   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.

  • Oh, I didn't see your answer—mine pretty much says the same thing :-) 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 .