\newcommand 在二维码中的宏中带有参数(案例)

\newcommand 在二维码中的宏中带有参数(案例)

这有效

\newcommand之内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}

这也有效

\newcommand与内的论点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}

这不管用

\newcommand带有参数(包含案例) 之内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}

我怎样才能解决这个问题?

答案1

\IfEqCase不可扩展,这会导致错误。请使用\ifcase可扩展的原语:

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

答案2

你需要一个可扩展案例切换:

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

在此处输入图片描述

日志文件有

<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.>

需要注意的是,的参数\mylink不仅限于数字,因此

\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}{}}%
} 

等等\mylink{One}也可以。

相关内容