1

I have two macros. The first, \qrwifidata, generates a string from two arguments and some optional key value. The second, \qrwifi, takes the same arguments and generates a QR code with text overlay.

\newcommand{\qrwifidata}[3][]{\begingroup\setkeys{qrwifi}{#1}%
    WIFI:S:#2;T:\qw@enc;P:#3;;%
\endgroup}

\newcommand{\qrwifi}[3][]{\begingroup\setkeys{qrwifi}{#1}%
    \begin{tikzpicture}%
        \node at (0, 0) {\qrcode{WIFI:S:#2;P:#3;T:\qw@enc;;}};%
        \node[fill=white,align=center] at (0, 0) {#2\\#3};%
    \end{tikzpicture}%
\endgroup}

Both work fine on their own. But now, I want to reuse the first macro inside the second as

\newcommand{\qrwifi}[3][]{\begingroup\setkeys{qrwifi}{#1}%
    \begin{tikzpicture}%
        \node at (0, 0) {\qrcode{\qrwifidata[encryption=\qw@enc]{#2}{#3}}};%
        \node[fill=white,align=center] at (0, 0) {#2\\#3};%
    \end{tikzpicture}%
\endgroup}

However, using the latter version produces a lot of errors like 'Undefined control sequence' and 'Illegal parameter number'. I guess, this has to do with macro expansion order, so I tried inserting some \expandafters, but to no avail.

How can I have \qrwifidata expanded first and its result passed to \qrcode?

For completeness, here is a minimal (non)working example:

\documentclass{article}

\usepackage{keyval}

\usepackage{qrcode}
\usepackage{tikz}

\makeatletter

\newlength{\qw@size}
\define@key{qrwifi}{size}{\setlength\qw@size{#1}}

\define@key{qrwifi}{encryption}{\def\qw@enc{#1}}

\setkeys{qrwifi}{
    encryption=WPA,
    size=5cm,
}

\newcommand{\qrwifidata}[3][]{\begingroup\setkeys{qrwifi}{#1}%
    WIFI:S:#2;T:\qw@enc;P:#3;;%
\endgroup}

\newcommand{\qrwifi}[3][]{\begingroup\setkeys{qrwifi}{#1}%
    \begin{tikzpicture}%
        \node at (0, 0) {\qrcode[level=H,height=\qw@size,padding]{WIFI:S:#2;P:#3;T:\qw@enc;;}};%
%        \node at (0, 0) {\qrcode[level=H,height=\qw@size,padding]{\qrwifidata[encryption=\qw@enc]{#2}{#3}}};%
        \node[fill=white,align=center] at (0, 0) {#2\\#3};%
    \end{tikzpicture}%
\endgroup}

\makeatother

\begin{document}
    \qrwifidata[encryption=WEP]{SSID}{Key}

    \qrwifi[encryption=WEP]{SSID}{Key}
\end{document}
  • 1
    \qrcode needs to see something that expands to the things to encode, not the whole set of instruction to produce them. – egreg Jul 22 '18 at 14:39
  • What would that be? I also tried \def\qw@data{\qrwifidata[encryption=\qw@enc]{#2}{#3}} and then \qrcode{\qe@data}. But this still produces the same errors. – DLichti Jul 22 '18 at 14:52
  • Of course: they do exactly the same thing. – egreg Jul 22 '18 at 14:53
  • I'm not sure why you pass again the option to \qrwifidata, which is already known. – egreg Jul 22 '18 at 15:01
0

I'm not sure why you want to evaluate again a key that's already set to the desired value.

Anyway, \qrcode expands its second argument, but key setting cannot appear there.

\documentclass{article}

\usepackage{keyval}

\usepackage{qrcode}
\usepackage{tikz}

\makeatletter

\newlength{\qw@size}
\define@key{qrwifi}{size}{\setlength\qw@size{#1}}
\define@key{qrwifi}{encryption}{\def\qw@enc{#1}}

\setkeys{qrwifi}{
  encryption=WPA,
  size=5cm,
}

\newcommand{\qrwifidata}[2]{%
  WIFI:S:#1;T:\qw@enc;P:#2;;%
}

\newcommand{\qrwifi}[3][]{%
  \begin{tikzpicture}%
  \setkeys{qrwifi}{#1}%
    \node at (0, 0) {%
      \qrcode[level=H,height=\qw@size,padding]{\qrwifidata{#2}{#3}}%
  };
    \node[fill=white,align=center] at (0, 0) {#2\\#3};%
  \end{tikzpicture}%
}
\makeatother

\begin{document}

\qrwifi[encryption=WEP]{SSID}{Key}

\end{document}

enter image description here

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.