4

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}

\newcommand{\mylink}[1]{#1}

\begin{document}

\qrcode[height=5cm]{www.domain.tld/\mylink{test1}}
\qrcode[height=5cm]{www.domain.tld/\mylink{test2}}
\qrcode[height=5cm]{www.domain.tld/\mylink{test3}}

\end{document}

This doesn't work

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

\documentclass{article}

\usepackage{qrcode}

\usepackage{xstring}

\newcommand{\mylink}[1]{%
    \IfEqCase{#1}{%
        {1}{test1}%
        {2}{test2}%
        {3}{test3}%
        % you can add more cases here as desired
    }[\PackageError{mylink}{Undefined option to tree: #1}{}]%
}%

\begin{document}

\qrcode[height=5cm]{www.domain.tld/\mylink{1}}
\qrcode[height=5cm]{www.domain.tld/\mylink{2}}
\qrcode[height=5cm]{www.domain.tld/\mylink{3}}

\end{document}

How can I fix this?

  • 1
    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. – Phelype Oleinik Jan 15 '18 at 11:36
4

\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
}
3

You need an expandable case switch:

\documentclass{article}

\usepackage{qrcode}
\usepackage{xparse}

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

\newcommand{\mylink}[1]{%
  \stringcase{#1}{%
    {1}{test1}%
    {2}{test2}%
    {3}{test3}%
    % you can add more cases here as desired
  }{\PackageError{mylink}{Undefined option to tree: #1}{}}%
} 

\begin{document}

\qrcode[height=5cm]{www.domain.tld/\mylink{1}}\par\bigskip
\qrcode[height=5cm]{www.domain.tld/\mylink{2}}\par\bigskip
\qrcode[height=5cm]{www.domain.tld/\mylink{3}}

\end{document}

enter image description here

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
  }{\PackageError{mylink}{Undefined option to tree: #1}{}}%
} 

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.