How to write a LaTeX macro that doesn’t need a fixed number of arguments
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 4×4=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:
- \newcommand{\THE_MACRO_NAME}[4]{ #1 #2 #3 #4 \checknextarg}
- \newcommand{\checknextarg}{\@ifnextchar\bgroup{\gobblenextarg}{ }}
- \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 %header \setlength{\arrayrulewidth}{\[email protected]} \ecvblueitem{Academic transcript}{ \renewcommand{\arraystretch}{1.5} \setlength{\[email protected]}{\[email protected]} \addtolength{\[email protected]}{\arrayrulewidth} \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} %header %\setlength{\arrayrulewidth}{\[email protected]} \ecvblueitem[-5pt]{}{ \renewcommand{\arraystretch}{1.5} \setlength{\[email protected]}{\[email protected]} \addtolength{\[email protected]}{\arrayrulewidth} \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:
Download
You can find and verify my macro in Overleaf and GitHub at these following links:
Fonte: Source
Se vuoi sostenerci, puoi farlo acquistando qualsiasi cosa dai diversi link di affiliazione che abbiamo nel nostro sito o partendo da qui oppure alcune di queste distribuzioni GNU/Linux che sono disponibili sul nostro negozio online, quelle mancanti possono essere comunque richieste, e su cui trovi anche PC, NAS e il ns ServerOne. Se ti senti generoso, puoi anche donarmi solo 1€ o più se vuoi con PayPal e aiutarmi a continuare a pubblicare più contenuti come questo. Grazie!
Hai dubbi o problemi? Ti aiutiamo noi!
Se vuoi rimanere sempre aggiornato, iscriviti al nostro canale Telegram.Se vuoi ricevere supporto per qualsiasi dubbio o problema, iscriviti alla nostra community Facebook o gruppo Telegram.
Cosa ne pensi? Fateci sapere i vostri pensieri nei commenti qui sotto.
Ti piace quello che leggi? Per favore condividilo con gli altri.