pgf-Tikz QR code generator

Well this is cheating, but I could not resist:

The result

Result

The document

\documentclass{article}

\usepackage{qrcode}
% Defines \qrcode command to be used inside a tikzpicture, with two arguments
%  #1  Size of the square (in tikz relative units) [optional, defaults to 10 units]
%  #2  String for the QRCode
% It draws the qrcode using styles "pixel on" and "pixel off" and defines
% a node named (qrcode) of the given size, so you can refer to its anchors for
% further drawing or labelling.

\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[node distance=2mm]
  \qrcode[4]{This is a test};
  \node[below=of qrcode] {This is a test};
\end{tikzpicture}
%
\hfill
%
\begin{tikzpicture}[node distance=2mm]
\qrcode[6]{https://tex.stackexchange.com/a/102569/12571}
\node[below=of qrcode] {This is not an answer};
\end{tikzpicture}
\end{document}

The cheat

Contents of qrcode.sty

\RequirePackage{tikz}
\usetikzlibrary{fit}
\directlua{dofile("luaqrcode.lua")}  % <--- Oooh!
\newcommand{\qrcode}[2][10]{%
\begin{scope}[qrcode]
\directlua{tikzQRCode("#2",#1)}%
\end{scope}
}
\tikzset{
  qrcode/.style = {line width = 2sp},
  pixel on/.style = {black},
  pixel off/.style = {white},
  pixel err/.style = {red}
}

Contents of luaqrcode.lua

qrencode = dofile("qrencode.lua")    --  <--- Ooooh! You cheater!

local function matrix_to_tikz( tab , size)
  if (size == nil) then size=10 end
  pixel_width = size / #tab
  pixel_cmd = string.format("\\filldraw[%%s] (%%f, %%f) +(-%f, %f) rectangle +(%f, -%f);",
          pixel_width/2, pixel_width/2, pixel_width/2, pixel_width/2)
  str_tab = {}
  for y=1,#tab do
    row = {}
    for x=1,#tab do
      if tab[x][y] > 0 then
          style = "pixel on"
      elseif tab[x][y] < 0 then
          style = "pixel off"
      else
          style = "pixel err"
      end
      if style=="pixel off" then
          row[x] = ""
      else
          row[x] = string.format(pixel_cmd, style, x*pixel_width, -y*pixel_width)
      end
    end
    str_tab[y] = table.concat(row, "\n")
  end
  local extra = {}
  extra[1] = string.format("\\coordinate (aux1) at (%f,-%f);", pixel_width/2, size+pixel_width/2)
  extra[2] = string.format("\\coordinate (aux2) at (%f, %f);", size+pixel_width/2, -pixel_width/2)
  extra[3] = "\\node[inner sep=0pt, fit=(aux1) (aux2)] (qrcode) {};"
  str_tab[#tab+1] =  table.concat(extra, "\n")
  return table.concat(str_tab,"\n")
end

function tikzQRCode(txt, size)
    local ok, tab_or_message = qrencode.qrcode(txt)
    if not ok then
        tex.print(tab_or_message)
    else
        tex.print(matrix_to_tikz(tab_or_message, size))
    end
end

The file qrencode.lua

It can be downloaded from here:

luaqrencode

Update: A note on performance

The compilation of the main document took more time than I expected (a few seconds) but the bottleneck is not Lua, but tikz. The lua code generates a lot of small rectangles to be filled, and tikz requires time to draw all of them (I replaced the tikz-generation code in lua for simple "print" statements to the console, and it compiled almost instantaneously).

Trying to reduce the compile-time, I modified the lua code so that only the black pixels are actually drawn, thus reducing the compile time to the half (in my first implementation Lua generated both the black and the white pixels).

The compiling time can be dramatically improved if, instead of using tikz to draw the pixels, tex \rule primitive is used, as in this answer which basically does the same thing than my code, but without tikz (thanks to michal.h21 for pointing me to his answer).

Inspection of library luaqrcode shows that the QR algorithmm is rather complex. Although a lot of values are precomputed and stored in tables, which would simplify a pure tex implementation, I'm concerned for the performance of that implementation.


There is a new package on CTAN:

Anders O.F. Hendrickson submitted the

            qrcode

package.

Version number: 1.0 License type: lppl1.3

Summary description: Generate QR codes in LaTeX.

Announcement text:

This package generates Quick Response (QR) codes in LaTeX, without the need for pstricks or any other graphics package.


This package is located at
http://mirror.ctan.org/macros/latex/contrib/qrcode

More information is at http://www.ctan.org/pkg/qrcode

I installed the package via TeXLive 2014 and the result looks correct with pdfLaTeX, xeLaTeX and luaLaTeX.

Code example:

\documentclass{article}
\usepackage[]{qrcode}
\begin{document}
\qrcode[]{Dummy code}
\end{document}

Result:

QRCODE example