2

I would like to use the \x and \y variables of the two nested \foreach loops in my tikz script to reference the elements of a list. The code looks like this :

\documentclass{minimal}
\usepackage{tikz}
\usepackage{calc}
\RequirePackage{etoolbox} % defines lists and their operations

\pgfsetxvec{\pgfpoint{.866cm}{.5cm}}
\pgfsetyvec{\pgfpoint{.866cm}{-.5cm}}
\pgfsetzvec{\pgfpoint{0cm}{-1cm}}

\begin{document}

\begin{tikzpicture}

\newcount\tmpnum
\def\storedata#1#2{\tmpnum=0 \edef\tmp{\string#1}\storedataA#2\end}
\def\storedataA#1{\advance\tmpnum by1
   \ifx\end#1\else
      \expandafter\def\csname data:\tmp:\the\tmpnum\endcsname{#1}%
      \expandafter\storedataA\fi
}
\def\getdata[#1]#2{\csname data:\string#2:#1\endcsname}

\storedata\List{{$0$}{$1$}{$\ast$}{$\times$}
                          {$0$}{$1$}{$\ast$}{$\times$}
                          {$0$}{$1$}{$\ast$}{$\times$}
                          {$0$}{$1$}{$\ast$}{$\times$}}


  \foreach \x in {1,...,4}
  \foreach \y in {1,...,4}
    \draw (\x,\y,0) -- (\x+1,\y,0) --(\x+1,\y+1,0)--(\x,\y+1,0) -- (\x,\y,0)  node [midway,sloped,above,xslant=0.5]{\getdata[\x+\y]\List};
        

\end{tikzpicture}

\end{document}

I have some troubles understanding why it actually does not perform the addition of the two variables. Furthermore I tried printing just the \x+y value and it looked like it was doing a string concatenation e.g. :

\x+\y = "x + y" as

I am sorry for this dumb question, I hope some tikz-ninjas and TeX-samurais can help.

4
  • Welcome to TeX.SX! Could you show what you would like the output to look like? It is not fully clear to me what you try to achieve, I have to admit ... Did you test your array mechanism separately from the TikZ environment? Why do you load etoolbox when you don't use it? Do you need help with creating the list and storing and getting values to/from it? Or what exactly is your question here? Commented Jun 16, 2023 at 10:42
  • 1
    If you want to print the result of a calculation, you need to use \numexpr or \pgfmathparse or the like. TeX only performs calculations when the defintion of a command tells it to do so. So, 1 + 1 will print "1 + 1", but \the\numexpr1 + 1\relax, for example, or \pgfmathparse{int(1 + 1)}\pgfmathresult would print "2". Commented Jun 16, 2023 at 10:51
  • @JasperHabicht PGF/TikZ does calculation in almost all places w/o the user having to explicitly say so. It seems many people learn TikZ before they learn Latex leading many to assume you can just calculate something everywhere as if Tex were a programming language. Commented Jun 16, 2023 at 11:26
  • @Qrrbrbirlbel Yes, right. In a TikZ context, calculations are done almost everywhere automatically. But here, the OP seems to try to retrieve the result of a calculation inside the node text, which is one of the few things where TikZ does not execute calculations automatically ... (but I have to admit that I don't fully understand what the OP tries to do, which why I asked for some representation of the desired output). Commented Jun 16, 2023 at 11:49

1 Answer 1

2

I am very unsure what you want to achieve, but you probably only need TikZ for it:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
    x=({.866cm,.5cm}),
    y=({.866cm,-.5cm})
]

\newcommand{\mylist}{
    1/1/*/$\times$,
    2/2/+/$+$,
    3/3/-/$-$,
    4/4/{/}/$\div$%
}

\foreach \va/\vb/\oc/\op [count=\x] in \mylist {
  \foreach \y in {0,...,3} {
    \draw (-\x,\y) -- ({-\x+1},\y) -- 
      ({-\x+1},{\y+1}) -- (-\x,{\y+1}) -- cycle;
    \path ({-\x+0.5},\y) -- ({-\x+0.5},{\y+1})
      node[midway, sloped, xslant=0.5] {%
        \ifcase\y\relax
          \va\or
          \op\or
          \vb\or
          \pgfmathparse{int(\va \oc \vb)}\pgfmathresult
        \fi%
      };
  }
}

\end{tikzpicture}
\end{document}

enter image description here

3
  • Hi, I want to draw two overlapping KV-Diagrams as really overlapping, and not just saying that "they actually overlap" like in 99% of books. I will try the solution shortly but looks incredibly good to me ;)
    – lazerbeam
    Commented Jun 18, 2023 at 22:05
  • Hi, yes it worked, thank you very much
    – lazerbeam
    Commented Jun 19, 2023 at 21:51
  • 1
    @lazerbeam I thought that my solution was probably not exactly what you wanted, at least concerning the labels inside the "squares". But then, it is just to show the basic use and you are probably able to change the code in order to make your specific use case work. Commented Jun 20, 2023 at 7:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .