Pass array as macro argument

\documentclass{article}
\usepackage{listofitems,url}
\begin{document}
\newcommand{\printLinks}[1]{%
  \readlist*\mylist{#1}%
  \foreachitem\z\in\mylist{Link \#\zcnt{} is \expandafter\url\expandafter{\z}\\}%
}

\printLinks{link1, link2, ..., linkn}
% Results in \url{link1}, \url{link2} ...
\end{document}

enter image description here


Just for fun: without additional packages.

\documentclass{article}
\usepackage{url}
\makeatletter
\newcommand{\printLinks}[2][\par]{%
\@for\next:=#2\do{\expandafter\url\expandafter{\next}#1}%
}
\makeatother
\begin{document}
\printLinks{link1, link2, link3,linkn} 

\printLinks[;]{link1, link2, link3,linkn} 
\end{document}

enter image description here


As a third alternative you can also do this:

\documentclass{article}

\usepackage{pgffor}
\usepackage{hyperref}

\newcommand\printLinks[1]{%
    \foreach[count=\i] \j in {#1} {%
        \ifnum\i>1 , \fi%
        \url{\j}%
    }
}

\setlength\parindent{0pt}

\begin{document}

\printLinks{%
https://www.aaa.com,
https://www.bbb.com,
https://www.ccc.com,
https://www.ddd.com,
https://www.eee.com,
https://www.fff.com
}

\end{document}

Tags:

Arrays

Macros