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=$
.)
matrix of math nodes
). Just because it's easy to answer, doesn't mean it's bad.