TikZ 重新定义 \xrightarrow 导致方程式周围出现不平等的线跳跃

TikZ 重新定义 \xrightarrow 导致方程式周围出现不平等的线跳跃

我最近遇到了一些代码重新定义\xrightarrow等问题,以tikz-cd避免可能出现的问题unicode-math。代码如下所示:

\RequirePackage{tikz-cd}
\newbox\xratbelow
\newbox\xratabove
\renewcommand{\xrightarrow}[2][]{%
\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
\mathrel{\tikz [->, baseline=-.75ex]
\draw (0,0) -- node[below=-2pt] {\box\xratbelow}
node[above] {\box\xratabove}
(\xratlen,0) ;}}

我看到这段代码的来源说它来自 Antal Spector-Zabusky,但我没有在这个网站上找到代码。但是这段代码有一个问题:使用baseline=-.75ex会导致等式前后的行不相等。有什么办法可以解决这个问题吗?

以下是 MWE:

\documentclass{article}

\usepackage{amsmath}
\usepackage{parskip}

\RequirePackage{tikz-cd}
\newbox\xratbelow
\newbox\xratabove
\renewcommand{\xrightarrow}[2][]{%
\setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
\setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
\pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .6em}%
\mathrel{\tikz [->, baseline=-.75ex]
\draw (0,0) -- node[below=-2pt] {\box\xratbelow}
node[above] {\box\xratabove}
(\xratlen,0) ;}}

\usepackage{blindtext}

\begin{document}

\blindtext
\[
    A \xrightarrow{\sim} B
\]
\blindtext
\[
    A \overset{\sim}{\to} B
\]
\blindtext

\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

您所说的垂直空间不是由使\baseline箭头处于正确位置所需的命令修改的,​​而是由两个节点的内部空间修改的。

我稍微修改了。在上面的页面上,它用于第二个中心方程。如您所见,现在与使用 的\renewcommand{xrightarrow}第一个方程相比没有区别 。\longrightarrow

评论。什么时候1/x取代~,垂直空间将保持不变。

代码

\documentclass{article}

\usepackage{amsmath}
\usepackage{parskip}

\RequirePackage{tikz-cd}
\newbox\xratbelow
\newbox\xratabove
\renewcommand{\xrightarrow}[2][]{%
  \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
  \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
  \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
  \hspace{.2ex}\mathrel{\tikz[->, baseline=-.75ex,
    every node/.style={inner sep=.5pt}]
    \draw (0,0) -- node[below=2pt] {\box\xratbelow}
    node[above=2pt] {\box\xratabove} (\xratlen, 0);
  }\hspace{.2ex}
}

\usepackage{blindtext}

\begin{document}
\begin{tikzpicture}[remember picture, overlay]
  \draw[red] (0, -3.82) rectangle ++(12.1, -1.3)
  ++(0, .51) -- ++(-12.1, 0);
  \draw[red] (0, -9.16) rectangle ++(12.1, -1.3)
  ++(0, .51) -- ++(-12.1, 0);
\end{tikzpicture}
\blindtext
\[
  A \overset{\sim}{\longrightarrow} B
\]
\blindtext
\[
  A \xrightarrow{\sim} B
\]
\blindtext
\end{document}

答案2

仅供参考,根据 Daniel 的回答,以下是所有那些可伸缩箭头的完整定义。其行为与 不完全相同\overset,但与 Asana Math / Tex Gyre Pagella Math 或类似的数学字体一致。

% The following uses tikz-cd to define retractable arrows to avoid problems with unicode-math.
\newbox\xratbelow
\newbox\xratabove
% Redefine \xrightarrow[below]{above}
\renewcommand{\xrightarrow}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [->, baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) -- node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Redefine \xlefttarrow[below]{above}
\renewcommand{\xleftarrow}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [<-, baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) -- node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Redefine \xleftrightarrow[below]{above}
\renewcommand{\xleftrightarrow}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [<->, baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) -- node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Redefine \xhookrightarrow[below]{above}, using the hookrightarrow from tikz-cd
\renewcommand{\xhookrightarrow}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) edge[commutative diagrams/hookrightarrow] node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Redefine \xhooklefttarrow[below]{above}, using the hookleftarrow from tikz-cd
\renewcommand{\xhookleftarrow}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) edge[commutative diagrams/hookleftarrow] node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Redefine \xmapsto[below]{above}, using the mapsto from tikz-cd
\renewcommand{\xmapsto}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) edge[commutative diagrams/mapsto] node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}
% Define \xlongequal[below]{above}, using the equal from tikz-cd
\newcommand{\xlongequal}[2][]{%
    \setbox\xratbelow=\hbox{\ensuremath{\scriptstyle #1}}%
    \setbox\xratabove=\hbox{\ensuremath{\scriptstyle #2}}%
    \pgfmathsetlengthmacro{\xratlen}{max(\wd\xratbelow, \wd\xratabove) + .8em}%
    \mathrel{\tikz [baseline=-.6ex, every node/.style={inner sep=0pt}]
        \draw (0,0) edge[commutative diagrams/equal] node[below=3pt] {\box\xratbelow}
        node[above=2pt] {\box\xratabove}
        (\xratlen,0) ;}}

相关内容