# Creating a list that's callable by index

I want to define a list cp such that I can call elements of that list by their index. I was hoping that something like the following MWE would output the first date followed by the second. Any ideas on how to achieve this? I'd prefer to do this without additional packages.

\documentclass{article}
\newcommand{\cp{1}}{\textbf{(1) Monday, March 27} \\}
\newcommand{\cp{2}}{\textbf{(2) Wednesday, March 29} \\}
\begin{document}
\cp{1}
\cp{2}
\end{document}

• BTW--not sure if the macro tag is appropriate here nor what tags are. So if you're smarter than I and feeling helpful, I would appreciate your help in editing this for the proper tags. – Shane Mar 21 '17 at 20:33

\documentclass{article}
% \newdate{..label..}{..date..}{..info..}
\newcommand\newdate[3]%
{\expandafter\def\csname date:#1\endcsname{#2}%
\expandafter\def\csname info:#1\endcsname{#3}%
}

% \showdate{..label..}
\newcommand\showdate[1]%
{\expandafter\csname date:#1\endcsname}

% \showinfo{..label..}
\newcommand\showinfo[1]%
{\expandafter\csname info:#1\endcsname}

% \cp{..label..}
\newcommand\cp[1]%
{\noindent
\textbf{\showdate{#1}:} \showinfo{#1}
\par
}

\begin{document}
\newdate{1}{Monday, March 27}{My birthday}
\newdate{2}{Wednesday, March 29}{Call auntie}
\newdate{xxx}{In the near future}{Get rich}

\cp{1}
\cp{2}
\cp{xxx}
\end{document}


Here is a expl3 way with xparse (loading expl3 package) and storing the entries in \seq 'variable'. The individual items can be called with \cp{X} where X stands for a integer number.

As long as expandability is no issue this provides a quick and safe manner to perform an index - access, if the \cp command should be expandable, another approach can be given (later on).

The command \populateindexlist uses a ; separated list of index entries and initializes the list, i.e. any existing content in there will be cleared before stored. If the ; shall be a part of the content, it must be wrapped in a {} pair together with the content.

To append entries to the list, use \addtoindexlist however.

The wrapper command \indexformater is meant to simplify the formatting of the index output.

The \\ aren't necessary, in my point of view.

If \cp{X} is called with a index number larger than the number of elements nothing is printed (without any further warning). There's no check for X < 0 yet.

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_shane_index_list

\NewDocumentCommand{\populateindexlist}{+m}{%
\seq_set_split:Nnn \l_shane_index_list {;} {#1}
}

\seq_set_split:Nnn \l_tmpa_list {;} {#1}
\seq_map_inline:Nn \l_tmpa_list {
\seq_put_right:Nn \l_shane_index_list {##1}
}
}

\NewDocumentCommand{\cp}{m}{%
\int_compare:nNnT {#1} < {\seq_count:N \l_shane_index_list +1} {%
\indexformatter{#1}{\seq_item:Nn \l_shane_index_list {#1}}
}
}

\ExplSyntaxOff

\newcommand{\indexformatter}[2]{%
\textbf{(#1) #2}%
}

\populateindexlist{Monday, March 27 ;  {Wednesday, March 29}; {Wednesday; March 22}}

\parindent0em
\begin{document}

\cp{1}

\cp{3}

\cp{2}

\cp{4}