2

I have some sort of a list and want to add the actual enumi-count to that list. I assume, that in my problem I'm somehow just storing the pointer to that counter and not the value itself, so even the stored counter-value increases as the counter itself increases. I even tried definining a tempcounter I set to the value of the enumi-counter, but I've read somewheere, that I should expand the counter, so I'm clueless what to do.

\documentclass{scrartcl}
\usepackage{ifthen}

\def\aliste{}
\def\zaliste#1\relax{\gdef\aliste{#1}}
\def\addtoaliste#1{%
  \ifthenelse{\equal{\aliste}{}}{%
    \expandafter\zaliste\aliste#1\relax
  }{\expandafter\zaliste\aliste,#1\relax
  }%
}

\begin{document}
    Value: \aliste\\
    \addtoaliste{A}
    Value: \aliste\\ %Returns A
    \begin{enumerate}
        \item 
            Output: \arabic{enumi}\\ %is 1
            \addtoaliste{\arabic{enumi}} %adds 1 to the list
            \aliste %returns A,1
        \item 
            Output: \arabic{enumi}\\ %is 2
            \addtoaliste{\arabic{enumi}} %shall add 2 to the list
            Value: \aliste %returns A,2,2
    \end{enumerate}
    Value: \aliste %returns A,2,2
\end{document}
2

I remove ifthen (since it's considered obsolete) and use a regular \ifx\aliste\relax test to see what the value of \aliste is before adding something. Note that the addition uses \xdef - a expandable version of \gdef (which is a global definition). The global definition is required in order for the list additions to survive outside the scoped (enumerate) environment.

enter image description here

\documentclass{article}

\let\aliste\relax
\def\addtoaliste#1{%
  \ifx\aliste\relax
    \xdef\aliste{#1}% List is empty. Just add #1
  \else
    \xdef\aliste{\aliste,#1}% List is non-empty. Add old list, comma and #1
  \fi
}

\begin{document}
Value: \aliste \\

\addtoaliste{A}

Value: \aliste \\ % returns A

\begin{enumerate}
  \item 
    Output: \arabic{enumi} \\ % is 1
    \addtoaliste{\arabic{enumi}} % adds 1 to the list
    Value: \aliste % returns A,1
  \item 
    Output: \arabic{enumi} \\ % is 2
    \addtoaliste{\arabic{enumi}} % shall add 2 to the list
    Value: \aliste % returns A,1,2
\end{enumerate}

Value: \aliste % returns A,1,2

\end{document}
  • It's working, only if the list is not empty when adding the first value in the enumerate-environment. Try comment out the 3 lines before the enumerate. Solved it by changing the other gdef to xdef too – WuaghhhhXMA Sep 24 at 6:57
  • @WuaghhhhXMA: Yes, sorry. Missed that one. – Werner Sep 24 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.