4

I am trying to define an array to contain elements and stumbled across the following code and it works. The question is how I can define a command which will print all elements at once without printing each element one by one.

\documentclass{article} 
\usepackage{etoolbox}

\newcounter{cnt}

\newcommand\addtext[1]{%
\stepcounter{cnt}%
 \csdef{text\thecnt}{#1}}

\newcommand\gettext[1]{%
  \csuse{text#1}}


\begin{document}

\addtext{one}
\addtext{two}
\addtext{three}

\gettext{1}
\gettext{2}
\gettext{3}


\end{document}

2 Answers 2

1

Any looping command will do, I used \forloop from the equally named package and defined a command \getfulltext[]{number of textelements}

The first optional argument defaults to [ ] (white space between!!!) and acts as a separator of individual text elements. If no space is wanted, use [] explitly when calling.

However, the command does used \gettext always being the same text 'array'

Edit Now there is no additional spacing at the end of the list.

\documentclass{article} 

\usepackage{forloop}%


\usepackage{etoolbox}

\newcounter{cnt}

\newcounter{loopcounter}
\newcounter{numberofelements}%

\newcommand\addtext[1]{%
\stepcounter{cnt}%
 \csdef{text\thecnt}{#1}
\setcounter{numberofelements}{\number\value{cnt}}%
}%



\newcommand\gettext[1]{%
  \csuse{text#1}}

\newrobustcmd{\getfulltext}[2][ ]{%
\forloop{loopcounter}{1}{\number\value{loopcounter} < #2}{%
\ifnumless{\number\value{loopcounter}}{#2 - 1}{%
\gettext{\number\value{loopcounter}}#1%
}{%
\gettext{\number\value{loopcounter}}%
}%
}% End of loop
\setcounter{loopcounter}{0}%
}% End of command


\begin{document}

\addtext{one}
\addtext{two}
\addtext{three}

\stepcounter{numberofelements}%



  \begin{itemize}
\item Separate usage of `\texttt{\(\backslash\) gettext\{\}}`

\gettext{1}
\gettext{2}
\gettext{3}

\item No space between:

\getfulltext[]{\number\value{numberofelements}}%

\item Space between

\getfulltext{\number\value{numberofelements}}%

\item Long \(\longrightarrow\) between

\getfulltext[\(\longrightarrow\)]{\number\value{numberofelements}}% 
\end{itemize}



\end{document}

enter image description here

5

The etoolbox package has several functions for managing lists, but I'll show a different approach using xparse and expl3, the programming environment for LaTeX3.

I'll define macros for managing as many lists as you want. The macro \addtolist adds to a list; the macro \printlist maps through a list using the command given as optional argument (default is just print the item). With \listlength you access the number of items in the list.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\createlist}{ m }
 {
  \seq_new:c { g_ncory_list_#1_seq }
 }

\NewDocumentCommand{\addtolist}{ m m }
 {
  \seq_gput_right:cn { g_ncory_list_#1_seq } { #2 }
 }

\DeclareExpandableDocumentCommand{\getfromlist}{ m m }
 {
  \ncory_getfromlist:nn { #1 } { #2 }
 }

\NewDocumentCommand{\printlist}{ O{\use:n} m }
 {
  \ncory_printlist:nn { #1 } { #2 }
 }

\DeclareExpandableDocumentCommand{\listlength}{ m }
 {
  \seq_count:c { g_ncory_list_#1_seq }
 }

\cs_new:Npn \ncory_getfromlist:nn #1 #2
 {
  \int_compare:nTF { #2 > \seq_count:c { g_ncory_list_#1_seq } }
   {
    OUT~OF~RANGE
   }
   {
    \seq_item:cn { g_ncory_list_#1_seq } { #2 }
   }
 }

\cs_new_protected:Npn \ncory_printlist:nn #1 #2
 {
  \seq_map_inline:cn { g_ncory_list_#2_seq } { #1 { ##1 } }
 }

\ExplSyntaxOff

\createlist{default}
\newcommand{\foo}[1]{\item #1}

\begin{document}

\addtolist{default}{one}
\addtolist{default}{two}
\addtolist{default}{three}

\getfromlist{default}{1}

\printlist{default}, \listlength{default}

\begin{itemize}
\printlist[\foo]{default}
\end{itemize}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .