3

I have a .csv file with some URLs.

  • First I want to store each URL in a variable.
  • Second I want to print the variables in QR codes.

What's the easiest way to do this?

MWE

\documentclass{article}

\usepackage{qrcode}

\usepackage{blindtext}

\begin{filecontents*}{url.csv}
url1
url2
url3
url4
url5
\end{filecontents*}

\begin{document}

\section{One}
\blindtext[1]

\qrcode[height=1in]{url1} % there should be a variable

\section{Two}
\blindtext[1]

\qrcode[height=1in]{url2} % there should be a variable

\section{Three}
\blindtext[1]

\qrcode[height=1in]{url3} % there should be a variable

\section{Four}
\blindtext[1]

\qrcode[height=1in]{url4} % there should be a variable

\section{Five}
\blindtext[1]

\qrcode[height=1in]{url5} % there should be a variable

\end{document}
0

2 Answers 2

2

It's not clear what “variable” means. Here I define a command \urlvar with an argument that points to the URL in the corresponding line.

\begin{filecontents*}{\jobname.dat}
https://tex.stackexchange.com
https://tex.stackexchange.com/questions/420647/
https://tex.stackexchange.com/questions/420647/csv-file-and-qr-codes
https://tex.stackexchange.com/users/101831/sr-schneider
\end{filecontents*}

\documentclass{article}
\usepackage{xparse}
\usepackage{qrcode}
\usepackage{url}

\ExplSyntaxOn
\ior_new:N \g_schneider_urlfile_ior
\seq_new:N \l_schneider_urlfile_seq

\NewDocumentCommand{\readurls}{m}
 {% #1 is the name of the file
  \seq_clear:N \l_schneider_urlfile_seq
  \ior_open:Nn \g_schneider_urlfile_ior { #1 }
  \ior_str_map_inline:Nn \g_schneider_urlfile_ior
   {
    \seq_put_right:Nn \l_schneider_urlfile_seq { ##1 }
   }
  \ior_close:N \g_schneider_urlfile_ior
 }
\NewExpandableDocumentCommand{\urlvar}{m}
 {
  \seq_item:Nn \l_schneider_urlfile_seq { #1 }
 }
\ExplSyntaxOff

\begin{document}

\readurls{\jobname.dat}

\section{One}
\texttt{\urlvar{1}}

\qrcode[height=1in]{\urlvar{1}} % there should be a variable

\section{Two}
\texttt{\urlvar{2}}

\qrcode[height=1in]{\urlvar{2}} % there should be a variable

\section{Three}
\texttt{\urlvar{3}}

\qrcode[height=1in]{\urlvar{3}} % there should be a variable

\section{Four}
\texttt{\urlvar{4}}

\qrcode[height=1in]{\urlvar{4}} % there should be a variable

\end{document}

enter image description here

1

You need not to use special macro for csv reading when you have one URL per line in your working file. You can simply use \read TeX primitive:

{\endlinechar=-1 \global\read\urlfile to\urlvar}

If \urlvar is empty then you are at the end of the file. It means that you can process all lines in the loop:

\loop
   {\endlinechar=-1 \global\read\urlfile to\urlvar}
   \if^\urlvar^\else
      ... do something with \urlvar
      \repeat

Full working example (which reads the file url.csv) follows:

\newread\urlfile
\openin\urlfile=url.csv
\def\iterate{\body \expandafter \iterate \fi} % plain TeX correction of \loop

\input qrcode % qrcode.tex for plain TeX

\loop
   {\endlinechar=-1 \global\read\urlfile to\urlvar}
   \if^\urlvar^\else
      \noindent \qrcode{\urlvar}\qquad {\tt \urlvar}
      \bigskip
      \repeat

\bye

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .