回溯方程

回溯方程

回溯

我一直在尝试创建可复制回溯框的代码。如果不使用两个不同的 tikz 图片,我无法拆分框。我希望箭头从框中间穿过,并且我希望有足够的空间让学生将答案放入框中。此外,文本在我的图片中是倒置的,我无法使用 $ 符号输入方程式,因为它是文本。

我的 MWE 如下

\documentclass[10pt]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\usetikzlibrary{shapes}
\tikzstyle{data}=[rectangle split,rectangle split parts=2,draw,text centered]   
\usetikzlibrary{decorations.text}
\begin{document}

\begin{tikzpicture}
    \node (One) at (-2,0) [shape=rectangle,draw,  ] {$p$}; 
    \node (Two) at (2,0) [shape=rectangle,draw] {$2p$};
    \node (Three) at (6,0) [shape=rectangle,draw] {$2p - 5$};
        \def\myshift#1{\raisebox{1ex}}
    \draw [->,thick, red, postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift| x 2}}}]      (One) to [bend left=45] (Two);
    \def\myshift#1{\raisebox{1ex}}
    \draw [->,thick, red, postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift| -5 }}}]      (Two) to [bend left=45] (Three);
\def\myshift#1{\raisebox{1ex}}
\draw [->,thick, red, postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift|  5 }}}]      (Three) to [bend left=45] (Two);

\def\myshift#1{\raisebox{1ex}}
\draw [->,thick, red, postaction={decorate,decoration={text along path,text align=center,text={|\sffamily\myshift| div 5  }}}]      (Two) to [bend left=45] (One);  
    
\end{tikzpicture}
\end{document}

答案1

这是实现此目的的一种方法。一些注释:

分割矩形

  • \nodepart{two}可以省略
  • 然而,为了使用\strut元素,我把它们放在
  • \strut是一个宽度为 0 的结构框
  • text环境中,它只是保持高度恒定,因为......
  • ... 高度node根据文本内容而变化

連接

  • 这里不需要装饰,让事情变得更容易
  • 使用to [bend left]只是弯曲线条的一种方式
  • 为了稍微移动箭头的锚点,我使用了极坐标,参见% comments
  • 放置标签的方法有很多种;这里我使用了node {}插入路径
  • 它们的格式txt,并将txb标签移离箭头

结果

\documentclass[10pt,border=3mm,tikz]{standalone}% during development
\usepackage{tikz}
\usetikzlibrary{shapes.multipart,arrows.meta}

\begin{document}

 \begin{tikzpicture}[
    box/.style={rectangle split, rectangle split parts=2,
                draw, %             show the rectangles
                minimum width=20mm% make large for a uniform width
                },
    arr/.style={->, red, >={Stealth}},
    txt/.style={black, yshift=+2.5mm},
    txb/.style={black, yshift=-2.5mm},
 ]
    % ~~~ text boxes ~~~~~~
    %     \strut is a LaTeX text element; effect: makes heights equal
    \node[box] (A) at (0,0) {\strut $p$                 \nodepart{two}\strut};
    \node[box] (B) at (3,0) {\strut $2 \times p$        \nodepart{two}\strut};
    \node[box] (C) at (6,0) {\strut $2 \times p - 5$    \nodepart{two}\strut};
    
    % ~~~ connectors ~~~~~~~
    %     anchors: (A.north) == (A.90); (A.south) == (A.270) 
    \draw [arr] (A.80) to [bend left] node[txt] {$\times 2$} (B.100);   
    \draw [arr] (B.80) to [bend left] node[txt] {$- 5$} (C.100);    
    
    \draw [arr] (C.260) to [bend left] node[txb] {$+ 5$} (B.280);   
    \draw [arr] (B.260) to [bend left] node[txb] {$\div 2$} (A.280);    
    
 \end{tikzpicture}

\end{document}

inner ysep=5mm向样式中添加box...

    box/.style={rectangle split, rectangle split parts=2,
                draw, %             show the rectangles
                minimum width=20mm,% make large for a uniform width
                inner ysep=5mm
                },

结果是

ysep 应用

答案2

如果您多次需要此功能,我建议使用chains可以自动放置节点并可即时连接它们的库。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{arrows.meta, chains, shapes.multipart}
\makeatletter
\tikzset{
  save tikztonodes/.style={every to/.append style/.expanded={
    edge node={\pgfutil@unexpanded\expandafter{\tikz@tonodes}}}}}
\makeatother
\tikzset{
  /utils/temp/.style n args={3}{
    backtracking equations/step #1/.style={edge label={$#2##1$}},
    backtracking equations/step' #1/.style={edge label={$#3##1$}}},
  /utils/temp/.list={++-, --+, *\times\div, :\div\times},
  every backtracking equations diagram/.style={
    start chain=going right, node distance=1em},
  backtracking equations/.cd,
  data/.style={
    rectangle split, draw, minimum width=4.6em,
    rectangle split parts=2, rectangle split horizontal=false},
  every conn/.style={auto=left, nodes={text=blue}, red, >=Latex, bend angle=30},
  arr >/.style 2 args={
    backtracking equations/every conn, ->, backtracking equations/step #1={#2},
    to path={[save tikztonodes] ([xshift=+0.1em]\tikztostart.north)
                  to[bend left] ([xshift=+-.1em]\tikztotarget.north)}},
  arr </.style 2 args={
    backtracking equations/every conn, <-, backtracking equations/step' #1={#2}, ',
    to path={[save tikztonodes] ([xshift=+0.1em]\tikztostart.south)
                 to[bend right] ([xshift=+-.1em]\tikztotarget.south)}}}
\newcommand*\tikzbackeq[2][]{%
  \tikz[every backtracking equations diagram,#1]
    \foreach \content/\step in {#2}
      \node[backtracking equations/data, on chain,
        join=by {backtracking equations/arr >/.expand once=\step},
        join=by {backtracking equations/arr </.expand once=\step}]
        {\strut$\content$ \nodepart{two} \strut};}
\begin{document}
\tikzbackeq{p, 2p/*2, 2p-5/-5}

\tikzbackeq{p, p+3/+3, \frac{p+3}{2}/:2, \pi\frac{p+3}{2}/*\pi}
\end{document}

输出

在此处输入图片描述

在此处输入图片描述

答案3

使用{NiceTabular}nicematrixTikz 绘制箭头。

\documentclass{article}
\usepackage{nicematrix,tikz}
\usetikzlibrary{arrows.meta}

\begin{document}

\newcolumntype{O}{w{c}{1cm}}

\[\setlength{\arraycolsep}{2pt}
\begin{NiceArray}{OcOcO}
\Block[hvlines]{2-1}{}p && \Block[hvlines]{2-1}{}2p && \Block[hvlines]{2-1}{} 2p-5 \\
\\
\CodeAfter
  \begin{tikzpicture} [->, draw=red, >=Latex, bend left]
    \draw (1-|1.5) to node [auto] { $\times 2$ } (1-|3.5) ; 
    \draw (1-|3.5) to node [auto] { $-5$ } (1-|5.5) ; 
    \draw (3-|5.5) to node [auto] { $+5$ } (3-|3.5) ; 
    \draw (3-|3.5) to node [auto] { $\div 2$ } (3-|1.5) ; 
  \end{tikzpicture}
\end{NiceArray}\]

\end{document}

上述代码的输出

相关内容