TransWikia.com

Latex: How to write single cross arrow in a 2x2 matrix?

TeX - LaTeX Asked by Weilory on March 18, 2021

sample

Is there a way to make a version of the diagram provided above?

Thank you very much.

2 Answers

I'm adapting my answer to "How to add arrow in equations and matrix?"; see there for some explanations on how to draw arrows between text elements.

  • First, typeset the text without arrows.
  • Wrap all elements, that will be the origin or target of an arrow, into a tikznode command. This assigns a name to the text element and stores its size and position.
  • Add a tikzpicture environment with the options remember picture,overlay below. It contains the graphical elements, in this case the arrows. Here you will use the names assigned in the previous step.
  • Run LaTeX at least two times to propagate the information about the nodes and arrows everywhere.

enter image description here

documentclass{article}
usepackage{tikz}
usetikzlibrary{arrows}% only needed for the arrow tip stealth'
newcommandtikznode[3][]{%
  tikz[remember picture,baseline=(#2.base)]
    node[minimum size=0pt,inner sep=0pt,#1](#2){#3};%
}
begin{document}
begin{tabular}{r@{qquad}r}
    tikznode{-x}{$-x$} & tikznode{1}{$1$} [2ex]
    tikznode{2x}{$2x$} & tikznode{3}{$3$} 
    cline{2-2}
                        & $-x$
end{tabular}
begin{tikzpicture}[remember picture,overlay,> = stealth',shorten <= 4pt,shorten > = 4pt]
  draw[->] (-x.east) -- (3.west);
  draw[->] (2x.east) -- (1.west);
end{tikzpicture}
end{document}

Correct answer by gernot on March 18, 2021

Here is a solution with {NiceArray} of nicematrix.

That environment is similar to the classical environment {array} (of array) but creates PGF nodes under the cells of the array. Then, it's possible to use Tikz to draw the required arrows.

documentclass{article}
usepackage{nicematrix}
usepackage{tikz}

begin{document}

$begin{NiceArray}{r@{qquad}r}
    -x & 1 [2ex]
    2x & 3 
    cline{2-2}
         & -x
CodeAfter
begin{tikzpicture}[shorten <= 4pt,shorten > = 4pt]
  draw[->] (1-1.east) -- (2-2.west);
  draw[->] (2-1.east) -- (1-2.west);
end{tikzpicture}
end{NiceArray}$

end{document}

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

Output of the above code

Answered by F. Pantigny on March 18, 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