TransWikia.com

Mathematical manipulation of a pgf macro

TeX - LaTeX Asked by Rashiq on May 31, 2021

I am trying to draw a TikZ diagram that I would like to start counting from zero instead of one. I have adapted this answer for my needs.

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[auto matrix/.style={matrix of nodes,
  draw,thick,inner sep=0pt,
  nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
  cells={nodes={minimum width=1.9em,minimum height=1.9em,
   draw,very thin,anchor=center,fill=white,
   execute at begin node={%
   $vphantom{x_|}ifnumthepgfmatrixcurrentrow<4
     ifnumthepgfmatrixcurrentcolumn<4
      {#1}^{thepgfmatrixcurrentrow}_{thepgfmatrixcurrentcolumn}
     else 
      ifnumthepgfmatrixcurrentcolumn=5
       {#1}^{thepgfmatrixcurrentrow}_{N}
      fi
     fi
    else
     ifnumthepgfmatrixcurrentrow=5
      ifnumthepgfmatrixcurrentcolumn<4
       {#1}^{T}_{thepgfmatrixcurrentcolumn}
      else
       ifnumthepgfmatrixcurrentcolumn=5
        {#1}^{T}_{N}
       fi 
      fi
     fi
    fi  
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=14
     cdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=41
     vdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=44
     ddots
    fi$
    }
  }}}]
 matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=x](matx){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
  node[midway,below] {$D$};
 draw[thick,-stealth] ([yshift=-1ex]matx.south west) -- 
  ([yshift=-1ex]matx.south east) node[midway,below] {joints};
 draw[thick,-stealth] ([xshift=-1ex]matx.north west)
   -- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
end{tikzpicture}
end{document}

This code has the following output.

enter image description here

I would like the index to begin from zero. Something like this.

enter image description here

I tried using the calc package to subtract from the thepgfmatrixcurrentrow and the thepgfmatrixcurrentcolumn like $thepgfmatrixcurrentrow - 1 = result$ but to no avail. Any help is greatly appreciated.

2 Answers

You can use pgfmathtruncatemacro to compute your desired numbers just before all the code into the matrix definition, and replace all thepgfmatrixcurrentrow and thepgfmatrixcurrentcolumn by your computed variables.

multiple matrix planes with pgfmath

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[auto matrix/.style={matrix of nodes,
  draw,thick,inner sep=0pt,
  nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
  cells={nodes={minimum width=1.9em,minimum height=1.9em,
   draw,very thin,anchor=center,fill=white,
   execute at begin node={%
   pgfmathtruncatemacro{nrow}{thepgfmatrixcurrentrow-1}%
   pgfmathtruncatemacro{ncol}{thepgfmatrixcurrentcolumn-1}%
   $vphantom{x_|}ifnumthepgfmatrixcurrentrow<4
     ifnumthepgfmatrixcurrentcolumn<4
      {#1}^{nrow}_{ncol}     
     else 
      ifnumthepgfmatrixcurrentcolumn=5
       {#1}^{nrow}_{N}
      fi
     fi
    else
     ifnumthepgfmatrixcurrentrow=5
      ifnumthepgfmatrixcurrentcolumn<4
       {#1}^{T}_{ncol}
      else
       ifnumthepgfmatrixcurrentcolumn=5
        {#1}^{T}_{N}
       fi 
      fi
     fi
    fi  
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=14
     cdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=41
     vdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=44
     ddots
    fi$
    }
  }}}]
 matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=x](matx){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
  node[midway,below] {$D$};
 draw[thick,-stealth] ([yshift=-1ex]matx.south west) -- 
  ([yshift=-1ex]matx.south east) node[midway,below] {joints};
 draw[thick,-stealth] ([xshift=-1ex]matx.north west)
   -- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
end{tikzpicture}
end{document}

Correct answer by SebGlav on May 31, 2021

Since these are integers, use thenumexprpgfmatrixcurrentrow-1

documentclass[tikz,border=3mm]{standalone}
usetikzlibrary{matrix}
begin{document}
begin{tikzpicture}[auto matrix/.style={matrix of nodes,
  draw,thick,inner sep=0pt,
  nodes in empty cells,column sep=-0.2pt,row sep=-0.2pt,
  cells={nodes={minimum width=1.9em,minimum height=1.9em,
   draw,very thin,anchor=center,fill=white,
   execute at begin node={%
   $vphantom{x_|}ifnumthepgfmatrixcurrentrow<4
     ifnumthepgfmatrixcurrentcolumn<4
      {#1}^{thenumexprpgfmatrixcurrentrow-1}_{thenumexprpgfmatrixcurrentcolumn-1}
     else 
      ifnumthepgfmatrixcurrentcolumn=5
       ,{#1}^{thenumexprpgfmatrixcurrentrow-1}_{N}
      fi
     fi
    else
     ifnumthepgfmatrixcurrentrow=5
      ifnumthepgfmatrixcurrentcolumn<4
       {#1}^{T}_{thenumexprpgfmatrixcurrentcolumn-1}
      else
       ifnumthepgfmatrixcurrentcolumn=5
        ,{#1}^{T}_{N}
       fi 
      fi
     fi
    fi  
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=14
     cdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=41
     vdots
    fi
    ifnumthepgfmatrixcurrentrowthepgfmatrixcurrentcolumn=44
     ddots
    fi$
    }
  }}}]
 matrix[auto matrix=z,xshift=3em,yshift=3em](matz){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=y,xshift=1.5em,yshift=1.5em](maty){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 matrix[auto matrix=x](matx){
  & & & & 
  & & & & 
  & & & & 
  & & & & 
  & & & & 
 };
 draw[thick,-stealth] ([xshift=1ex]matx.south east) -- ([xshift=1ex]matz.south east)
  node[midway,below] {$D$};
 draw[thick,-stealth] ([yshift=-1ex]matx.south west) -- 
  ([yshift=-1ex]matx.south east) node[midway,below] {joints};
 draw[thick,-stealth] ([xshift=-1ex]matx.north west)
   -- ([xshift=-1ex]matx.south west) node[midway,above,rotate=90] {time};
end{tikzpicture}
end{document}

I also added , in the last column to avoid too much overlapping.

enter image description here

Answered by egreg on May 31, 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