GeneralSplit to a list while passing through \keys_define

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
thanhph111
Posts: 2
Joined: Wed Dec 08, 2021 5:00 am

Split to a list while passing through \keys_define

Post by thanhph111 »

Hi, I'm new to LaTex3. I'm only familiar with the topmost layers like xparse right now but not so close to expl3, I'm trying to dig deeper.

Below is my MWE, using key-value arguments with the second one is a list of items separated by commas. The list is printed normally when I passed it directly to the \DrawList command, which has a \SplitList processor and a \ProcessList command. However, if the text is parsed by the \keys_define before being splitted, it seems to ignore the comma and the processor cannot split it at all.

How can I make it print like the \DrawList command? It's also great if anyone has a better alternative to make the interface easier. Thank you all.

Code: Select all

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand\DrawList{>{\SplitList{,}}m}{
    \begin{itemize}
        \ProcessList{#1}{\item}
    \end{itemize}
}

\keys_define:nn {module_name}{
    ,1 .tl_set:N  = \__title
    ,2 .tl_set:N  = \__list
}

\NewDocumentCommand \TitleAndList { O{} }{
    \group_begin:
    \keys_set:nn {module_name}{#1}
    \tl_if_empty:NF {\__title}{%
        \__title :\par
    }
    \tl_if_empty:NF {\__list}{%
        \DrawList{\__list}
    }
    \group_end:
}

\ExplSyntaxOff


\begin{document}

\DrawList{{First item, with a comma}, {Second item}}

---

\TitleAndList[
    1=Include,
    2={{First item, with a comma}, {Second item}}
]

---

\TitleAndList[1=Not a list]

\TitleAndList[2=An item]

\end{document}
No luck with lower-layer code:

Code: Select all

\seq_new:N \list_sequence
\NewDocumentCommand\DrawList{m}{
    \seq_set_split:Nnn \list_sequence{,}{#1}
    \begin{itemize}
        \seq_map_function:NN \list_sequence \item
    \end{itemize}
}
I've also posted the question here so you can read other answers.
Last edited by thanhph111 on Wed Dec 08, 2021 2:12 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Split to a list while passing through \keys_define

Post by Ijon Tichy »

\DrawList does not expand the argument before splitting it. So in your case it does never see {First item, with a comma}, {Second item} but always \__list, which does not contain a comma. You need at least one step of expansion of \__list so see the comma, e.g.:

Code: Select all

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand\DrawList{>{\SplitList{,}}m}{
    \begin{itemize}
        \ProcessList{#1}{\item}
    \end{itemize}
}

\keys_define:nn {module_name}{
    ,1 .tl_set:N  = \__title
    ,2 .tl_set:N  = \__list
}

\NewDocumentCommand \TitleAndList { O{} }{
    \group_begin:
    \keys_set:nn {module_name}{#1}
    \tl_if_empty:NF {\__title}{%
        \__title :\par
    }
    \tl_if_empty:NF {\__list}{%
        \exp_after:wN \DrawList \exp_after:wN {\__list}
    }
    \group_end:
}

\ExplSyntaxOff


\begin{document}

\DrawList{{First item, with a comma}, {Second item}}

---

\TitleAndList[
    1=Include,
    2={{First item, with a comma}, {Second item}}
]

---

\TitleAndList[1=Not a list]

\TitleAndList[2=An item]

\end{document}
There are other expansion functions like all the \exp_args:… that would be useful here. For example

Code: Select all

\NewDocumentCommand \TitleAndList { O{} }{
    \group_begin:
    \keys_set:nn {module_name}{#1}
    \tl_if_empty:NF {\__title}{%
        \__title :\par
    }
    \tl_if_empty:NF {\__list}{%
        \exp_args:No \DrawList{\__list}
    }
    \group_end:
}
would be an optimized definition. Maybe \exp_args:No should also be added before \tl_if_empty:NF {\__list}.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
User avatar
Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

Split to a list while passing through \keys_define

Post by Ijon Tichy »

Please always ever set links to crossposts. This can save some precious time, if the question, e.g., on TeX.SX is already completely answered. It is not that helpers have endless time. It is therefore very inconsiderate to scatter questions over several forums and to keep helpers everywhere busy with them even when the questions have already been answered elsewhere.
Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
User avatar
thanhph111
Posts: 2
Joined: Wed Dec 08, 2021 5:00 am

Split to a list while passing through \keys_define

Post by thanhph111 »

I'm sorry for that, just included my crosspost, I should have waited a while before posting elsewhere.
I also learned new approach in your answer so I will mark it accepted. Thank you for your detailed answer!
Post Reply