7

I have a macro to box (parts of) equations with tikz. I want to use mathchoice to remember the style and I want to label the tikz nodes for later reference. However, it does not work.

MWE:

\documentclass{article}

\usepackage[customcolors,shade]{hf-tikz}
\usepackage[]{tikz}

\tikzset{
  tbx-base/.style={ fill opacity=0.4, draw ,
  color=red, fill=red!80, },
}

\newcommand{\tbx}[3]{
  \mathchoice%
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\displaystyle#3$}};}
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\textstyle#3$}};}
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\scriptstyle#3$}};}
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\scriptscriptstyle#3$}};}
}

\newcommand{\tbxx}[3]{
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\displaystyle#3$}};}
}

\begin{document}
\[
  \tbx{tbx-base}{nodeEkin1}{A}
  +
  \tbxx{tbx-base}{nodeEkin2}{A}
\]
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin01) {}; \\
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin02) {};
\begin{tikzpicture}[remember picture,overlay]
  \path[-stealth,draw] (nodeEkin01) edge [out=0, in=-90] (nodeEkin1);
  \path[-stealth,draw] (nodeEkin02) edge [out=0, in=-90] (nodeEkin2);
\end{tikzpicture}
\end{document}

The arrow to the box created with \tbxx is correct, but \tbxx does not allow for the correct size (if, e.g. used in fractions or indices). \tbx allows for correct sizing, but not for correct referencing of the tikz nodes.

Is there a possibility to get the advantages of both macros?

1 Answer 1

6

The following example remembers the math style in a \label. Each argument of \mathchoice gets the same \label that is put in a deferred \write node. Later, when TeX makes its choice, only the correct \write node is executed.

Because the example uses references, two LaTeX runs are needed:

\documentclass{article}

\usepackage[customcolors,shade]{hf-tikz}
\usepackage[]{tikz}

\tikzset{
  tbx-base/.style={ fill opacity=0.4, draw ,
  color=red, fill=red!80, },
}

\usepackage{refcount}

\makeatletter
\newcounter{tbx}
\renewcommand*{\thetbx}{tbx@\the\value{tbx}}
\newcommand*{\tbxstyle}{%
  \refused{\thetbx}%
  \ifcase\getrefbykeydefault{\thetbx}{}{0} %
    \displaystyle
  \or\textstyle
  \or\scriptstyle
  \or\scriptscriptstyle
  \fi
}
\newcommand{\tbx}[3]{%
  \stepcounter{tbx}%
  \mathchoice
  {\def\@currentlabel{0}\label{\thetbx}}%
  {\def\@currentlabel{1}\label{\thetbx}}%
  {\def\@currentlabel{2}\label{\thetbx}}%
  {\def\@currentlabel{3}\label{\thetbx}}%
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor{$\tbxstyle#3$}};}
}
\makeatother

\begin{document}
\[
  \tbx{tbx-base}{nodeEkin1}{A}
  _{\tbx{tbx-base}{nodeEkin2}{A}}
\]
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin01) {};
\\
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin02) {};
\begin{tikzpicture}[remember picture,overlay]
  \path[-stealth,draw] (nodeEkin01) edge [out=0, in=-90] (nodeEkin1);
  \path[-stealth,draw] (nodeEkin02) edge [out=0, in=-90] (nodeEkin2);
\end{tikzpicture}
\end{document}

Result

Remarks:

  • The math style is encoded as plain integer number, 0 for \displaystyle, 1 for \textstyle, ...

  • The reference data is extracted with the help of package refcount.

  • The label names are automatically generated with the help of counter tbx.

Package refmathstyle

For easier usage I have written a package refmathstyle that saves and references the current math style via macros \labelmathstyle and \refmathstyle. Until the next release of my bundle, the package is also available here (The .dtx file is embedded as PDF attachment. Then run tex refmathstyle.dtx (not latex) to extract the package file.)

The example simplifies to:

\documentclass{article}

\usepackage[customcolors,shade]{hf-tikz}
\usepackage[]{tikz}

\tikzset{
  tbx-base/.style={ fill opacity=0.4, draw ,
  color=red, fill=red!80, },
}

\usepackage{refmathstyle}
\newcommand{\tbx}[3]{%
  \labelmathstyle{}%
  {\tikz[remember picture] \node[#1] (#2) {\normalcolor$\refmathstyle{}#3$};}
}

\begin{document}
\[
  \tbx{tbx-base}{nodeEkin1}{A}
  _{\tbx{tbx-base}{nodeEkin2}{A}}
\]
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin01) {};
\\
energy \tikz[remember picture,overlay, baseline=-.5ex]\node (nodeEkin02) {};
\begin{tikzpicture}[remember picture,overlay]
  \path[-stealth,draw] (nodeEkin01) edge [out=0, in=-90] (nodeEkin1);
  \path[-stealth,draw] (nodeEkin02) edge [out=0, in=-90] (nodeEkin2);
\end{tikzpicture}
\end{document}
3
  • You mention "until the next release of my bundle"; however, it doesn't seem to be on CTAN yet. Do you have an idea as to when you plan to submit it to CTAN?
    – JP-Ellis
    Commented Dec 15, 2015 at 10:33
  • Sorry to bump this comment again, but do you think you could publish refmathstyle to CTAN? It still doesn't look like you've done that yet.
    – JP-Ellis
    Commented May 25, 2017 at 14:47
  • I don't know how much time you have for TeX these days, but may I ask, as JP-Ellis did, whether the package will make its way to CTAN anytime? (Especially since the Dropbox-link is dead.) Commented Nov 21, 2017 at 23:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .