Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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
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. –  Antal S-Z Nov 17 '10 at 0:25
3  
Another day, another thing learned about TikZ. –  Caramdir Nov 17 '10 at 4:43
3  
@Ryan: Great question! –  Hendrik Vogt Nov 17 '10 at 9:43
    
@Ryan: Completely agree with the comments other than yours! Just because you figured out the answer shortly after posting doesn't mean that anyone else would have done so. –  Andrew Stacey Nov 17 '10 at 10:17
show 3 more comments

2 Answers

up vote 29 down vote accepted

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

share|improve this answer
    
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
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 '10 at 13:08
    
@Ryan: Good point, I added that to the answer. –  Antal S-Z Nov 22 '10 at 22:18
add comment

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.

share|improve this answer
    
Oh, I didn't see your answer—mine pretty much says the same thing :-) –  Antal S-Z Nov 17 '10 at 0:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.