2

Is there a way I could do something like

\createlist{listone} % A description list specifically
\additem{listone}{item1}{description1}
\additem{listone}{item2}{description2}
 .
 .
 .
\makelist{listone}

I really don't know how I would google this to begin with (tried and failed). If you can point me to any documentation or tell me if it is at all possible to create a function like this then that would be amazing! Thank you!

Also if you can point me to the correct tags for this question that would be very appreciated.


Edit:

The idea is that you can have various "lists" to which you can add as many items as you need throughout the .tex. You then decide to output that description list when you use the command \makelist

  • What should \makelist do? – egreg May 5 at 20:46
  • Sorry if it wasn't clear! It is supposed to output the description list that contains the items item1 and item2 with their descriptions, which are stored in list listone. Basically, it let's you create varios "lists" to which you can add items as you wish and you decide where in the text the list is written by using the command \makelist – M.O. May 5 at 20:54
  • try https://tex.stackexchange.com/search?tab=newest&q=itemi*+list or tighter as https://tex.stackexchange.com/search?q=itemi*+list+enumerate – user170109 May 5 at 21:13
  • The indentation is easy. The hard part is nesting them. – John Kormylo May 6 at 2:59
3

You can do it with xparse and expl3:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\createlist}{m}
 {
  \seq_new:c { g_mo_list_#1_seq }
 }
\NewDocumentCommand{\additem}{mmm}
 {
  \seq_gput_right:cn { g_mo_list_#1_seq } { {#2}{#3} }
 }
\NewDocumentCommand{\makelist}{m}
 {
  \begin{description}
  \seq_map_function:cN { g_mo_list_#1_seq } \__mo_list_item:n
  \end{description}
 }
\cs_new:Nn \__mo_list_item:n { \__mo_list_item:nn #1 }
\cs_new:Nn \__mo_list_item:nn { \item[#1] #2 }

\ExplSyntaxOff

\begin{document}

\createlist{listone} % A description list specifically
\additem{listone}{item1}{description1}
\additem{listone}{item2}{description2}

\makelist{listone}

\end{document}

enter image description here

A more flexible implementation, where you can vary the formatting of the list.

The value for the command key can use #1 and #2 for referring to the two parts of the items.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\createlist}{m}
 {
  \seq_new:c { g_mo_list_#1_seq }
 }
\NewDocumentCommand{\additem}{mmm}
 {
  \seq_gput_right:cn { g_mo_list_#1_seq } { \__mo_list_do:nn {#2}{#3} }
 }
\NewDocumentCommand{\makelist}{O{}m}
 {
  \group_begin:
  \keys_set:nn { mo/list } { #1 }
  \l__mo_list_pre_tl
  \seq_use:cn { g_mo_list_#2_seq } { }
  \l__mo_list_post_tl
  \group_end:
 }
\keys_define:nn { mo/list }
 {
  pre     .tl_set:N  = \l__mo_list_pre_tl,
  post    .tl_set:N  = \l__mo_list_post_tl,
  command .code:n    = \cs_set:Nn \__mo_list_do:nn { #1 },
  pre     .initial:n = { \begin{description} },
  post    .initial:n = { \end{description} },
  command .initial:n = \item[#1] #2,
 }
\ExplSyntaxOff


\begin{document}

\createlist{listone} % A description list specifically
\additem{listone}{item1}{description1}
\additem{listone}{item2}{description2}

\makelist{listone}

\bigskip

\makelist[
  pre=\begin{tabular}{ll} Item & Description \\ \hline,
  post=\end{tabular},
  command=#1 & #2 \\,
]{listone}

\end{document}

enter image description here

  • Thank you! It works. With the search parameters KJO gave me I was able to find a question like mine that you also answered! If only I had known how to search for it. Now I will need to understand it. Thanks! – M.O. May 5 at 21:34
  • 1
    @M.O. I added a different implementation where it's easier to change the list making. – egreg May 5 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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