# \newcommand with argument (cases) in macro within qrcode

This works

\newcommand within qrcode.

\documentclass{article}

\usepackage{qrcode}

\newcommand{\aaa}{test1}
\newcommand{\aab}{test2}
\newcommand{\aac}{test3}

\begin{document}

\qrcode[height=5cm]{www.domain.tld/\aaa}
\qrcode[height=5cm]{www.domain.tld/\aab}
\qrcode[height=5cm]{www.domain.tld/\aac}

\end{document}


This works too

\newcommand with argument within qrcode.

\documentclass{article}

\usepackage{qrcode}

\begin{document}

\end{document}


This doesn't work

\newcommand with argument (which contains cases) within qrcode.

\documentclass{article}

\usepackage{qrcode}

\usepackage{xstring}

\IfEqCase{#1}{%
{1}{test1}%
{2}{test2}%
{3}{test3}%
% you can add more cases here as desired
}%

\begin{document}

\end{document}


How can I fix this?

• One of the problems seems to be that when you call \mylink in the \qrcode the \mylink macro is not expanded and that causes an error. A simple solution to this is to force the expansion of \mylink using \edef: \edef\mylink#1{\IfEqCase{#1}{<stuff>}[<error_stuff>]}. But that leads to another error in the \IfEqCase macro, which I could not solve, so I'll leave to someone else. Jan 15, 2018 at 11:36

\IfEqCase is not expandable and this causes the error. Use primitive \ifcase which is expandable:

\def\mylink#1{%
\ifcase#1\or test1\or test2\or test3\else\errmessage{something wrong}\fi
}


You need an expandable case switch:

\documentclass{article}

\usepackage{qrcode}
\usepackage{xparse}

\ExplSyntaxOn
\cs_new_eq:NN \stringcase \str_case:nnF
\ExplSyntaxOff

\stringcase{#1}{%
{1}{test1}%
{2}{test2}%
{3}{test3}%
% you can add more cases here as desired
}

\begin{document}

\end{document}


The log file has

<Reading QR code for "www.domain.tld/test1" at level 2-Q from aux file.>
<Reading QR code for "www.domain.tld/test2" at level 2-Q from aux file.>
<Reading QR code for "www.domain.tld/test3" at level 2-Q from aux file.>


One should note that the argument to \mylink is not limited to numbers, so

\newcommand{\mylink}[1]{%
\stringcase{#1}{%
{One}{test1}%
{Two}{test2}%
{Three}{test3}%
% you can add more cases here as desired

with \mylink{One} and so on would work as well.