TransWikia.com

Can tikz-matrices be "forgotten"?

TeX - LaTeX Asked by Brian Fitzpatrick on December 10, 2020

I’m having an issue with matrix names in tikz being "persistent".

Consider the two matrices:

enter image description here

These matrices were generated with the following code:

documentclass[border=12pt]{standalone}

usepackage{tikz}
usetikzlibrary{matrix}

begin{document}
$
begin{tikzpicture}
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (m)
  {
    1  & -32  & 0  & 15 
    16 & -138 & -3 & 5  
    4  & 14   & 11 & 19 
  };
end{tikzpicture}
$
$
begin{tikzpicture}
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (a)
  {
    1  
    16 
    4  
  };

  draw[red, thick] (m-1-4) -- (m-3-1);
end{tikzpicture}
$

end{document}

The two matrices are constructed in different tikzpicture environments. The one on the left is named m and the one on the right is named a.

The second tikzpicture has a draw command that references nodes in a matrix from a previous tikzpicture. I actually want this code to throw an error, since this tikzpicture has no matrix named m.

Is it possible to ask tikz to "forget" the matrix m?

2 Answers

In TikZ all coordinates are global. So they do not get forgotten even if you start a new tikzpicture or even if you go on the next page. So the answer is no. You can create mechanisms that produce the error message by installing some coordinate prefixes, say. Here is a code that installs a unique prefix in every tikzpicture. Please use with care if you decide to use it.

documentclass[border=12pt]{standalone}

usepackage{tikz}
usetikzlibrary{matrix}
newcounter{TikZpicture}
tikzset{every picture/.append style={step tikz counter,
    name prefix=pic-numbervalue{TikZpicture}},
step tikz counter/.code={stepcounter{TikZpicture}}}

begin{document}
$
begin{tikzpicture}
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (m)
  {
    1  & -32  & 0  & 15 
    16 & -138 & -3 & 5  
    4  & 14   & 11 & 19 
  };
end{tikzpicture}
$
$
begin{tikzpicture}
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (a)
  {
    1  
    16 
    4  
  };

  draw[red, thick] (m-1-4) -- (m-3-1);
end{tikzpicture}
$

end{document}

Answered by user228539 on December 10, 2020

It turns out that TikZ nodes are stored as 5 global macros. I can't actually delete nodes, but I can alias them to all be the same (nonexistent) node. Presumably the now unused global macros will be released as with globalletfoo=relax.

documentclass[border=12pt]{standalone}
usepackage{tikz}
usetikzlibrary{matrix}

makeatletter
newcommand{removenode}[1]% #1 = node name
{bgroup
  letpgf@nodecallback=pgfutil@gobble%
  pgfnodealias{#1}{}% node ()
egroup}
newcommand{removematrix}[3]% #1 = matrix name, #2 = number rows, #3 = number columns
{bgroup% use local i and j
  letpgf@nodecallback=pgfutil@gobble%
  foreach i in {1,...,#2}{%
    foreach j in {1,...,#3}{pgfnodealias{#1-i-j}{}}}%
  pgfnodealias{#1}{}%
egroup}
makeatother

begin{document}
 begin{tikzpicture}[baseline=(current bounding box.center)]
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (m)
  {
    1  & -32  & 0  & 15 
    16 & -138 & -3 & 5  
    4  & 14   & 11 & 19 
  };
end{tikzpicture}

removematrix{m}{3}{4}%

begin{tikzpicture}[baseline=(current bounding box.center)]
  matrix[
  , matrix of math nodes
  , left delimiter = {[}
  , right delimiter = {]}
  ] (a)
  {
    1  
    16 
    4  
  };
  draw[red, thick] (m-1-4) -- (m-3-1);
end{tikzpicture}
end{document}

BTW, a faster but less readable version of removematrix follows.

newcommand{removematrix}[3]% #1 = matrix name, #2 = number rows, #3 = number columns
{bgroup% use local registers
  letpgf@nodecallback=pgfutil@gobble%
  count1=#2relax
  loopifnumcount1>0{% one loop per group
    count2=#3relax
    loopifnumcount2>0
      edefnodename{#1-numbercount1-numbercount2}%
      pgfnodealias{nodename}{}%
      advancecount2 by -1
    repeat}%
    advancecount1 by -1
  repeat
  pgfnodealias{#1}{}%
egroup}

Answered by John Kormylo on December 10, 2020

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