How to write a LaTeX macro that doesn't need a fixed number of arguments

Submitted by Nicola Rainiero on 2018-05-09

In order to answer at a comment in an old article, I have discovered an interesting trick to bypass the limit of the predetermined arguments that define a LaTeX macro. In the following sections I explain the basic idea under the hood with the solution for creating a dynamic table that supports a multiple of 4 arguments at time.

In the comment there was a request that sounds like this: «How to add a table that contains the University exams with date, exam, credits and grade in my EuroPASS CV template that respect the style of it?».

The question is simple and almost easy to resolve but manually, i.e. writing a code and every time starting from a scratch. Against the idea that goes with the template: a series of macro easy to call and use.

From the LaTeX/Macros wiki available here, to add your own commands, you have to use:

\newcommand{name}[num]{definition}

Basically, the command requires two arguments: the name of the command you want to create, and the definition of the command. Note that the command name can but need not be enclosed in braces, as you like. The num argument in square brackets is optional and specifies the number of arguments the new command takes (up to 9 are possible). If missing it defaults to 0, i.e. no argument allowed.

The problem

So, if I wanted to realize an handy macro that should simplifiy that procedure, how do I should do? I will have many problems for sure, due to the number of exames that the user have to highlight (all or the most importants for the CV?) and still remains the limit of default arguments that is 9. But above all, every University faculty has a fixed number of exams, therefore I can't define from the start, how many variables should be put in a classical LaTeX macro.

In roughly words I can't establish how many arguments my macro needs but only a multiple number of them, for example if I want to write 4 exams: I need 4x4=16 arguments and so on.

The only solution is a macro that repeats itself in function of the number of arguments. As well described in this article: Writing a LaTeX macro that takes a variable number of arguments.

Summing up with a macro divided in the following three part, I am able to solve the problem:

1. \newcommand{\THE_MACRO_NAME}[4]{ #1 #2 #3 #4 \checknextarg}
2. \newcommand{\checknextarg}{\@ifnextchar\bgroup{\gobblenextarg}{ }}
3. \newcommand{\gobblenextarg}[4]{#1 #2 #3 #4\@ifnextchar\bgroup{\gobblenextarg}{ }}

My macro for the Europass CV

The code for the europasscv.cls:

% University exam transcript
\newcommand*\ecvexames[4]{%
\def\[email protected]{0.25} % because I want 4 column
\setlength{\arrayrulewidth}{\[email protected]}
\renewcommand{\arraystretch}{1.5}
\setlength{\[email protected]}{\[email protected]}
\arrayrulecolor{ecvtablebordercolor}
\begin{tabular}[t]{m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}}
\firsthline
\centering \ecvbluestyle{Date}& \centering \ecvbluestyle{Exam} &\centering \ecvbluestyle{Credits}& \centering \ecvbluestyle{Grade} \tabularnewline
\hline
\centering{#1} &\centering{#2} &\centering{#3} &\centering{#4} \tabularnewline
\hline
\end{tabular}
}
\checknextarg
}

\newcommand{\checknextarg}{\@ifnextchar\bgroup{\gobblenextarg}{
}}

\newcommand{\gobblenextarg}[4]{
\def\[email protected]{0.25}
%\setlength{\arrayrulewidth}{\[email protected]}
\ecvblueitem[-5pt]{}{
\renewcommand{\arraystretch}{1.5}
\setlength{\[email protected]}{\[email protected]}
\arrayrulecolor{ecvtablebordercolor}
%\rowcolors{3}{white}{ecvtablebordercolor}
\begin{tabular}[t]{m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}|m{\[email protected]\[email protected]}}
\centering{#1} &\centering{#2} &\centering{#3} &\centering{#4} \tabularnewline
\hline
\end{tabular}
}
\@ifnextchar\bgroup{\gobblenextarg}{
}}


The code for main.tex file:

\ecvexames
{Date 1}{Exam 1}{Credits 1}{Grade 1}
{Date 2}{Exam 2}{Credits 2}{Grade 2}
{Date 3}{Exam 3}{Credits 3}{Grade 3}
{Date 4}{Exam 4}{Credits 4}{Grade 4}
{Date ...}{Exam ...}{Credits ...}{Grade ...}
{Date N}{Exam N}{Credits N}{Grade N}


The final result:

You can find and verify my macro in Overleaf and GitHub at these following links:

Related Content:

Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.