瞧,妈妈!没有包裹!

瞧,妈妈!没有包裹!

我创建了一个使用 3 个参数的命令,即

\newcommand{\mycommand}[3]{%
   \begin{tabular}{c}
       #1 \\
       #2 \\
       #3
   \end{tabular}
}

因此,当我使用这个命令时,我使用\mycommand{arg1}{arg2}{arg3},但是我想要的是像这样的一些东西\mycommand{arg1,arg2,arg3}

提前致谢。

答案1

任意数量的行:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
 {
  \begin{tabular}{c}
  \seq_set_from_clist:Nn \l_tmpa_seq { #1 }
  \seq_use:Nn \l_tmpa_seq { \\ }
  \end{tabular}
 }
\ExplSyntaxOff

\begin{document}

\mycommand{a} \mycommand{a,b} \mycommand{a,b,c,d,e,f}

\end{document}

在此处输入图片描述

替代方法:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
 {
  \begin{tabular}{c}
  \clist_map_function:nN { #1 } \joan_makerow:n
  \end{tabular}
 }
\cs_new_protected:Nn \joan_makerow:n { #1 \\ }
\ExplSyntaxOff

\begin{document}

\mycommand{a} \mycommand{a,b} \mycommand{a,b,c,d,e,f}

\end{document}

瞧,妈妈!没有包裹!

\documentclass{article}

\makeatletter
\newtoks\joan@rows
\newcommand{\mycommand}[1]{%
  \begin{tabular}{c}
  \@for\next:=#1\do{\joan@rows=\expandafter{\the\expandafter\joan@rows\next\\}}%
  \the\joan@rows
  \end{tabular}%
}
\makeatother

\begin{document}

\mycommand{a} \mycommand{a,b} \mycommand{a,b,c,d,e,f}

\end{document}

添加

假设你想将项目大写。那么“无包裹”方法是

\documentclass{article}

\makeatletter
\newtoks\joan@rows
\newcommand{\mycommand}[1]{%
  \begin{tabular}{c}
  \@for\next:=#1\do{\joan@rows=\expandafter{\the\expandafter\joan@rows\expandafter\MakeUppercase\expandafter{\next}\\}}%
  \the\joan@rows
  \end{tabular}%
}
\makeatother

\begin{document}

\mycommand{a} \mycommand{a,b} \mycommand{a,b,c,d,e,f}

\end{document}

方法xparse很多更轻松:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\mycommand}{m}
 {
  \begin{tabular}{c}
  \clist_map_function:nN { #1 } \joan_makerow:n
  \end{tabular}
 }
\cs_new_protected:Nn \joan_makerow:n { \tl_upper_case:n { #1 } \\ }
\ExplSyntaxOff

\begin{document}

\mycommand{a} \mycommand{a,b} \mycommand{a,b,c,d,e,f}

\end{document}

在此处输入图片描述

答案2

这是非常粗鲁的方法

\documentclass{article}

\makeatletter
\newcommand\mycmd[1]{\ae@my@cmd#1;}
\def\ae@my@cmd#1,#2,#3;{%%
  \begin{tabular}{c}
  #1 \\
   #2\\
  #3
  \end{tabular}}


\makeatother
\begin{document}

\noindent\mycmd{hello, there,folks}

\end{document}

这是一种非 xparse 方法,允许您根据需要使用尽可能少或尽可能多的参数:

\documentclass{article}
\usepackage{etoolbox}

\makeatletter
\newcommand\mycmd[1]{%%
  \def\my@table{\begin{tabular}{c}}%%
  \ae@my@cmd#1,\relax,\relax,\relax;}

\def\ae@my@cmd#1,#2,#3;{%%
  \ifx\relax#1
    \xdef\my@table{\expandonce\my@table\noexpand\end{tabular}}%%
    \let\my@execute\my@table
  \else
    \def\ae@tmp{#1 \\}%%
    \xdef\my@table{\expandonce\my@table\expandonce\ae@tmp}%%
    \def\my@execute{\ae@my@cmd#2,#3;}%%
  \fi
  \my@execute
}

\makeatother
\begin{document}

\noindent
\mycmd{hello, there,folks}%%
\hspace{0.5em}%%a
\mycmd{a,b,c,d,e,f,g}%%
\hspace{0.5em}%%a
\mycmd{ONLY THIS ENTRY}%%

\end{document}

在此处输入图片描述

我没有意识到你不想加载任何额外的包。在这里我做了一些类似于 egreg 的事情(使用\newtoks),但我坚持使用上面使用的基本语法,而不是 for 循环:

\documentclass{article}

\makeatletter
\newtoks\my@table
\newcommand\mycmd[1]{%%
  \my@table={\begin{tabular}{c}}%%
  \ae@my@cmd#1,\relax,\relax,\relax;}

\def\ae@my@cmd#1,#2,#3;{%%
  \ifx\relax#1
    \my@table=\expandafter{\the\my@table\end{tabular}}%%
    \def\my@execute{\the\my@table}%%
  \else
    \my@table=\expandafter{\the\my@table#1 \\}%%
    \def\my@execute{\ae@my@cmd#2,#3;}%%
  \fi
  \my@execute
}

\makeatother
\begin{document}

\noindent
\mycmd{hello, there,folks}%%
\hspace{0.5em}%%a
\mycmd{a,b,c,d,e,f,g}%%
\hspace{0.5em}%%a
\mycmd{ONLY THIS ENTRY}%%

\end{document}

相关内容