TransWikia.com

Iterate on a list coming from a variable

TeX - LaTeX Asked on October 4, 2021

I’d like to iterate over a list of values, which is stored inside a variable.

The problem is that the list of items (myList in the MWE) to iterate over is the value of a previously-declared variable (mydata).

This nested declaration breaks the iteration, as shown in the MWE. Indeed, iterating over myData does not give any problems while iterating over myList does.

MWE output

documentclass{article}
usepackage{pgffor}
setlengthparindent{0pt}
defmyData{1,2}
defmyList{myData}

begin{document}
section*{Variables}

Mydata : myData

Mylist : myList

section*{Iteration over myList} 
Somewhere this is broken due to the variable declaration...

foreach x in myList
{
    Item x 
}

section*{Iteration over myData}

foreach x in myData
{
    Item x 
}
end{document}

How can I make the variable re-definition not break the iteration ?
I cannot do otherwise since this MWE is a excerpt of a much broader project.

One Answer

It is an expansion issue. If you use edefmyList{myData} or edefmyList{myData} then your code will work:

documentclass{article}
usepackage{pgffor}
setlengthparindent{0pt}
defmyData{1,2}
edefmyList{myData}

begin{document}
section*{Variables}

Mydata : myData

Mylist : myList

section*{Iteration over myList}
Somewhere this is broken due to the variable declaration...

foreach x in myList
{
    Item x 
}

section*{Iteration over myData}

foreach x in myData
{
    Item x 
}
end{document}

Correct answer by user30471 on October 4, 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