I would like to modify the data (black square or white square) in a QR code so that to display any graphic feature of my own choice in the code at the expense of sacrificing a certain amount of its redundancy. There are related questions on this site like Generate QR Code with image inside or How to add text into a QR-Code? of the same flavour. If you wish, I am looking for a way to generate a corrupted QR code in TeX.

Is there a QR code latex package which readily has a high-level command to do this, like \setpixel[xcoord=5,ycoord=12,"black"]?

I add as a clarification, that I am currently experimenting with the recent qrcode package with the MWE as shown in this thread pgf-Tikz QR code generator, whose very short documentation does not hint a feature like this. But my question is not restricted to this particular package.


Well, apparently, more clarification is needed. So, as described above, I am using the following explicit MWE, called test.tex:

\qrcode[]{Dummy code}

The code above compiles fine with pdflatex, and inspection of the compilation output reveals, that pdflatex puts the 0-1 representation of the QR code into the auxiliary file test.aux, as shown below:

\ifx\qr@savematrix\@undefined\def\qr@savematrix{\begingroup\let\do\@makeother\dospecials\catcode`\{=1\catcode`\}=2\relax \qr@savematrix@int}\def\qr@savematrix@int#1#2#3#4{\endgroup}\fi
\qr@savematrix{Dummy code}{1}{3}{111111100010101111111100000101111101000001101110100111101011101101110101000001011101101110101010001011101100000100110001000001111111101010101111111000000001110000000000010111101000111011010111111011010010001110000010100010110000111001110010000110100100001100101000110111001000000001010001011011111111100101010011100100000101001011011100101110101100000101000101110101011110000000101110100000001110111100000101110011101111111111100011010101000}

So this is the source of the QR code, which I can freely modify anyway I wish, and this is what being transformed to the actual picture. At this point my question boils down to the following: How can I cheat pdflatex to use the 0-1 string of my choice, and not the one which is automatically generated by the qrcode package and pdflatex? To specify the question, I am using TeXnicCenter under Windows, and I am clicking a button to have the latex source compiled (no fancy command-line stuff).

One naive way to do this is to carefully place white or black squares on the top of the QR code image, but this is really not something I am looking for.

Until now I was generating my QR codes with any of the readily available free online programs. Then, as hinted by this not-too-in-depth Explanation of QR codes (YouTube), based on the generated image I manually recoloured certain cells in a spreadsheet (say Excel), and then got an image by saving the contents of the screen. There should be clearly more efficient (and reliable!) ways to do this.

Related: LaTeX Package to generate QR codes?


This is an attempt to show that the spirit of what I want can be done by knowledge of basic latex programming. Here is what I came up with:


\qrcode[]{Dummy code}%Original code

%Corrupted code
%\draw[gray,very thin] (0,0) rectangle (21,-21);
    \foreach \x [count=\xi from 0] in {1,1,1,1,1,1,1,0,0,0,1,0,1,0,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,1,0,1,1,1,0,1,0,0,1,1,1,1,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,0,0,0,0,0,1,0,1,1,1,0,1,1,0,1,1,1,0,1,0,1,0,1,0,0,0,1,0,1,1,1,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,0,1,0,0,0,1,1,1,0,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,0,0,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,0,0,0,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1,1,0,0,0,1,0,1,0,1,0,1,0,1,0,1}{%
        \fill[black] (\intcalcMod{\xi}{21},-\intcalcDiv{\xi-\intcalcMod{\xi}{21}}{21}) rectangle (\intcalcMod{\xi}{21}+1,-\intcalcDiv{\xi-\intcalcMod{\xi}{21}}{21}-1);}{};}


What happens here is that the \qrcode[]{Dummy code} call generates the actual QR code in binary format and puts it into the auxiliary file. I copy-pasted (and put commas between the digits) that into my source file right after the \foreach statement and corrupted it at my own desire to show a checker board in its lower right part. Then I redraw the code.

To polish this sloppy attempt to perfection, one needs to:

  1. Generate the QR code by the qrcode package, but not display it; and
  2. Access the binary code from the auxiliary file, and corrupt its elements (via the sought-after \setpixel command which does nothing but flips some digits d to 1-d, as I did it manually); and

then render the corrupted code, perhaps the same way, as above. I feel like I am reinventing the wheel, and I do that in a not particularly elegant way.


  • Is this a satisfactory answer to your question? If not, it shouldn't be posted as an answer. Apart from anything else, people looking to answer questions are less likely to look at questions with answers, especially those answered by the OP (since you kind of assume the OP knows what the OP wants, whereas the rest of us might get it wrong). For whatever it is worth, I got it to write user-specified binary formulations to the .aux, together with the preliminary definition setting that up. And I did end up with a document where \qrcode no longer displayed anything, if that's any use ;). – cfr May 1 '16 at 22:33
  • Perhaps you could try breaking what you want down in stages and, for each stage, either solving it yourself or posting a question asking for help to overcome the specific obstacle. I think that you are probably asking for far too much in one question. – cfr May 1 '16 at 22:36
  • Since this prize winning answer, the question's relevance to TeX is at least no longer debated. I don't think breaking up this question into further smaller parts would serve it well, as people might have radically different ideas how to approach this problem than what I can suggest them. – Matsmath May 2 '16 at 5:07
  • Not sure what you meant to link to, but that doesn't link to an answer. In any case, my point was that some question in its original form had no apparent relation to TeX. After that you edited it. That typically changes things. As for splitting this one up, it's your question. It's up to you. Another option you might consider is to offer a bounty on it. Sometimes people are prepared to invest more time in answering those and the bounty draws attention to the issue. But, again, it's your question. – cfr May 2 '16 at 13:07

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.