TransWikia.com

Printing an element of a non-numeric array as the node label

TeX - LaTeX Asked on November 1, 2021

I want to retrieve a member of a non-numeric array passed to a TikZ pic as an argument, to use it as a node label. foreach easily does it while iterating over the array, but I cannot do it for the individual elements. The following are my two attempts that fail miserably:

documentclass[tikz]{standalone}

begin{document}

tikzset{
   pics/mypic/.style n args={1}{
      code={
         foreach a [count=i] in {#1}{
            node at (i, 1) {$a$};
         }

         % Attempt 1: Doesn't work
         % node at (1, 2) {$#1[0]$};

         % Attempt 2: Doesn't compile
         % Error: Undefined control sequence. ... Missing $ inserted. ...
         node at (2, 2) {$pgfmathparse{{#1}[0]}pgfmathresult$};
      }
   }
}

tikz{pic {mypic={mathtt{a},mathtt{b},mathtt{c}};}}
% tikz{pic {mypic={a,b,c};}}   % It's the same for this one.
% tikz{pic {mypic={0,1,2};}}   % Attempt 2 works with this one,
% tikz{pic {mypic={0,a,2};}}   % but not with this one ("Unknown function `a').

end{document}

Output:

bad array access

I want to be able to somehow draw a node with the label #1[0]. Regarding Attempt 2, I noticed that it yields the same errors also with the text arrays. It works fine for the all-numeric arrays, but hybrid arrays are also troubling, even if the accessed element is numeric.

2 Answers

Here, I use listofitems to read the desired data into an array mydata. The OP suggested a useful EDIT to my original approach by including all of the listofitems processing inside the tikzset code.

Note that the 2 in mydata[2], calling out a single array element, can be any expandable calculation that results in an integer. Also, negative values denote "reverse index", that is, relative to the end of the array, rather than the beginning.

documentclass[tikz]{standalone}
usepackage{listofitems}

begin{document}

tikzset{
   pics/mypic/.style n args={1}{
      code={
         foreach a [count=i] in {#1}{
            node at (i, 1) {$a$};
         }

         readlistmydata{#1}
         node at (2, 2) {$mydata[2]$};
      }
   }
}

tikz{pic {mypic={mathtt{a},mathtt{b},mathtt{c}};}}
end{document}

enter image description here

Answered by Steven B. Segletes on November 1, 2021

The following command uses foreach to retrieve the element at a given index, and assign it globally to the given macro.

% Does #1 = #2[#3] with 0-based indexing
newcommandarrayget[3]{
    foreach a [count=i from 0] in {#2}{
        ifnum #3=i
            xdef#1{unexpandedexpandafter{a}}
        fi
    }
}

References for the curious:

  • Global definition with xdef: source 1
  • unexpandedexpandafter{...} to expand a once: source 2

It should be able to handle anything that foreach can. Demonstration:

documentclass[tikz]{standalone}

newcommandarrayget[3]{   % Does #1 = #2[#3] with 0-based indexing
    foreach a [count=i from 0] in {#2}{
        ifnum #3=i
            xdef#1{unexpandedexpandafter{a}}
        fi
    }
}

begin{document}

tikzset{
   pics/mypic/.style n args={1}{
      code={
         foreach a [count=i] in {#1}{ node at (i, 1) {$a$}; }

         arrayget{asd}{#1}{0} node at (1.5, 2) {$asd$};
         arrayget{asd}{#1}{1} node at (2.0, 2) {$asd$};
         arrayget{qwe}{#1}{2} node at (2.5, 2) {$qwe$};
      }
   }
}

tikz{pic {mypic={mathtt{a},mathtt{b},mathtt{c}};}}
tikz{pic {mypic={a,b,c};}}
tikz{pic {mypic={0,1,2};}}
tikz{pic {mypic={0,a,2};}}

end{document}

Output:

Properly indexed

Answered by Utkan Gezer on November 1, 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