4

I have created a command that uses 3 arguments, i.e,

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

so, when I use this command I use \mycommand{arg1}{arg2}{arg3}, but instead I want somethink like \mycommand{arg1,arg2,arg3}.

Thanks in advance.

  • You can use keys to achieve this, among other things. – A.Ellett Sep 28 '18 at 21:08
5

As many rows as you want:

\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}

enter image description here

Alternative approach:

\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}

Look, ma! No packages!

\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}

Addition

Suppose you want to uppercase the items. Then the “no packages” approach is

\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}

The xparse approach is much easier:

\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}

enter image description here

  • Thanks for your answer, but I want to create the command whitout any package. – Joan Sep 28 '18 at 21:11
  • @Joan Added, but you should not do such requirements. ;-) – egreg Sep 28 '18 at 21:15
  • I'm try to add to your code a \MakeUppercase, i.e, on the line \@for\next:=\MakeUppercase{#1}\do{\joan@rows=\expandafter{\the\expandafter\joan@rows\next\\}}%, but it does not work :( – Joan Sep 28 '18 at 21:31
  • 1
    @Joan Surely not. Try \@for\next:=#1\do{\joan@rows=\expandafter{\the\expandafter\joan@rows\expandafter\MakeUppercase\expandafter{\next}\\} – egreg Sep 28 '18 at 21:35
  • How could I fix it to work with \MakeUppercase ? – Joan Sep 28 '18 at 21:36
4

Here's very crude approach

\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}

Here's a non-xparse approach that allows you as few and as many arguments as you would like:

\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}

enter image description here

I didn't realize you didn't want to load any additional packages. Here I do something similar to egreg (using \newtoks), but instead of a for-loop I stick with the basic syntax I used above:

\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}

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.