如何使用命令后附加功能定义 tikzstyle?

如何使用命令后附加功能定义 tikzstyle?

我正在尝试定义一种风格来帮助我的学生理解方程式的解法。然而,我有两个问题:

1:我无法在解决方案的各个步骤之间制作灰色箭头。2:如何智能地使用这种样式为解决方案添加步骤?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning,shapes}
\begin{document}

\tikzset{
    eq/.style={minimum width=3cm,rectangle},
    bubble/.style={draw,help lines,ellipse},
}

%%%%%%%%%%%%%%%%%%%% What I would like to automate %%%%%%%%%%%%%%%%%%%%
\begin{tikzpicture}
    \node[eq]   (first) {$e=mc^2$};
    \node[bubble]   (left)  [below left=of first] {$\times 2$};
    \node[bubble]   (right) [below right=of first] {$\times 2$};
    \node[eq] (second) [below right=of left] {$2e=2mc^2$};
    \draw[help lines] (first.west) to [out=180,in=90] (left.north);
    \draw[help lines] (first.east) to [out=0,in=95] (right.north);
    \draw[->,help lines] (left.south) to [out=-90,in=180] (second.west);
    \draw[->,help lines] (right.south) to [out=-90,in=0] (second.east);

    \node[bubble]   (lleft) [below left=of second] {$-1$};
    \node[bubble]   (rright) [below right=of second] {$-1$};
    \node[eq] (third) [below right=of lleft] {$2e-1=2mc^2-1$};
    \draw[help lines] (second.west) to [out=180,in=90] (lleft.north);
    \draw[help lines] (second.east) to [out=0,in=95] (rright.north);
    \draw[->,help lines] (lleft.south) to [out=-90,in=180] (third.west);
    \draw[->,help lines] (rright.south) to [out=-90,in=0] (third.east);

\end{tikzpicture}

\vspace{1cm}

%%%%%%%%%%%%%%%%%%%% Intent of automation %%%%%%%%%%%%%%%%%%%%
\tikzset{%
    nexteq/.style n args={2}{%
        append after command={
            \pgfextra{\let\mainnode=\tikzlastnode}
            node[bubble]    (left) [below left=of \mainnode] {#1}
            node[bubble]    (right) [below right=of \mainnode] {#1}
            node[eq] (second) [below right=of left] {#2}
            (\mainnode.west) to [out=180,in=90] (left)
            (\mainnode.east) to [out=0,in=95] (right.north)
            (left.south) to [out=-90,in=180] (second.west)
            (right.south) to [out=-90,in=0] (second.east)
        },
    }
}

\begin{tikzpicture}
\draw node(first) [eq,nexteq={$\times 2$}{$2e=2mc^2$}] {$e=mc2$};
\end{tikzpicture}

\end{document}

答案1

tikz-cd在这种情况下,我发现它出奇地适合。

\documentclass[tikz,border=9]{standalone}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\begin{document}

%%%%%%%%%%%%%%%%%%%% What YOU would like to automate %%%%%%%%%%%%%%%%%%%%
\begin{tikzcd}[row sep=30]
    ax^2+bx+c=0
                                                             \dar[bend left=80][description,ellipse,draw]{\div a} \\
    x^2+\frac bax+\frac ca=0
                                                             \dar[bend left=80][description,ellipse,draw]{+\frac{b^2}{4a^2}-\frac ca} \\
    x^2+\frac bax+\frac{b^2}{4a^2}=\frac{b^2}{4a^2}-\frac ca
                                                             \dar[bend left=80][description,ellipse,draw]{=} \\
    \left(x+\frac b{2a}\right)^2=\frac{b^2}{4a^2}-\frac ca
                                                             \dar[bend left=80][description,ellipse,draw]{\sqrt{~}} \\
    x+\frac b{2a}=\sqrt{\frac{b^2}{4a^2}-\frac ca}
                                                             \dar[bend left=80][description,ellipse,draw]{-\frac b{2a}} \\
    x=-\frac b{2a}+\sqrt{\frac{b^2}{4a^2}-\frac ca}
                                                             \dar[bend left=80][description,ellipse,draw]{=} \\
    x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}
\end{tikzcd}

%%%%%%%%%%%%%%%%%%%% Intent of automation %%%%%%%%%%%%%%%%%%%%
\def\dear#1{\dar[bend left=80][description,ellipse,draw]{#1}}
\begin{tikzcd}[row sep=30]
    ax^2+bx+c=0
                                                             \dear{\div a}\\
    x^2+\frac bax+\frac ca=0 
                                                             \dear{+\frac{b^2}{4a^2}-\frac ca} \\
    x^2+\frac bax+\frac{b^2}{4a^2}=\frac{b^2}{4a^2}-\frac ca
                                                             \dear{=} \\
    \left(x+\frac b{2a}\right)^2=\frac{b^2}{4a^2}-\frac ca 
                                                             \dear{\sqrt{~}} \\
    x+\frac b{2a}=\pm\sqrt{\frac{b^2}{4a^2}-\frac ca}
                                                             \dear{-\frac b{2a}} \\
    x=-\frac b{2a}\pm\sqrt{\frac{b^2}{4a^2}-\frac ca}
                                                             \dear{=} \\
    x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}
\end{tikzcd}

\end{document}

答案2

另一种方法,这里的节点被命名为 0,1,...

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,shapes}

\newcounter{i}
\setcounter{i}{0}

\tikzset{
    eq/.style={minimum width=3cm,rectangle,name=\thei},
    bubble/.style={draw,help lines,ellipse}}
\begin{document}

\def\step#1#2{%
    \node[bubble]   (left)  [below left=of \thei] {#1};
    \node[bubble]   (right) [below right=of \thei] {#1};
    \draw[help lines] (\thei .west) to [out=180,in=90] (left.north);
    \draw[help lines] (\thei .east) to [out=0,in=95] (right.north);
    \stepcounter{i}
    \node[eq] [below right=of left] {#2};
    \draw[->,help lines] (left.south) to [out=-90,in=180] (\thei .west);
    \draw[->,help lines] (right.south) to [out=-90,in=0] (\thei .east);
    }


\begin{tikzpicture}
\node[eq]{$ax+b=c$};
\step{$-b$}{$ax=c-b$}
\step{$\div a$}{$x=\frac{c-b}{a}$}
\end{tikzpicture}

\end{document}

相关内容