# tikzmark for math inside tikz node label

I am looking for a way to mark particular positions within the math-text of node.

My MNWE is:

\documentclass[tikz]{standalone}
\usepackage{mathtools,xparse,bm}
\usetikzlibrary{tikzmark}
\NewDocumentCommand{\outermacro}{mO{}}{ p( #1 \mid #2 ) }
\NewDocumentCommand{\innermacro}{mO{}}{ \bm{x}_{#1}^{#2} }
\begin{document}
\begin{tikzpicture}
%       \node (n1) at (0,0) { $\outermacro{ X }[ \subnode{x}{ \innermacro{2}[z] } ]$ }; % complains about missing $\node (n2) at (2,0) {$\outermacro{ X }[ \innermacro{2}[z] ]$}; % test for both macros, doesn't do what I want \node (n4) at (0.5,1) {this is a note}; \draw (n4) -- (x.center); \end{tikzpicture} \end{document}  For the "real" case,I get the errors as given in the comment. What is the right way of doing this? P.S.: the subnode part, I stole from https://tex.stackexchange.com/a/287727/34538 EDIT: I tried  \begin{tikzpicture} \node (n1) at (0,0) {$\outermacro{ X }[ \pgfmark{x}\innermacro{2}[z] ]$}; % complains about missing$
\node (n2) at (3,0) { $\outermacro{ X }[ \innermacro{2}[z] ]$ }; % test for both macros
\node (n4) at (0.5,1) {this is a note};
\draw (n4) -- (pic cs:x);
\end{tikzpicture}


which compiles, but does not yield a line to correct position.

• Have you try with \subnode? See the documentation of tikzmark pag.7. – vi pa Feb 18 at 12:02
• @vipa, yes, please see the part before the edit (the commented line) – bonanza Feb 18 at 12:03

I was having all sorts of problems with far simpler versions until I hit on [anchor=base]. Only then did the coordinates line up.

I might add that putting a tikzpicture (or tikzmark) inside a node is a bad idea, and using the [tikz] option of standalone puts every tikzpicture on a separate page.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{mathtools,xparse,bm}
\usetikzlibrary{tikzmark}
\NewDocumentCommand{\outermacro}{mO{}}{ p( #1 \mid #2 ) }
\NewDocumentCommand{\innermacro}{mO{}}{ \bm{x}_{#1}^{#2} }

\newsavebox{\tempbox}
\begin{document}
\savebox{\tempbox}{$\outermacro{ X }[\tikzmark{x}\innermacro{2}[z]]$}%
\begin{tikzpicture}[remember picture, every node/.style={anchor=base}]
\node (n1) at (0,0) {\usebox{\tempbox}};
\node (n2) at (3,0) { $\outermacro{ X }[\innermacro{2}[z]]$ }; % test for both macros
\node (n4) at (0.5,1) {this is a note};
\draw (n4) -- (pic cs:x);
\end{tikzpicture}
\end{document}


Using overlay, remember picture solves the problem:

    \begin{tikzpicture}[overlay, remember picture]
\node (n1) at (0,0) { $\outermacro{ X }[ \pgfmark{x}\innermacro{2}[z]\pgfmark{y} ]$ };
\node (n4) at (0.5,1) {this is a note};
\node[fit=(pic cs:x) (pic cs:y)] (target) {};
\draw (n4) -- (target);
\end{tikzpicture}

• No overlay needed here. All text is inside the bounding box. – John Kormylo Feb 16 at 16:12
• @bonanza for use fit you must load the fit library. – vi pa Feb 18 at 12:08