I need a list of counters with the special requirement that I a-priori don't know how many counters are needed. My first idea is to use a sequence of integers that has a variable length. As suggested by egreg's answer to
Get n-th element of a list (with etoolbox, or not)
I want to use the xparse package. If there is an easier solution, feel free to tell me.
Egreg's code allows to predefine the list, add elements and read the n-th element. However, I also want to increase a specific element of the list (by 1) every time another command is run. But, honestly I am having a hard time with the syntax. I tried to write a function \setnthelement that has the optional argument (the identifier of the sequence) and two mandatory arguments: the first shall be the list index to be changed and the second the new value of the list at the specified index. I use the following code
\documentclass[10pt]{scrartcl}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\newchangelist}{ m }
{
\seq_new:c { g_change_#1_seq }
}
\newchangelist{changelist}
\NewDocumentCommand{\addtochangelist}{ O{changelist} m }
{
\seq_gput_right:cn { g_change_#1_seq } { #2 }
}
\NewDocumentCommand{\getnthelement}{ O{changelist} m }
{
\seq_item:cn { g_change_#1_seq } { #2 }
}
\NewDocumentCommand{\setnthelement}{ O{changelist} m m } %not working
{
\cs_set:Npx{\seq_item:cn{g_change_#1_seq}{#2}{#3}}
}
\ExplSyntaxOff
\begin{document}
\addtochangelist{1}
\addtochangelist{0}
\addtochangelist{3}
\getnthelement{2}
%\setnthelement{1}{2}
%\setnthelement{2}{\getnthelement{2}+1}
%\getnthelement{2} %should now return 0+1=1)
\end{document}
If any of the \setnthelement lines is activated, I obtain an error like
Missing control sequence inserted. \setnthelement{1}{2}
Obviously my code for \setnthelement is wrong. Furthermore, I guess that the addition won't be that easy, so a solution for this problem is also welcome, but I can probably figure out that on my own.
Can you please help me and tell me how to set the value of a list at a specific index?
That was much easier to figure out than setting the element of the list, so thanks again, egreg!
– TP1987 Dec 21 '17 at 07:34\intevalraises an error, you can add\ProvideExpandableDocumentCommand{\inteval}{m}{\int_eval:n{#1}}just after\ExplSyntaxOn. Your system has, apparently, an older release ofexpl3. – egreg Dec 21 '17 at 09:37\getnthelementmethod expandable otherwise\intevalwill not work (that was my first mistake). – TP1987 Feb 07 '18 at 08:17