Macro - Repeat the pattern for any (even) number of arguments

You can as a stopping criteria to halt the search for paired arguments. Such a stopping criteria could be \ByTwo ... \StopByTwo, say:

enter image description here

\documentclass{article}

\makeatletter
\newcommand{\ByTwo}{\@ifnextchar\StopByTwo\relax\@ByTwo}
\newcommand{\@ByTwo}[2]{ {\bfseries #1} {`#2'} \ByTwo}
\makeatother
\let\StopByTwo\relax

\begin{document}

\ByTwo{A}{B}\StopByTwo

\ByTwo{A}{B}{C}{D}{E}{F}\StopByTwo

\ByTwo ABCDEF\StopByTwo

\end{document}

Here is how to implement such a macro with using a stopper. The answer is in ConTeXt because I use the \dodoublegroupempty macro from ConTeXt. I am sure there will be a similar helper macro in the LaTeX kernel, which can be used in a LaTeX solution.

\unexpanded\def\ByTwo
    {\dodoublegroupempty\doByTwo}

\def\doByTwo#1#2%
    {\ifsecondargument
       {\bf #1} ‘#2’ 
       \expandafter\ByTwo
     \fi}

\starttext
\startlines
\ByTwo{A}{B}
\ByTwo{A}{B}{C}{D}
\ByTwo{A}{B}{C}{D}{E}{F}
\stoplines
\stoptext

which gives

enter image description here


I propose an answer without end markup.

There is some problem with this question: what must be the answer if you provide an odd number of arguments? My answer to this question is that:

\AddAnyEvenNumberOfArguments{A}{B}{C}{D}{E}

should display

but you can easily change the code if you want another behavior.

\documentclass{article}

\makeatletter
\newcommand\AddAnyEvenNumberOfArgument{%
  \@ifnextchar\bgroup{\AddCoupleArgument}{}
}
\newcommand\AddCoupleArgument[1]{%
  \textbf{#1}
  \@ifnextchar\bgroup{\AddSecondArgument}{\AddEmptySecondArgument}
}
\newcommand\AddSecondArgument[1]{%
  `#1'
  \@ifnextchar\bgroup{\AddCoupleArgument}{}
}
\newcommand\AddEmptySecondArgument{%
  `'
}
\makeatother

\begin{document}
\AddAnyEvenNumberOfArgument{A}\par
\AddAnyEvenNumberOfArgument{A}{B}\par
\AddAnyEvenNumberOfArgument{A}{B}{C}\par
\AddAnyEvenNumberOfArgument{A}{B}{C}{D}\par
\AddAnyEvenNumberOfArgument{A}{B}{C}{D}{E}\par
\AddAnyEvenNumberOfArgument{A}{B}{C}{D}{E}{F}\par
\AddAnyEvenNumberOfArgument{A}{B}{C}{D}{E}{F}{G}
\end{document}

Pay attention that if the code after \AddAnyEvenNumberOfArguments begins with a left brace then this group will be eat by this macro! Logical. Normal. Add a \relax For exemple

\AddAnyEvenNumberOfArgument{A}{B}{C}{D}{E}{F}\relax
{\bfseries That's all folks}

Tags:

Macros