我有一个宏,用于用 tikz 框选(部分)方程式。我想使用 mathchoice 来记住样式,并标记 tikz 节点以供以后参考。但是,它不起作用。
梅威瑟:
\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}
使用 \tbxx 创建的框的箭头是正确的,但是 \tbxx 不允许正确的大小(例如,用于分数或指数)。 \tbx 允许正确的大小,但不允许正确引用 tikz 节点。
是否有可能同时发挥这两个宏的优点?
答案1
以下示例记住了 中的数学样式\label
。 的每个参数都与放入延迟节点中的\mathchoice
参数相同。 稍后,当 TeX 做出选择时,只会执行正确的节点。\label
\write
\write
因为示例使用了参考文献,所以需要运行两次 LaTeX:
\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}
评论:
数学样式被编码为普通整数,0 表示
\displaystyle
,1
为\textstyle
,...借助包来提取参考数据
refcount
。标签名称在计数器的帮助下自动生成
tbx
。
包裹refmathstyle
为了方便使用,我编写了一个包,它通过宏和refmathstyle
保存并引用当前的数学样式。在我的软件包的下一个版本发布之前,该包也可用\labelmathstyle
\refmathstyle
这里
(该.dtx
文件以 PDF 附件形式嵌入。然后运行tex refmathstyle.dtx
(不是latex
)来提取包文件。)
该示例简化为:
\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}