如何传递无限数量的参数?
我想要以下类型的命令:
\coollist{a}{b}{c}{d}{e}{f}... {y}{z}
。
更具体地说,我需要一个命令\lexinfinite
,允许我{a,b,c}
在文本中传递无限数量的类型项目,对应于列表中的词素(A斜体字,b下标语法类别,以及C译文):
\lexinfinite{a,b,c}{a',b',c'}{...}
平均能量损失
\documentclass{article}
\usepackage{xspace}
\newcommand{\tslt}[1]{\unskip\xspace`#1'}
\NewDocumentCommand{\lexAUX}{mmm}{\textit{#1}\IfNoValueF{#2}{\textup{\textsubscript{\textsc{#2}}}}\IfNoValueF{#3}{\tslt{#3}}}
\NewDocumentCommand{\lex}{>{\SplitArgument{2}{,}}m}{\lexAUX#1}
\newcommand{\lexduo}[2]{\lex{#1}\space and\space\lex{#2}}
\newcommand{\lextrio}[3]{\lex{#1},\space\lex{#2}\space and\space\lex{#3}}
\begin{document}
\lex{tree,n,arbre}, \lex{apple,n,pomme} and \lex{stuff,n,thing}
\lexduo{tree,n,arbre}{apple,n,pomme}
\lextrio{tree,n,arbre}{apple,n,pomme}{stuff,n,thing}
\end{document}
答案1
我不明白它的目的,但这是你想要的吗?
\documentclass{article}
\usepackage{xspace,xparse}
\newcommand{\tslt}[1]{\unskip\xspace`#1'}
\NewDocumentCommand{\lexAUX}{mmm}{\textit{#1}\IfNoValueF{#2}{\textup{\textsubscript{\textsc{#2}}}}\IfNoValueF{#3}{\tslt{#3}}}
\NewDocumentCommand{\lexTRIPLE}{>{\SplitArgument{2}{,}}m}{\lexAUX#1}
\DeclareDocumentCommand\lexEXTRA{g}{%
\IfValueT{#1}{%
,\space\lexTRIPLE{#1}\lexEXTRA
}%
}
\DeclareDocumentCommand\lex{m}{%
\lexTRIPLE{#1}%
\lexEXTRA
}
\begin{document}
\lex{tree,n,arbre}, \lex{apple,n,pomme} and \lex{stuff,n,thing}
\lex{tree,n,arbre}{apple,n,pomme}
\lex{tree,n,arbre}{apple,n,pomme}{stuff,n,thing}
\end{document}
稍微不那么简洁的版本添加了“and”:
\documentclass{article}
\usepackage{xspace,xparse}
\newcommand{\tslt}[1]{\unskip\xspace`#1'}
\NewDocumentCommand{\lexAUX}{mmm}{\textit{#1}\IfNoValueF{#2}{\textup{\textsubscript{\textsc{#2}}}}\IfNoValueF{#3}{\tslt{#3}}}
\NewDocumentCommand{\lexTRIPLE}{>{\SplitArgument{2}{,}}m}{\lexAUX#1}
\DeclareDocumentCommand\lexEXTRA{mg}{%
\IfValueTF{#2}{%
,\space\lexTRIPLE{#1}\lexEXTRA{#2}%
}{%
\space and\space\lexTRIPLE{#1}%
}%
}
\DeclareDocumentCommand\lex{mg}{%
\lexTRIPLE{#1}%
\IfValueT{#2}{%
\lexEXTRA{#2}%
}%
}
\begin{document}
\lex{tree,n,arbre}, \lex{apple,n,pomme} and \lex{stuff,n,thing}
\lex{tree,n,arbre}{apple,n,pomme}
\lex{tree,n,arbre}{apple,n,pomme}{stuff,n,thing}
\end{document}
答案2
您可以使用 TeX 原语和基本宏来定义宏:
\documentclass{article}
\newcount\lexcount
\def\lexi#1,#2,#3,#4^{{\it #1}\if^#2^\else\uppercase{$_{\rm #2}$}\if^#3^\else~`#3'\fi\fi}
\def\lexinf {\lexcount=0 \def\lexinfS{}\futurelet\next\lexinfA}
\def\lexinfA {\ifx\next\bgroup \expandafter\lexinfB \else \expandafter\lexinfS \fi}
\def\lexinfB #1{\advance\lexcount by1 \addto\lexinfS{\lexinfC{#1}}\futurelet\next\lexinfA}
\def\lexinfC#1{\lexi #1,,,^\advance\lexcount by-1 \ifcase\lexcount \or \ and \else , \fi}
\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\begin{document}
\lexinf{tree,n,arbre}
\lexinf{tree,n,arbre}{apple,n,pomme}
\lexinf{tree,n,arbre}{apple,n,pomme}{stuff,n,thing}
\lexinf{tree,n,arbre}{apple,n,pomme}{stuff,n,thing}{last,x,uff}
\end{document}
答案3
您是这个意思吗?或者您是否需要以某种方式将省略号 (...) 合并到输入/输出中?
\documentclass{article}
\usepackage{listofitems}
\newcommand\coolitlist[1]{%
\readlist*\mylist{#1}%
\foreachitem\z\in\mylist[]{%
$\z$%
\ifnum\zcnt<\listlen\mylist[]\relax, \fi
\ifnum\zcnt=\numexpr\listlen\mylist[]-1\relax and \fi
}%
}
\begin{document}
\coolitlist{a, b, c}
\coolitlist{a,b,c,d,e,f}
\end{document}
附录
也许这个添加\lexlist
解决了 OP 对命令列表的评论\lex
。
\documentclass{article}
\usepackage{listofitems,xspace}
\newcommand\coolitlist[1]{%
\readlist*\mylist{#1}%
\foreachitem\z\in\mylist[]{%
$\z$%
\ifnum\zcnt<\listlen\mylist[]\relax, \fi
\ifnum\zcnt=\numexpr\listlen\mylist[]-1\relax and \fi
}%
}
\newcommand\lexlist[1]{%
\readlist*\mylist{#1}%
\foreachitem\z\in\mylist[]{%
\expandafter\lex\expandafter{\z}%
\ifnum\zcnt<\listlen\mylist[]\relax, \fi
\ifnum\zcnt=\numexpr\listlen\mylist[]-1\relax and \fi
}%
}
\NewDocumentCommand{\lexAUX}{mmm}{\textit{#1}\IfNoValueF{#2}{\textup{\textsubscript{\textsc{#2}}}}\IfNoValueF{#3}{\tslt{#3}}}
\NewDocumentCommand{\lex}{>{\SplitArgument{2}{,}}m}{\lexAUX#1}
\newcommand{\tslt}[1]{\unskip\xspace`#1'}
\begin{document}
\coolitlist{a, b, c}
\coolitlist{a,b,c,d,e,f}
\lex{house,n,maison}
\lexlist{{house,n,maison},{arbre,n,tree},{stuff,n,thing}}
\end{document}
次要答案
根据 OP 的要求,这将\lexlist
吸收尽可能多的组,以便不需要对组进行分组,如前面的答案所示:
\documentclass{article}
\usepackage{listofitems,xspace}
\newtoks\lextoks
\ignoreemptyitems
\newcommand\coolitlist[1]{%
\readlist*\mylist{#1}%
\foreachitem\z\in\mylist[]{%
$\z$%
\ifnum\zcnt<\listlen\mylist[]\relax, \fi
\ifnum\zcnt=\numexpr\listlen\mylist[]-1\relax and \fi
}%
}
\newcommand\lexlist{\lextoks{}\lexlistA}
\newcommand\lexlistA{\futurelet\nexttok\lexlistB}
\newcommand\lexlistB{\ifx\nexttok\bgroup\expandafter\lexlistC
\else\expandafter\lexlistD\expandafter{\the\lextoks}\fi}
\newcommand\lexlistC[1]{\lextoks\expandafter{\the\lextoks{#1},}%
\lexlistA}
\newcommand\lexlistD[1]{%
\readlist*\mylist{#1}%
\foreachitem\z\in\mylist[]{%
\expandafter\lex\expandafter{\z}%
\ifnum\zcnt<\listlen\mylist[]\relax, \fi
\ifnum\zcnt=\numexpr\listlen\mylist[]-1\relax and \fi
}%
}
\NewDocumentCommand{\lexAUX}{mmm}{\textit{#1}\IfNoValueF{#2}{\textup{\textsubscript{\textsc{#2}}}}\IfNoValueF{#3}{\tslt{#3}}}
\NewDocumentCommand{\lex}{>{\SplitArgument{2}{,}}m}{\lexAUX#1}
\newcommand{\tslt}[1]{\unskip\xspace`#1'}
\begin{document}
\coolitlist{a, b, c}
\coolitlist{a,b,c,d,e,f}
\lex{house,n,maison}
\lexlist{house,n,maison}{arbre,n,tree}{stuff,n,thing} or else
plain text
\end{document}
答案4
您可以使用pgffor
包(由 加载tikz
)来利用该...
语法。
\documentclass{article}
\usepackage{pgffor, ifthen}
\newcommand{\coolitlist}[1]{\foreach\W[count=\n]in{#1}{\xdef\nnn{\n}}%
\foreach\W[count=\n, evaluate=\n as \m using int(\n+1)]in{#1}
{\ifthenelse{\n=\nnn}{ and \W}{\ifthenelse{\m=\nnn}{\W}{\W,\ }}}}
\begin{document}
I like \coolitlist{a,b,...,g}.
I like \coolitlist{10,9,...,2}.
I like \coolitlist{{blue},{green},{yellow}}.
\end{document}