4

I've used an example from

Create an array of variables

By adding a new \newcommand as in the code below:

\documentclass{article} 
\usepackage{etoolbox}

\newcounter{cnt}
\newcommand\textlist{}
\newcommand\settext[2]{%
  \csdef{text#1}{#2}}
\newcommand\addtext[1]{%
  \stepcounter{cnt}%
  \csdef{text\thecnt}{#1}}
\newcommand\gettext[1]{%
  \csuse{text#1}}

\newcounter{newcnt}
\newcommand\newtextlist{}
\newcommand\setnewtext[2]{%
  \csdef{text#1}{#2}}
\newcommand\addnewtext[1]{%
  \stepcounter{newcnt}%
  \csdef{text\thenewcnt}{#1}}
\newcommand\getnewtext[1]{%
  \csuse{text#1}}


\begin{document}

\addtext{one}
\addtext{two}
\addtext{three}
\settext{100}{one hundred}

 This is text \gettext{1} and \gettext{3}, that is text \gettext{2}.
 100 is \gettext{100}.\\

 Add new info into newtext.
\addnewtext{four}
\addnewtext{five}
\addnewtext{six}
\setnewtext{100}{seven hundred}

 This is newtext \getnewtext{1} and \getnewtext{3}, that is newtext \getnewtext{2}.

 100 is \getnewtext{100}.\\

 This is text \gettext{1} and \gettext{3}, that is text \gettext{2}.
 100 is \gettext{100}.\\

\end{document}

The first call of gettext will give a correct answer: One, two, three. But the second gettext will get data same as getnewtext above it: Four, Five, six.

How can I make it back as before which is One, Two, Three? Thank You.

  • 1
    \csdef{newtext#1}... and similarly for the other text#1 etc. in the second group. – cfr Feb 04 '16 at 04:01
  • What is \textlist supposed to do? It doesn't seem to do anything right now. – cfr Feb 04 '16 at 04:06
  • There are better ways to do this, I think. Some of the other answers there demonstrate some of them. For example, the one about etoolbox's list functions etc. (which may not have existed when the code you're using was written, I'm not sure, but they do now). – cfr Feb 04 '16 at 04:10

2 Answers2

2

Use newtext#1 etc. rather than text#1 etc. in the second list. These are defining macro names. You want them to be unique or you'll overwrite the old definitions.

Oh, and don't use \\ to break lines in ordinary contexts (outside tabular, array etc.).

\documentclass{article}
\usepackage{etoolbox}

\newcounter{cnt}
\newcommand\textlist{}
\newcommand\settext[2]{%
  \csdef{text#1}{#2}}
\newcommand\addtext[1]{%
  \stepcounter{cnt}%
  \csdef{text\thecnt}{#1}}
\newcommand\gettext[1]{%
  \csuse{text#1}}

\newcounter{newcnt}
\newcommand\newtextlist{}
\newcommand\setnewtext[2]{%
  \csdef{newtext#1}{#2}}
\newcommand\addnewtext[1]{%
  \stepcounter{newcnt}%
  \csdef{newtext\thenewcnt}{#1}}
\newcommand\getnewtext[1]{%
  \csuse{newtext#1}}


\begin{document}

\addtext{one}
\addtext{two}
\addtext{three}
\settext{100}{one hundred}

 This is text \gettext{1} and \gettext{3}, that is text \gettext{2}.
 100 is \gettext{100}.

 Add new info into newtext.
\addnewtext{four}
\addnewtext{five}
\addnewtext{six}
\setnewtext{100}{seven hundred}

 This is newtext \getnewtext{1} and \getnewtext{3}, that is newtext \getnewtext{2}.

 100 is \getnewtext{100}.

 This is text \gettext{1} and \gettext{3}, that is text \gettext{2}.
 100 is \gettext{100}.

\end{document}
cfr
  • 198,882
1

You can maintain as many arrays (actually property lists) as you want, with a consistent interface with expl3.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

% 1. The user interface
% 1a. \additem[<name>]{<text>}
\NewDocumentCommand{\additem}{ O{default} m }
 {
  \ramiza_array_add:nnn { #1 } { } { #2 }
 }
% 1b. \setitem[<name>]{<slot>}{<text>}
\NewDocumentCommand{\setitem}{ O{default} m m }
 {
  \ramiza_array_add:nnn { #1 } { #2 } { #3 }
 }
% 1c. \getitem[<name>]{<slot>}
\DeclareExpandableDocumentCommand{\getitem}{ O{default} m }
 {
  \ramiza_array_get:nn { #1 } { #2 }
 }

% 2. The internal functions
\cs_new_protected:Nn \ramiza_array_add:nnn
 {% if the array doesn't exist yet, allocate it with its related counter
  \prop_if_exist:cF { g_ramiza_array_#1_prop }
   {
    \prop_new:c { g_ramiza_array_#1_prop }
    \int_new:c { g_ramiza_array_#1_int }
   }
  \tl_if_empty:nTF { #2 }
   {% with \additem we step the counter and set the next slot
    \int_gincr:c { g_ramiza_array_#1_int }
    \prop_gput:cvn { g_ramiza_array_#1_prop } { g_ramiza_array_#1_int } { #3 }
   }
   {% with \setitem we set the requested slot
    \prop_gput:cnn { g_ramiza_array_#1_prop } { #2 } { #3 }
   }
 }
\cs_generate_variant:Nn \prop_gput:Nnn { cv }

\cs_new:Nn \ramiza_array_get:nn
 {% retrieve the requested item
  \prop_item:cn { g_ramiza_array_#1_prop } { #2 }
 }
\ExplSyntaxOff

\begin{document}

\additem{one}
\additem{two}
\additem{three}
\setitem{100}{one hundred}

This is text \getitem{1} and \getitem{3}, that is text \getitem{2}. 100 is \getitem{100}.

Add new info into newtext.
\additem[new]{four}
\additem[new]{five}
\additem[new]{six}
\setitem[new]{100}{seven hundred}

This is newtext \getitem[new]{1} and \getitem[new]{3}, that is newtext \getitem[new]{2}.

100 is \getitem[new]{100}.

This is text \getitem{1} and \getitem{3}, that is text \getitem{2}. 100 is \getitem{100}.

\end{document}

There is a “default” array, which you access without the optional argument to \additem, \setitem or \getitem; as soon as you use the optional argument to \additem or \setitem, a new array is allocated, to be used with \getitem.

enter image description here

egreg
  • 1,121,712