我尝试在 LaTeX 中使用递归来创建项目列表。我对 LaTeX 没什么经验,所以请解释一下任何不常用的命令。这是我的代码。
\makeatletter
\newcommand\@itemsi{\@ifnextchar\stopitems{}{\@add}}
\newcommand\@add[1]{
\item #1
\@itemsi
}
\newcommand\@newitems{
\begin{itemize}
\@itemsi
\end{itemize}
}
\makeatother
这是我使用此代码得到的输出
\@newitems
{Fruits}
{Vegetables}
{Drink}
\stopitems
输出:新品水果蔬菜饮料
我想要的是这个,
- 水果
- 蔬菜
- 喝
答案1
\documentclass{article}
\begin{document}
\makeatletter% don't use @ for commands intended for document use....
\newcommand\@newitems{\begin{itemize}\@itemsi}
\def\@itemsi#1{%
\ifx\stopitems#1%
\end{itemize}\expandafter\@gobble
\else
\item#1\expandafter
\fi
\@itemsi}
This is the output I'm getting using this code
\@newitems
{Fruits}
{Vegetables}
{Drink}
\stopitems
\end{document}
答案2
递归方法
另请参见底部的笑声以了解通用实现。:)
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\quark_new:N \StopItems
\cs_new:Nn \milap_new_items: { \begin{itemize} \milap_next_item:n }
\cs_new:Nn \milap_next_item:n {
\tl_if_eq:nnTF {#1} {\StopItems}
{ \end{itemize} }
{ \item #1 \milap_next_item:n }
}
\NewDocumentCommand \NewItems { } {
\milap_new_items:
}
\ExplSyntaxOff
\begin{document}
\NewItems
{hello}
{there}
{world}
\StopItems
\end{document}
惯用语expl3
(我认为)
用于xparse
获取所有参数并expl3
使用它们:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \NewItems { u{\StopItems} } {
\tl_set:Nn \l_tmpa_tl {#1}
\begin{itemize}
\tl_map_inline:Nn \l_tmpa_tl { \item ##1 }
\end{itemize}
}
\ExplSyntaxOff
\begin{document}
\NewItems
{hello}
{world}
\StopItems
\end{document}
逗号分隔列表
为了完成:)
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand \NewItems { m } {
\clist_set:Nn \l_tmpa_clist {#1}
\begin{itemize}
\clist_map_inline:Nn \l_tmpa_clist { \item ##1 }
\end{itemize}
}
\ExplSyntaxOff
\begin{document}
\NewItems{hello,world}
\end{document}
咯咯笑
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\quark_new:N \StopItems
\cs_new:Nn \milap_new_items:nn { \begin{#1} \milap_next_item:nnn {#1} {#2} }
\cs_new:Nn \milap_next_item:nnn {
\tl_if_eq:NNTF {#3} {\StopItems}
{ \end{#1} }
{ \item #3 \milap_next_item:nnn {#1} {#2} }
}
\NewDocumentCommand \NewItems { O{itemize} O{\item} } {
\milap_new_items:nn {#1} {#2}
}
\ExplSyntaxOff
\begin{document}
\NewItems[enumerate]
{hello}
{there}
{world}
\StopItems
\end{document}
答案3
将其添加到您的序言中,并在文档内部调用
\documentclass{article}
\usepackage{etoolbox}
\makeatletter
\newcommand\makeitemize[1]{%
\begin{itemize}
\forcsvlist{\makeitemize@item}{#1}
\end{itemize}
}
\newcommand\makeitemize@item[1]{\item #1}
\makeatother
\begin{document}
\makeitemize{A,B,C}
\end{document}