I would probably do
\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
By setting the baseline to the base
anchor of the node, you don't have to use some specific value. With inner sep=0pt
you wont get any extra space around the text in the node.
Note also that I removed the overlay
option. That option means that you get a zero-size bounding box, which you don't want here.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\tikzset{
ncbar angle/.initial=-90,
ncbar/.style={
to path=(\tikztostart)
-- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
-- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
-- (\tikztotarget)
},
ncbar/.default=0.5cm
}
\begin{document}
blabla \tikznode{A}{A}$\tikznode{B}{B}$\tikznode{C}{C} blabla
\tikznode{X}{X}\tikznode{Y}{Y}\tikznode{Z}{Z} blabla
\begin{tikzpicture}[overlay,remember picture]
\draw[->] (A) to [ncbar=0.4] (X);
\draw[->] (B) to [ncbar=0.5] (Y);
\draw[->] (C) to [ncbar=0.6] (Z);
\end{tikzpicture}
\end{document}
If you want to implement \mathchoice
in this, you get problems, because all the four different choices are typeset. Hence, the naive implementation of
\newcommand{\tikznode}[2]{%
\ifmmode%
\mathchoice
{\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$\displaystyle #2$};}%
{\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$\textstyle #2$};}%
{\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$\scriptstyle #2$};}%
{\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$\scriptscriptstyle #2$};}%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
doesnt't work, because the node name will always refer to the scriptscript-version, which isn't used at all in the three other styles. So for the example above, where the B
is in textstyle, you get an arrow pointing out of the page.
Perhaps unsurprisingly though, someone has wondered about that problem before, and Heiko Oberdiek provided a solution to it in mathchoice and tikz's remember picture. The refmathstyle
package he made has not yet been published on CTAN though, and the link to it in that answer is dead, so here is the non-package version:

\documentclass{article}
\usepackage{tikz}
\usepackage{refcount}
\usetikzlibrary{calc}
% from https://tex.stackexchange.com/questions/122415/mathchoice-and-tikzs-remember-picture
\makeatletter
\newcounter{tikznode}
\renewcommand*{\thetikznode}{tikznode@\the\value{tikznode}}
\newcommand*{\tikznodestyle}{%
\refused{\thetikznode}%
\ifcase\getrefbykeydefault{\thetikznode}{}{0} %
\displaystyle
\or\textstyle
\or\scriptstyle
\or\scriptscriptstyle
\fi
}
\newcommand{\tikznode}[2]{%
\ifmmode%
\stepcounter{tikznode}%
\mathchoice
{\def\@currentlabel{0}\label{\thetikznode}}%
{\def\@currentlabel{1}\label{\thetikznode}}%
{\def\@currentlabel{2}\label{\thetikznode}}%
{\def\@currentlabel{3}\label{\thetikznode}}%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$\tikznodestyle #2$};
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\makeatother
% from https://tex.stackexchange.com/questions/55068/is-there-a-tikz-equivalent-to-the-pstricks-ncbar-command
\tikzset{
ncbar angle/.initial=-90,
ncbar/.style={
to path=(\tikztostart)
-- ($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)
-- ($(\tikztotarget)!($(\tikztostart)!#1!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztotarget)$)!\pgfkeysvalueof{/tikz/ncbar angle}:(\tikztostart)$)
-- (\tikztotarget)
},
ncbar/.default=0.5cm
}
\begin{document}
blabla
$\displaystyle\tikznode{A}{A}\textstyle\tikznode{B}{B}_{\tikznode{C}{C}_{\tikznode{D}{D}}}$
blabla
\tikznode{X}{X}\tikznode{Y}{Y}\tikznode{Z}{Z}\tikznode{W}{W}
blabla
\begin{tikzpicture}[overlay,remember picture]
\draw[->] (A) to [ncbar=0.4] (X);
\draw[->] (B) to [ncbar=0.5] (Y);
\draw[->] (C) to [ncbar=0.6] (Z);
\draw[->] (D) to [ncbar=0.6] (W);
\end{tikzpicture}
\end{document}
\tikz
works in both, and the mode of the text in the node isn't affected by the mode in which the\tikz
is placed.$\tikz{\node(x){x};}$
, thex
is not in math mode, but with$\tikznode{x}{x}$
it is.