Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am looking for a Tikz QR code generator. I am aware of pst-barcode as well as several "local" programs, a luatex solution and web services to do this. Nevertheless, if at all possible, I would much prefer a Tikz replacement working with pdflatex/xelatex as well, since this is the most common baseline at the moment. If this doesn't yet exist, I would be willing to award a bounty on the creation of one. However I will wait until someone expresses interest in doing so, since I am not sure how long this might take.

This link contains a couple of links to the spec and possibly helpful implementations.

Bounty promise

I promise that I, tohecz, will award a bounty of 300-500 rep points on this question, as soon as it gets a reasonably good answer. The value of the bounty depends on the quality of the answer. I decided not to provide the bounty now because 7 days might not be enough to solve the problem. The answer should be such that it is working with pdfLaTeX, it is in TikZ and is working well. I hope that my reputation and activity on this site is enough to give credit to this promise.

Yours, tohecz

share|improve this question
    
related –  leo Jan 6 '13 at 23:30
    
TikZ won’t be the problem here. Only the translation of text to its binary representation (which is then visualized in two dimensions) is probably a little trickier, but solely a TeX programming problem. –  Qrrbrbirlbel Jan 6 '13 at 23:39
6  
... and yet another package will see the light of screens all across the LaTeX world! :) –  Count Zero Jan 7 '13 at 11:35
12  
I was actually thinking about a QR package written solely in LaTeX (and displayed with TikZ), but minded the effort of the coding implementation so far. Maybe I find time after all for it. –  Martin Scharrer Jan 7 '13 at 16:32
    
A range of language implementations is provided here. –  Sean Allred Mar 1 '13 at 20:12

2 Answers 2

up vote 9 down vote accepted

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

share|improve this answer

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]{http://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.

share|improve this answer
1  
The last link is http://speedata.github.com/luaqrcode/. –  Paul Gaborit Mar 15 '13 at 6:28
3  
@PaulGaborit Sorry for the little prank, but I had made the image clickable :-) –  JLDiaz Mar 15 '13 at 8:18
1  
@JLDiaz for solution using \vrule see tex.stackexchange.com/a/88610/2891 –  michal.h21 Mar 15 '13 at 8:37
    
@michal.h21 Thanks! Obviously I was not aware of your implementation, otherwise I woulnd't do mine... This is probably the "luatex solution" that the OP mentions in his question. –  JLDiaz Mar 15 '13 at 8:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.