我怎样才能防止这个 tikzpicture 跑进我的文本中?

我怎样才能防止这个 tikzpicture 跑进我的文本中?

我正在使用tikzmark定义一些箭头来指向方程中的变量。

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{tikzmark}

\begin{document}
A \emph{complex number} is an expression of the form
\[
  z = \tikzmark{b}b\cdot\tikzmark{i}i+\tikzmark{a}a
  \begin{tikzpicture}[
    , line join=round
    , line cap=round
    , remember picture
    , overlay
    ]

    \draw[<-, thick]
    ([shift={(3pt,-2pt)}]pic cs:b) |- ([shift={(-10pt, -10pt)}]pic cs:b)
    node[anchor=east] {``imaginary part'' $\Im(z)\in\mathbb{R}$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:i) |- ([shift={(-15pt,-25pt)}]pic cs:i)
    node[anchor=east] {``imaginary unit'' $i^2=-1$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:a) |- ([shift={(15pt,-25pt)}]pic cs:a)
    node[anchor=west] {``real part'' $\Re(z)\in\mathbb{R}$};
  \end{tikzpicture}
\]
%The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

在此处输入图片描述

我的问题是取消注释等式后面的文本会产生:

在此处输入图片描述

图片挤到文字里了!可以修复吗?

答案1

据我所知,没有通用的简单解决方案,因为您使用(并且必须使用)覆盖图片,根据定义,这会禁用边界框。但是,您可以做的是测量节点占用的空间calc并将其插入。

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,fit}

\begin{document}
A \emph{complex number} is an expression of the form
\[
  z = \tikzmark{b}b\cdot\tikzmark{i}i+\tikzmark{a}a
  \begin{tikzpicture}[
    , line join=round
    , line cap=round
    , remember picture
    , overlay
    ]

    \draw[<-, thick]
    ([shift={(3pt,-2pt)}]pic cs:b) |- ([shift={(-10pt, -10pt)}]pic cs:b)
    node[anchor=east](a) {``imaginary part'' $\Im(z)\in\mathbb{R}$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:i) |- ([shift={(-15pt,-25pt)}]pic cs:i)
    node[anchor=east](b) {``imaginary unit'' $i^2=-1$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:a) |- ([shift={(15pt,-25pt)}]pic cs:a)
    node[anchor=west](c) {``real part'' $\Re(z)\in\mathbb{R}$};
    \node[fit=(a)(b)(c),inner sep=0pt](f){};
    \path let \p1=($(pic cs:a)-(f.south)$) in
    \pgfextra{\xdef\myh{\y1}};
  \end{tikzpicture}
\vspace*{\myh}
\]
The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

在此处输入图片描述

PS 我并不是完全无法想象让 TiZ 记录可能的边界框,但这需要大手术。

答案2

为了避免这种重叠,可以将复数表达式写在 中node。然后使用包\subnode的命令在其中tikzmark创建subnodes。但由于\subnode无法完美定位箭头,我更喜欢使用命令\tikzmarknode,它可以完美地使箭头居中。

我使用了hv pathtikz 手册第 74 页定义的带有粗体箭头的样式,并通过操作绘制了直角的箭头edge

hv path/.style={-{>[sep]},thick,to path={-| (\tikztotarget)}}

由于我不明白的原因,有必要保留该remember picture选项(但没有overlay选项)。如果有专家可以解释原因,我会很感兴趣。

截屏

我评论时没有删除你的原始代码,因为它现在已经没用了。

\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{tikzmark,positioning}

\begin{document}
A \emph{complex number} is an expression of the form

  \begin{tikzpicture}[
    , line join=round
    , line cap=round
    ,hv path/.style={-{>[sep]},thick,to path={-| (\tikztotarget)}}
    ,remember picture
 %   , overlay
    ]
\node (z) at (5,0) {$  z = \tikzmarknode{b}{b}\cdot\tikzmarknode{i}{i}+\tikzmarknode{a}{a}$};

%    \draw[<-, thick]   
%    ([shift={(3pt,-2pt)}]pic cs:b) |- ([shift={(-10pt, -10pt)}]pic cs:b)
    \node[below left = 0mm and 0mm of z] (i-part) {``imaginary part'' $\Im(z)\in\mathbb{R}$} edge[hv path] (pic cs:b);

%    \draw[<-, thick]
%    ([shift={(4pt,-2pt)}]pic cs:i) |- ([shift={(-15pt,-25pt)}]pic cs:i)
    \node[below = 0mm of i-part] (i-unit) {``imaginary unit'' $i^2=-1$}edge[hv path] (pic cs:i);

%    \draw[<-, thick]
%    ([shift={(4pt,-2pt)}]pic cs:a) |- ([shift={(15pt,-25pt)}]pic cs:a)
    \node[right = 2.5cm of i-unit] {``real part'' $\Re(z)\in\mathbb{R}$}edge[hv path] (pic cs:a);
    \end{tikzpicture}

The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

答案3

或者,可以通过保存尺寸来水平伪造覆盖。

请注意,[记住图片] 通过在辅助文件中记录原点位置来工作。叠加将原点置于当前基线/tikzpicture 位置。

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc,fit}

\newsavebox{\mybox}% global
\newlength{\myleft}
\newlength{\myright}

\begin{document}
A \emph{complex number} is an expression of the form
\savebox{\mybox}{\begin{tikzpicture}[
    , line join=round
    , line cap=round
    , remember picture
    , baseline=(origin)
    ]
    \coordinate (origin) at (0,0);

    \draw[<-, thick]
    ([shift={(3pt,-2pt)}]pic cs:b) |- ([shift={(-10pt, -10pt)}]pic cs:b)
    node[anchor=east] {``imaginary part'' $\Im(z)\in\mathbb{R}$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:i) |- ([shift={(-15pt,-25pt)}]pic cs:i)
    node[anchor=east] {``imaginary unit'' $i^2=-1$};

    \draw[<-, thick]
    ([shift={(4pt,-2pt)}]pic cs:a) |- ([shift={(15pt,-25pt)}]pic cs:a)
    node[anchor=west] {``real part'' $\Re(z)\in\mathbb{R}$};

    \pgfextractx{\myleft}{\pgfpointanchor{current bounding box}{west}}
    \global\myleft=\myleft
    \pgfextractx{\myright}{\pgfpointanchor{current bounding box}{east}}
    \global\myright=\myright
\end{tikzpicture}}
\[
  z = \tikzmark{b}b\cdot\tikzmark{i}i+\tikzmark{a}a
  \hspace{\myleft}\usebox\mybox\hspace{-\myright}
\]
The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

答案4

以下是另外两个解决方案。

在第一个解决方案中,我overlay从您的选项中删除了该选项tikzpicture并计算了您的注释的深度(第一个节点的顶​​部和最后一个节点的底部之间的距离)。

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark}
\begin{document}
A \emph{complex number} is an expression of the form
\[
  z = \tikzmark{b}b\cdot\tikzmark{i}i+\tikzmark{a}a
\begin{tikzpicture}[
  , line join=round
  , line cap=round
  , remember picture
  , baseline=0 % to compute depth...
  ]
  \begin{scope}[overlay]
    \draw[<-, thick] ([shift={(3pt,-2pt)}]pic cs:b) |- ([shift={(-10pt, -10pt)}]pic cs:b)
    node[anchor=mid east] (a) {``imaginary part'' $\Im(z)\in\mathbb{R}$};

    \draw[<-, thick] ([shift={(4pt,-2pt)}]pic cs:i) |- ([shift={(-15pt,-25pt)}]pic cs:i)
    node[anchor=mid east] {``imaginary unit'' $i^2=-1$};

    \draw[<-, thick] ([shift={(4pt,-2pt)}]pic cs:a) |- ([shift={(15pt,-25pt)}]pic cs:a)
    node[anchor=mid west] (b) {``real part'' $\Re(z)\in\mathbb{R}$};\
  \end{scope}
  \path let \p1=(a.north west), \p2=(b.south east) in  (0,0) -- (0,\y2-\y1);
\end{tikzpicture}
\]

The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

第二种解决方案使用\tikzmarknodefit计算带边距的数学目标,然后定位具有相同边距的评论节点,最后绘制箭头。语法更常规:没有shift和只有两个距离(2pt边距和1em水平移动)。

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{calc,tikzmark,fit}
\begin{document}
A \emph{complex number} is an expression of the form
\begin{equation}
  z = \tikzmarknode{b}{b}\cdot\tikzmarknode{i}{i}+\tikzmarknode{a}{a}
\begin{tikzpicture}[
  , line join=round
  , line cap=round
  , remember picture
  , baseline=0 % to compute depth
  ]
  \begin{scope}[overlay]
    \tikzset{
      text node/.style={inner sep=2pt},
      fitmath/.style={node contents={},text node,fit=#1},
    }
    % math target nodes
    \node[fitmath=(b),name=bt];
    \node[fitmath=(i),name=it];
    \node[fitmath=(a),name=at];
    % commentary nodes
    \path (bt.south) ++ (-1em,0)
    node[text node,anchor=north east] (bc) {``imaginary part'' $\Im(z)\in\mathbb{R}$};
    \path (bc.south east)
    node[text node,anchor=north east] (ic) {``imaginary unit'' $i^2=-1$};
    \path (ic.mid -| at) ++ (1em,0)
    node[text node,anchor=mid west] (ac)   {``real part'' $\Re(z)\in\mathbb{R}$};
    % arrows between nodes
    \draw[<-, thick] (bt.south) |- (bc.mid east);
    \draw[<-, thick] (it.south) |- (ic.mid east);
    \draw[<-, thick] (at.south) |- (ac.mid west);
  \end{scope}
  \path let \p1=(a.south), \p2=(ac.south) in  (0,0) -- (0,\y2-\y1);
\end{tikzpicture}
\end{equation}

The collection of complex numbers is denoted by $\mathbb{C}$.
\end{document}

相关内容