3

I would like to achieve two goals with the code below.

  1. Change the width of the red rectangle.
  2. Draw the arrows added by hand in the following screenshot.

enter image description here

Any clue or advice will be appreciated.

\documentclass{article}

\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}

$\begin{NiceArray}{rcl}[name=test]
    2 x + 3 & = & 5 x - 4
    \\[-.5em]
    \\
    \Block[draw=red]{1-3}{- 3}
    \\
    \\[-.5em]
        2 x & = & 5 x - 7
    \\[-.5em]
    \\
    \Block[draw=red]{1-3}{- 5x}
    \\
    \\[-.5em]
        -3 x & = & - 7
\end{NiceArray}$

\end{document}
2

Here is an automatic method.

\documentclass{article}

\usepackage{nicematrix,tikz}
\usetikzlibrary{calc}

\ExplSyntaxOn
\cs_new_protected:Nn \__projetmbc_ii:n
  {
    \begin{tikzpicture}
    \draw [->] ($(#1-|2)+(-1mm,4pt)$) -- ($(\int_eval:n { #1 + 1 } -| 2)+(-1mm,-4pt)$) ;
    \draw [->] ($(#1-|3)+(1mm,4pt)$) -- ($(\int_eval:n { #1 + 1 } -| 3)+(1mm,-4pt)$) ;
    \end{tikzpicture}
  }

\cs_new_protected:Nn \__projetmbc_i:n
  {
    \tl_gput_right:Nx \g_nicematrix_code_before_tl
      { \__projetmbc_ii:n { \arabic { iRow } } }
    \Block[draw=red,fill=white]{1-3}{#1}
  }

\NewExpandableDocumentCommand \NewLineWithBox { m }
  { \\[1mm] \__projetmbc_i:n {#1} \\ \noalign{\vspace{2mm}} }

\ExplSyntaxOff

\begin{document}

\NiceMatrixOptions{cell-space-limits=2pt}

$\begin{NiceArray}{r@{}>{{}}c<{{}}@{}l}
2x+3 & = & 5x-4 \NewLineWithBox{-3}
 2x  & = & 5x-7 \NewLineWithBox{-5x}
-3 x & = & - 7  \NewLineWithBox{\text{\Huge Huge}}
-3 x & = & - 7
\end{NiceArray}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

5
  • It is semi-automatic, unless you can make users just input an equation and the steps are provided out of the box. – Money Sets You Free Mar 5 at 10:58
  • 1
    The typesetting is automatic, that means that you don't have to adjust by hand some length. That's what I mean, of course. – F. Pantigny Mar 5 at 11:02
  • @F.Pantigny Great. Viva la France! Thanks for the "easy-to-read" LaTeX3 code. Can I use it directly in a GPL project? – projetmbc Mar 5 at 13:08
  • 1
    Of course, you can use that code (I think that this is in the conditions of utilisation of TeX StackExchange). – F. Pantigny Mar 5 at 15:02
  • @F.Pantigny Great! Thanks a lot for this. – projetmbc Mar 6 at 17:48
5

Here is a solution.

 \documentclass{article}

 \usepackage{nicematrix,tikz}
 \usetikzlibrary{calc}

 \begin{document}

 $\begin{NiceArray}{r@{}c@{}l}
 2x+3 & {} = {} & 5x-4 \\[7mm]
  2x  & {} = {} & 5x-7 \\
 \CodeAfter
 \begin{tikzpicture}
 \draw [->,shorten < = 1pt, shorten > = 1pt]
       let \p1 = ([xshift=-1mm]1-1.south east) ,
           \p2 = (2-1.north) 
       in (\p1) -- (\x1,\y2) ;
 \draw [->,shorten < = 1pt, shorten > = 1pt]
       let \p1 = ([xshift=1mm]1-3.south west) ,
           \p2 = (2-3.north) 
       in (\p1) -- (\x1,\y2) ;
 \node [draw, fill=white] at ([yshift=-5mm]1-2){\;$-3$\;} ;
 \end{tikzpicture}
 \end{NiceArray}$

 \end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes).

Output of the above code

2
  • Thanks "beaucoup". Is it possible to keep the use of \Block? I can transform your solution into an easy-to-use interface but this would be simpler to code with the version in my post. – projetmbc Mar 4 at 21:18
  • See my second answer. – F. Pantigny Mar 5 at 10:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.