我希望能够将数组作为宏参数传递,例如:
\newcommand{\printLinks}[1]{
//For each links in #1 print it
}
\printLinks{ {link1, link2, ..., linkn} }
% Results in \url{link1}, \url{link2} ...
在 LaTeX 中可以实现吗?
答案1
您可以使用listofitems
包裹。
\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}
答案2
答案3
作为第三种选择,您也可以这样做:
\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}
答案4
LaTeX3新手的解决方案:
\documentclass{article}
\usepackage{expl3}
\usepackage{url}
\usepackage{xparse}
\ExplSyntaxOn
\clist_new:N \l_links_printlinks
\NewDocumentCommand { \printLinks } { O{\par} m }
{
\clist_set:Nn \l_links_printlinks { #2 }
\clist_map_inline:Nn \l_links_printlinks { \url{ ##1 } #1 }
}
\ExplSyntaxOff
\begin{document}
\printLinks{link1, link2, link3, link4}
\printLinks[; ]{link1, link2, link3, link4}
\end{document}
输出: