TransWikia.com

Dimensions of nodes

TeX - LaTeX Asked on March 30, 2021

In the following picture, I would like to use the same width for the rounded rectangles and the same height for the arrows (take a look at the last ones).

To do that I need to store the dimensions of the nodes used. Is there an easy way to do that?

enter image description here

documentclass{article}

usepackage{nicematrix,tikz}
usetikzlibrary{shapes.misc, calc}

% Source: https://tex.stackexchange.com/a/585865/6880
newcommandtest[2]{%
    #1 & {} = {} & #2 [8mm]
}

newcommandstep[3]{
    draw [->,shorten < = 1pt, shorten > = 1pt]
           let p1 = ([xshift=-1mm]#2-1.south east) ,
               p2 = ([yshift=1mm]#3-1.north) 
           in (p1) -- (x1,y2) ;
    draw [->,shorten < = 1pt, shorten > = 1pt]
           let p1 = ([xshift=1mm]#2-3.south west) ,
               p2 = ([yshift=1mm]#3-3.north) 
           in (p1) -- (x1,y2) ;
    node [draw, fill=white, rounded rectangle] at ([yshift=-5mm]#2-2){tinykern1em$#1$kern1em} ;
}

begin{document}

$begin{NiceArray}{r@{}c@{}l}
    test{2x+3}{5x-4}
    test{2x  }{5x-7}
    test{-3x }{-7}
    test{x   }{dfrac73}
    CodeAfter
    begin{tikzpicture}
        step{-3}{1}{2}
        step{-5x}{2}{3}
        step{div (-3)}{3}{4}
    end{tikzpicture}
 end{NiceArray}$

 end{document}

One Answer

Update

I thought about a different solution that solves the problem of the arrows' length and at the same time, allows a normal code for the equations.

enter image description here

The code

documentclass[12pt, a4paper]{article}
usepackage{amsmath}
usepackage{tikz}

newcommand{action}[1]{%
  &= tikz[baseline=-.5ex]{%
    path[use as bounding box] (0, -3ex) rectangle ++(1pt, 6ex);
    draw[->] (-3.5ex, 3ex) -- ++(0, -6ex);
    draw[->] (.5ex, 3ex) -- ++(0, -6ex);
    path (-1.5ex, 0)
    node[draw, rectangle, rounded corners, fill=WG, thin,
    inner sep=1ex, minimum width=11ex, scale=.7] {$#1$};
  }
}
definecolor{WG}{RGB}{217, 221, 221}

begin{document}
begin{align*}
  2x +3 &= 5x -4 
  action{3} 
  2x &= 5x -7 
  action{-5x} 
  -3x &= -7 
  action{div(-3)} 
  x &= frac{7}{3}
end{align*}
end{document}

First solution I'm proposing a solution based on nodes (in TikZ). It is true that the alignment of = is lost. It might be retrieved by breaking the equations into pieces further, but I don't know if you are interested. Eventually, you can transform it into a LaTeX command.

enter image description here

  • The height of each wide node is fixed such that the fraction fits into it.
  • The arrows have the same length since they connect nodes at equal distances.
  • There is a parameter d which controls the horizontal space between the arrows.

The code

documentclass[11pt, margin=1cm]{standalone}
usepackage{tikz}
usetikzlibrary{calc, math} 
usetikzlibrary{fit, positioning, arrows.meta}
usetikzlibrary{backgrounds}

begin{document}
definecolor{WG}{RGB}{217, 221, 221}
tikzset{
  w/.style={minimum width=22ex, minimum height=6ex},
  n/.style={draw, rectangle, rounded corners,
    fill=white, thick,
    inner sep=1ex, minimum width=10ex, scale=.7},
  m/.style={->, shorten <=-3pt, shorten >=-2.75pt}
}
tikzmath{
  real d, il, ol, ie, oe;
  d = 35;
  ol = 270 -d;  il = 90 +d;
  oe = 270 +d;  ie = 90 -d;
}
begin{tikzpicture}[node distance=1ex and 5ex,
  every node/.style={text=black}]
  node[w] (Iw) {$2x +3 = 5x -4$};
  node[below=of Iw, n] (In) {$-3$};
  node[below=of In, w] (IIw) {$2x = 5x -7$};
  node[below=of IIw, n] (IIn) {$-5x$};
  node[below=of IIn, w] (IIIw) {$-3x = -7$};
  node[below=of IIIw, n] (IIIn) {$div(-3)$};
  node[below=of IIIn, w] (IVw) {$x = frac{7}{3}$};

  begin{pgfonlayer}{background}
    node[gray, fill=WG, fit=(Iw) (IVw), inner sep=0pt] {};
    draw
    (Iw.ol) edge[m] (IIw.il)
    (Iw.oe) edge[m] (IIw.ie)
    (IIw.ol) edge[m] (IIIw.il)
    (IIw.oe) edge[m] (IIIw.ie)
    (IIIw.ol) edge[m] (IVw.il)
    (IIIw.oe) edge[m] (IVw.ie);
  end{pgfonlayer}
end{tikzpicture}

end{document}

Correct answer by Daniel N on March 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP