5

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.

Joan
  • 479

2 Answers2

8

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

egreg
  • 1,121,712
  • 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
  • @Joan I added it to my previous comment. – egreg Sep 28 '18 at 21:37
  • It's perfect! thank you very much :-) – Joan Sep 28 '18 at 21:41
  • Hi @egreg. I want to create a command with arguments separated by comma, where each of the arguments I want to manipulate them in the most general way possible, whether using tables, boxes, etc.How can I do it? (without package :) ) – Joan Jan 26 '19 at 20:18
  • @Joan I'm afraid this is too generic. – egreg Jan 26 '19 at 21:28
  • Can you give me a basic example to create a command like that please? – Joan Jan 26 '19 at 21:31
  • What I want is to create a command that receives as many arguments as I want separated by comma in such a way that each argument can be used in a box, table, etc. – Joan Jan 26 '19 at 21:38
  • @Joan Used how? You see it's too generic, without a real specification. – egreg Jan 26 '19 at 22:08
  • For example, \mycommand{name, last name, age, ...} and that each argument can manipulate it as I want. It's possible? – Joan Jan 26 '19 at 22:24
  • @Joan Of course. But until you say *how* you want to manipulate it, it's like “I want to fly to the Moon, can I do it?” Yes, you can, provided you build a rocket and many more things next to it. – egreg Jan 26 '19 at 22:26
  • For now I want to create the dedication page of my thesis, then I would like to create a command like this \mydedication{dedicated to person 1, text 1, dedicated to person 2, text 2, ..., and more people if I would like} so that I can place each dedication in a box and can give it the format I want. Excuse me if I do not explain myself well. – Joan Jan 26 '19 at 22:36
  • @Joan This is not the best approach to the task. Anyway, open a new question with some more details. – egreg Jan 26 '19 at 22:39
  • That being the case, in what way do you recommend me to do it? PD:I forgot to mention that this command is for the class I'm creating – Joan Jan 26 '19 at 22:42
5

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}
A.Ellett
  • 50,533