[Tex/LaTex] How to create a list as a variable that can be appended and eventually typeset

programming

Question

In languages such as Python, you can define a list like so:

example_list = ['apples']

I can append this list like so:

example_list.append('oranges')

I can print this list using

print example_list

which results in:

['apples', 'oranges']

How can I do something similar in LaTeX?


This question is part of a trail of questions. The goal mine was to use this list inside of a tikz node, but I removed that from this question in the interest of keeping it simple. So, for those interested, here is a quick list for reference:

For making a macro that can handle post-parsing-inputted list items, see:

For that same solution with tikz node support, see:


Situation

I have a list of inputted languages (see my other question). I would like to capture each language code into a list that I can print to paper as an inline list (i.e. da, de, en, etc.)

I would like to title page to look like this:

TITLE

da, de, en … (or instead of ISO 639-1 codes, a list of full language names)


Appendage

There are a few good answers already. Although the original question was answered, my problem was not solved due to some other things I did not originally think about. 🙁 Maybe somebody can help me.

I have a list of documents to input

Sudo-Code

List based on Werner's answer

\maketitle[\printlist{languagelist}]{User Manual}
\inputlanguagefile{En}{English}{./Languages/en/UserManual_en.tex}
\inputlanguagefile{Da}{Dansk}{./Languages/da/UserManual_da.tex}
\inputlanguagefile{De}{Deutsch}{./Languages/de/UserManual_de.tex}

I have a custom \maketitle command where the mandatory input {} is the title of the document and the optional [] is the subtitle (list of ISO 639-1 language codes) My problem is that the \maketitle command is called before the file inputs are called, which means that at the time the language list is called, it is still empty. I need to find a way to loop the input commands one by one to grab the two-letter language codes (#1) and append them to my list.

Then I want to actually add the inputs after the \maketitle as shown in the sudo-code. Obviously, the inputted documents should appear after the cover page, but I need to parse the language codes before the maketitle page is called.

Best Answer

etoolbox provides a host of list managing tools. Here is one way using \listadd{<stuff>} that seems to match your requirements:

enter image description here

\documentclass{article}
\usepackage{etoolbox}

% \printlist[<sep>]{<list macro>}
\newcommand{\printlist}[2][,]{{% Print list
  % http://tex.stackexchange.com/a/89187/5764
  \def\listsep{\def\listsep{#1}}% Delayed execution of list separator
  \renewcommand{\do}[1]{\listsep`##1'}%
  [\dolistloop\languagelist]
}}
\begin{document}

\listadd{\languagelist}{de}
\listadd{\languagelist}{da}
\listadd{\languagelist}{en}

\printlist{\languagelist}

\end{document}

There are other ways to add list elements (pre-expanded, globally, etc.). The only requirement is that you define the elements contained in \languagelist before printing it via \printlist. That is, it doesn't work like the \label-\ref system where you can refer to future \labels.