Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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