2

I am trying to read a list of URLs from a file and create QR codes for them. If they contain a # like in the example, I get an

Illegal parameter number in definition of \qr@texttoencode

error. Is it possible to make this work without having to edit the input file and escape the special characters there?

\documentclass{article}
\usepackage{qrcode}

\begin{filecontents}{urls.txt} http://example.org/foo#bar \end{filecontents}

\def\loadurls#1{% \newread\file \openin\file=#1 \begingroup\endlinechar=-1 \loop\unless\ifeof\file \read\file to \fileline \qrcode{\fileline} \repeat \endgroup \closein\file }

\begin{document} \loadurls{urls.txt} \end{document}

2 Answers2

3

An alternative solution using csvsimple. Look at the section 3.6 Special Characters of the csvsimple documentation for other options to escape the special characters. Here I used the respect sharp=true to interpret # as a normal character:

\documentclass{article}
\usepackage{csvsimple}
\usepackage{qrcode}

\begin{filecontents}[overwrite,noheader]{urls.txt} http://example.org/foo#bar http://www.ctan.org \end{filecontents}

\begin{document}

\csvreader[nohead,respect sharp=true]{urls.txt}{}{\qrcode{\csvcoli}\quad}

\end{document}

enter image description here

Ivan
  • 4,368
1

You should use \readstring rather than \read. By the way, \newread should go outside the definition of \loadurls (and you should use a different name than \file).

Here's an expl3 version:

\begin{filecontents*}[overwrite]{\jobname.dat}
http://example.org/foo#bar
https://tex.stackexchange.com/questions/594413/special-characters-in-qr-code#594426
\end{filecontents*}

\documentclass{article} \usepackage{qrcode}

\ExplSyntaxOn

\NewDocumentCommand{\loadurls}{m} { \patheticpat_loadurls:n { #1 } }

\ior_new:N \g_patheticpat_loadurls_ior

\cs_new_protected:Nn \patheticpat_loadurls:n { \ior_open:Nn \g_patheticpat_loadurls_ior { #1 } \ior_str_map_inline:Nn \g_patheticpat_loadurls_ior { \qrcode{##1}\par\medskip } }

\ExplSyntaxOff

\begin{document}

\loadurls{\jobname.dat}

\end{document}

enter image description here

egreg
  • 1,121,712