0

I would like to embed one array within another array.

The reason behind it is basically to learn how it would work. I have some ideas how to use this kind of encapsulation, but first let's see if this is possible :)

MWE

\documentclass[10pt,oneside,a4paper]{article}
\usepackage{ifthen}
\usepackage{pgffor}
\setlength\parindent{0pt}

\def\componentArray{{X-Pos},{Y-Pos},{Z-Pos},{Sicherung},{Phase},{Status}}
\def\combinedArray{{room},{\componentArray}}
\def\combinedArrayByHand{{room},{{X-Pos},{Y-Pos},{Z-Pos},{Sicherung},{Phase},{Status}}}

\newcounter{myCounter}
\newcommand{\iterateThroughUnusedCells}[1]{
    \setcounter{myCounter}{1}
    \foreach \elementX in #1 {
        \ifthenelse{\isodd{\value{myCounter}}}{
            \expandafter\gdef\expandafter\componentType\expandafter{\elementX}%
            $COMPONENT:\componentType$\newline
        }{
            $ELEMENT_{X}:\elementX$\newline
            \foreach \elementY in \elementX {
                $ELEMENT_{Y}:\elementY$\newline
            }
        }
        \stepcounter{myCounter}
    }
}

\begin{document}
Embedded array:\newline
\iterateThroughUnusedCells{\combinedArray}

Copied array by hand into the other array\newline
\iterateThroughUnusedCells{\combinedArrayByHand}
\end{document}

RESULT

Result

As you can see, the first function call doesn't work as expected whereas the injection by hand seems to work pretty well.

For me this seems to be a problem with expanding \componentArray, but when I try something like:

\def\temp{\elementX}%
\foreach \elementY in \temp {
   ELEMENT\_Y:\elementY\newline
}

It still doesn't work... For me this should be the same like:

\def\combinedArrayByHand{{room},{{X-Pos},{Y-Pos},{Z-Pos},{Sicherung},{Phase},{Status}}}

But it isn't obviously...

2
  • 2
    it's rather hard to understand the question as you don't really say what output you want but \edef\combinedArray{{room},{\componentArray}} would expand the list so be the same as your "by hand" example. Commented Jul 26, 2024 at 19:54
  • Such a slight difference in the definition makes such a huge difference in the result. Thank you! That's it, now it works. And btw: I thought, that when I define this \iterateThroughUnusedCells{\combinedArray} and \iterateThroughUnusedCells{\combinedArrayByHand} it's clear that I'm aiming for the second one "edited by hand". But you are right, next time I will be more clear. Commented Jul 26, 2024 at 20:04

1 Answer 1

1

it's rather hard to understand the question as you don't really say what output you want but

\edef\combinedArray{{room},{\componentArray}}

would expand the list so be the same as your "by hand" example.

3
  • You are right, for my question this works! But what's about such an scenario: When I have more than one array reference like this: \def\combinedArray{{room},{\firstComponentArray},{distributionBox}{\secondComponentArray}}? The clue behind the scenes should be, to be able to iterate through the unexpanded \combinedArray but in case the \counter is odd then I want to iterate through the firstComponentArray and secondComponentArray. This makes it generic for the user independent how long the dedicated ComponentArrays are. Commented Jul 26, 2024 at 20:46
  • I'm sorry if I expressed myself imprecisely. But it is difficult for me to fully explain all the background information in this context. In general I just want to iterate through an unexpanded array. I know, that this array has the format: ODD = component / EVEN = ComponentArray. So when the \counter is odd => Save the component Name and when the \counter is even then iterate through the given 'ComponentArray` Commented Jul 26, 2024 at 20:54
  • if I understood the real use case it is almost certainly easiest to use expl3 and remove all the pgf \foreach and certainly not use \ifthenelse but to be honest I can't work out what the high level aim is, @PascalS Commented Jul 26, 2024 at 21:41

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.