4

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}
1
  • 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, 2017 at 20:33

2 Answers 2

4

enter image description here

\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}
3

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}
}

\NewDocumentCommand{\addtoindexlist}{+m}{%
  \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}


\addtoindexlist{Friday, April 7}

\cp{4}
\end{document}

enter image description here

3
  • Thanks! This works great. Still interested in seeing if anybody has an alternative without an additional package. If not, I'll accept this answer and more forward with this implementation.
    – Shane
    Mar 21, 2017 at 20:43
  • @Shane: Oh, I have not realized that you don't want an additional package ...
    – user31729
    Mar 21, 2017 at 20:44
  • It's just a preference. Your answer is still very helpful to me and anybody else who might have a similar question.
    – Shane
    Mar 21, 2017 at 20:46

You must log in to answer this question.

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