7

I would like to show the classical trick to calculate the determinant of a square matrix of dimension 3. Here is my very beginning code.

\documentclass{article}

\usepackage{tikz}
\usepackage{nicematrix}

\begin{document}

$\begin{NiceArray}{|CCC|CC}
    a & b & c & a & b \\
    r & s & t & r & s \\
    x & y & z & x & y
    %
    \CodeAfter
    %
    \tikz\draw[blue] (1-1.south east) -- (3-3.south east) ;
\end{NiceArray}$

\end{document}

Here is the unwanted output.

enter image description here

I would like the path to be hidden by the text as with the a below, and I need the line to be longer as in the picture below.

enter image description here

10

The simplest possibility would be to add a node that is filled white on the corresponding entry. Works, but only if the page background is white (which it is not already in your screen shots). So we need to avoid drawing the line in certain stretches. There are several possibilities, out of which I focus on clips and intersections.

You can use an even odd clip. So we first find the boundary path of the entry via fit, and then perform an even odd clip with that path and some large rectangle. The size of the blocked out region depends on the inner sep of the fit node. Everything outside the fit circle and inside the big box will be visible.

\documentclass{article}

\usepackage{tikz}
\usepackage{nicematrix}
\usetikzlibrary{fit}
\tikzset{% https://tex.stackexchange.com/a/76216
even odd clip/.code={\pgfseteorule}}
\makeatletter
\tikzset{% https://tex.stackexchange.com/a/38995  
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}} 
} 
\makeatother
\begin{document}

$\begin{NiceArray}{|CCC|CC}
    a & b & c & a & b \\
    r & s & t & r & s \\
    x & y & z & x & y
    %
    \CodeAfter
    \begin{tikzpicture}
     \node[inner sep=0.5pt,circle,fit=(1-1),save path=\pathA]{};
     \clip[even odd clip,reuse path=\pathA] ([xshift=-1cm,yshift=1cm]1-1)
      rectangle ([xshift=1cm,yshift=-1cm]3-3);
     \draw[semithick,blue,-latex,shorten <=-0.8em,shorten >=-0.8em] 
        (1-1.center) -- (3-3.center); 
    \end{tikzpicture}
\end{NiceArray}$

\end{document}

enter image description here

One can thus spare out all the entries, if needed.

\documentclass{article}

\usepackage{tikz}
\usepackage{nicematrix}
\usetikzlibrary{arrows.meta,fit}
\tikzset{% https://tex.stackexchange.com/a/76216
even odd clip/.code={\pgfseteorule}}
\makeatletter
\tikzset{% https://tex.stackexchange.com/a/38995  
reuse path/.code={\pgfsyssoftpath@setcurrentpath{#1}} 
} 
\makeatother
\begin{document}

$\begin{NiceArray}{|CCC|CC}
    a & b & c & a & b \\
    r & s & t & r & s \\
    x & y & z & x & y
    %
    \CodeAfter
    \begin{tikzpicture}
     \node[inner sep=0.5pt,circle,fit=(1-1),save path=\pathA]{};
     \node[inner sep=0.5pt,circle,fit=(2-2),save path=\pathB]{};
     \node[inner sep=0.5pt,circle,fit=(3-3),save path=\pathC]{};
     \clip[even odd clip,reuse path=\pathA] ([xshift=-1cm,yshift=1cm]1-1)
      rectangle ([xshift=1cm,yshift=-1cm]3-3);
     \clip[even odd clip,reuse path=\pathB] ([xshift=-1cm,yshift=1cm]1-1)
      rectangle ([xshift=1cm,yshift=-1cm]3-3);
     \clip[even odd clip,reuse path=\pathC] ([xshift=-1cm,yshift=1cm]1-1)
      rectangle ([xshift=1cm,yshift=-1cm]3-3);
     \draw[semithick,blue,-{Latex[round,length=3.5pt]},shorten <=-0.8em,shorten >=-0.8em] 
        (1-1.center) -- (3-3.center); 
    \end{tikzpicture}
\end{NiceArray}$

\end{document}

enter image description here

Alternatively, intersections can be used.1

\documentclass{article}

\usepackage{tikz}
\usepackage{nicematrix}
\usetikzlibrary{fit,intersections}
\begin{document}

$\begin{NiceArray}{|CCC|CC}
    a & b & c & a & b \\
    r & s & t & r & s \\
    x & y & z & x & y
    %
    \CodeAfter
    \begin{tikzpicture}
     \node[inner sep=0.5pt,circle,fit=(1-1),draw,opacity=0,name path=circ1]{};
     \path (1-1.center) -- (3-3.center) 
        coordinate[pos=-0.2] (start) coordinate[pos=1.2] (end);
     \path[name path=line]  (start) to[bend left=0] (end);
     \draw[semithick,blue,-latex,name intersections={of=line and circ1,total=\t}]
       (start) -- \ifnum\t=2 (intersection-1) (intersection-2) --\fi
        (end);
    \end{tikzpicture}
\end{NiceArray}$

\end{document}

enter image description here

If you want to spare out several entries, you can use use loops.

\documentclass{article}

\usepackage{tikz}
\usepackage{nicematrix}
\usetikzlibrary{arrows.meta,fit,intersections}
\begin{document}

$\begin{NiceArray}{|CCC|CC}
    a & b & c & a & b \\
    r & s & t & r & s \\
    x & y & z & x & y
    %
    \CodeAfter
    \begin{tikzpicture}
     \foreach \X in {1,2,3}{
     \node[inner sep=0.5pt,circle,fit=(\X-\X),draw,opacity=0,
        name path global=circ\X]{};
     }
     \path (1-1.center) -- (3-3.center) 
        coordinate[pos=-0.2] (start) coordinate[pos=1.2] (end);
     \path[name path=line]  (start) to[bend left=0] (end);
     \draw[semithick,blue,-{Latex[round,length=3.5pt]},
        name intersections={of=line and circ1,name=i1,total=\tA},
        name intersections={of=line and circ2,name=i2,total=\tB},
        name intersections={of=line and circ3,name=i3,total=\tC},
        ]
        (start) -- \ifnum\tA=2
        (i1-1) (i1-2) --\fi
        \ifnum\tB=2
        (i2-1) (i2-2) --\fi
        \ifnum\tC=2
        (i3-1) (i3-2) --\fi
         (end);
    \end{tikzpicture}
\end{NiceArray}$

\end{document}

enter image description here

1If you think that I am crazy because I draw a straight line with bend=0: the reason is that otherwise it is harder to sort the intersections: https://tex.stackexchange.com/a/418650.

| improve this answer | |
  • 1
    Thanks "another time". ;-) The problem with you is that the Schrödinger's cat is always alive. :-) – projetmbc Jun 15 at 9:42
  • 1
    @projetmbc Are you certain? ;-) – user194703 Jun 15 at 9:43
  • :-) One last question. Whats is the way to do multiple intersections ? I like the code of your third solution and will like to use \node[inner sep=0.5pt,circle,fit=(2-2),draw,opacity=0,name path=circ2]{}; and \node[inner sep=0.5pt,circle,fit=(3-3),draw,opacity=0,name path=circ3]{};. – projetmbc Jun 15 at 10:05
  • 2
    @projetmbc I added a loop and also fixed a bug. In the first run it did not find intersections since the paths were not yet in their final positions. Now I added \ifnum\t=2... \fi type statements to fix this problem. – user194703 Jun 15 at 10:17
  • 2
    @projetmbc Some of the things are only documented in the source codes. In are occasions the manual is even wrong, but there are continuing efforts of improving it. Some of the users on this site (not me) spend a lot of time and energy in doing this. (The safest way to avoid errors in the manual is to have no manual but I am not sure if I like that strategy. So I fully agree that TikZ is great, there are attempts to make it accessible to users, and there is a manual. ;-)) – user194703 Jun 15 at 10:29

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.