如何在 TikZ 中结合方程式和图形微积分?

如何在 TikZ 中结合方程式和图形微积分?

我正在写一篇关于范畴论的文章,​​我的很多方程都是图形化的,如下所示:

图解微积分与方程

直线(以及后来的弯线和圆圈)表示阿贝尔范畴中的态射。

但是它看起来一点也不好,尤其是间距。你可以看到两个和的垂直间距是如何混乱的,等号的位置是相当随意手工完成的,结果很平庸。前因子\dim Y到线的距离太大。线和节点标签的放置是由 TikZ 完成的,看起来还不错。有没有一个好的包或首选方法可以自动完成所有这些并产生好看的结果?

我的代码如下:

\documentclass{article}

\usepackage{tikz}
\usepackage{mathtools}

\begin{document}
    \begin{tikzpicture}[thick, dot/.style={circle,fill,inner sep=1pt}]
        %TODO Get vertical spacing of sums right
        \draw (0,0) -- node[right] {$X$} (0,3);
        \node at (1,1.5) {=};
        \newcommand{\inserted}[2]{\draw (#1,0) -- node[right] {$X$} +(0,1) node[dot] {} node[right] {$\iota$} -- node[right] {$#2$} +(0,2) node[dot] {} node[right] {$\iota^*$} -- node[right] {$X$} +(0,3);}
        \node at (1.9,1.5) {$\displaystyle\sum_{\iota \in \mathcal{C}(X,\Omega_\mathcal{C})}$};
        \inserted{2.8}{\Omega_\mathcal{C}}
        \node at (3.8,1.5) {=};
        \node at (5.0,1.5) {$\displaystyle\sum_{\mathclap{\substack{Y \in \Lambda_\mathcal{C} \\ \iota \in \mathcal{C}(X,Y)}}} \dim Y$};
        \inserted{6.2}{Y}
    \end{tikzpicture}
\end{document}

答案1

您可以使用baseline=(X.base)TikZ 的键来获取正确的基线。现在,您可以选择 tikzpicture 内的任何节点作为基线并将其命名为(X)。要在多线方程中获取它,只需使用其中一个多线amsmath环境,例如align

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz,amsmath,amssymb}
\tikzset{
    dot/.style = {
        draw,fill,
        circle,
        inner sep=0pt,
        outer sep=0pt,
        minimum size=2pt
    }
}
\newcommand\stikz[1]{\tikz[baseline=(X.base)]{#1}}
\begin{document}
\begin{align*}
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] (X) at (0,0) {$X$};
    }
    &=
    \stikz{\node (X) {$\displaystyle\sum_{\iota \in \mathcal{C}(X,\Omega_\mathcal{C})}$};}
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] at (0,0.75) {$X$};
        \node[dot,label={right:$\iota^*$}] at (0,0.4) {};
        \node[right] (X) at (0,0) {$\Omega_\mathcal{C}$};
        \node[dot,label={right:$\iota$}] at (0,-0.4) {};
        \node[right] at (0,-0.75) {$X$};
    }
    =
    \stikz{\node (X) {$\displaystyle\sum_{\substack{Y \in \Lambda_\mathcal{C} \\ \iota \in \mathcal{C}(X,Y)}} \dim Y$};}
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] at (0,0.75) {$X$};
        \node[dot,label={right:$\iota^*$}] at (0,0.4) {};
        \node[right] (X) at (0,0) {$Y$};
        \node[dot,label={right:$\iota$}] at (0,-0.4) {};
        \node[right] at (0,-0.75) {$X$};
    }
\\
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] (X) at (0,0) {$Y$};
    }
    &=
    \stikz{\node (X) {$\displaystyle\sum_{\kappa \in \mathcal{C}(Y,\Gamma_\mathcal{C})}$};}
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] at (0,0.75) {$Y$};
        \node[dot,label={right:$\kappa^*$}] at (0,0.4) {};
        \node[right] (X) at (0,0) {$\Gamma_\mathcal{C}$};
        \node[dot,label={right:$\kappa$}] at (0,-0.4) {};
        \node[right] at (0,-0.75) {$Y$};
    }
    =
    \stikz{\node (X) {$\displaystyle\sum_{\substack{Y \in \Phi_\mathcal{C} \\ \kappa \in \mathcal{C}(Y,Z)}} \dim Z$};}
    \stikz{
        \draw (0,-1) -- (0,1);
        \node[right] at (0,0.75) {$Y$};
        \node[dot,label={right:$\kappa^*$}] at (0,0.4) {};
        \node[right] (X) at (0,0) {$Z$};
        \node[dot,label={right:$\kappa$}] at (0,-0.4) {};
        \node[right] at (0,-0.75) {$Y$};
    }
\end{align*}
\end{document}

在此处输入图片描述

相关内容