Is there a tikz style for putting math in all nodes?

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 '10 at 0:12
• @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. – Antal Spector-Zabusky Nov 17 '10 at 0:25
• Another day, another thing learned about TikZ. – Caramdir Nov 17 '10 at 4:43
• @Ryan: Great question! – Hendrik Vogt Nov 17 '10 at 9:43
• 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 '12 at 15:36

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: 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 '10 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 '10 at 8:07
• 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 '10 at 13:08
• @Ryan: Good point, I added that to the answer. – Antal Spector-Zabusky Nov 22 '10 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,
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 :-) – Antal Spector-Zabusky Nov 17 '10 at 0:24
{"ft_lang_label":"__label__en","ft_lang_prob":0.81721276,"math_prob":0.79315203,"size":2643,"snap":"2019-43-2019-47","text_gpt3_token_len":778,"char_repetition_ratio":0.15157257,"word_repetition_ratio":0.024570024,"special_character_ratio":0.29738933,"punctuation_ratio":0.16728625,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98954666,"pos_list":[0,1,2],"im_url_duplicate_count":[null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-10-22T22:37:00Z\",\"WARC-Record-ID\":\"<urn:uuid:787a43e6-3ec3-4b8d-bcf1-5b82bfad6887>\",\"Content-Length\":\"160875\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9c46c9d1-2f6c-4353-b47d-42f516c24ed1>\",\"WARC-Concurrent-To\":\"<urn:uuid:dd648bed-830e-4e2e-b511-38d26992de97>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://tex.stackexchange.com/questions/5494/is-there-a-tikz-style-for-putting-math-in-all-nodes/5496\",\"WARC-Payload-Digest\":\"sha1:G4IJ2FQ5H2ZRWB7JV6FNKIKLG5MU7JJ2\",\"WARC-Block-Digest\":\"sha1:UXR36VCLT6LNXWVEPDC5J7JFWLMM5N4K\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-43/CC-MAIN-2019-43_segments_1570987824701.89_warc_CC-MAIN-20191022205851-20191022233351-00373.warc.gz\"}"}