# LaTeX Package to generate QR codes?

I would like to be able to generate QR barcodes in my LaTeX documents, and I'm having a hard time finding a solution. The use would be a command such as

\qr[1in]{Your text to be QR encoded here}


The optional argument would be the width of the square. I'd be using this to put a barcode on each page of an exam for my students, so that the assignments could be scanned and sorted electronically once they hand them in.

Anyone know of such a package? I don't mind using an external script (e.g., python) to generate a graphic that I could include.

ctan now has the package qrcode, which doesn't need any tricks for pdfLaTeX. The default is a 2cm square, but an option allows you to change the height. (And since you specifically mentioned the width, I'll state the obvious fact that the height and width of the square are equal.) For example, the document

\documentclass{article}
\usepackage{qrcode}
\begin{document}

\qrcode{https://www.ctan.org/tex-archive/macros/latex/contrib/qrcode?lang=en}
1 inch high (and wide):
\qrcode[height=1in]{https://www.ctan.org/tex-archive/macros/latex/contrib/qrcode?lang=en}

\end{document}


produces the output

• FWIW, this package works fine with lualatex as well. – Raphael Jul 3 '18 at 13:13

The pst-barcode package will work as follows:

\documentclass{standalone}
\usepackage{pst-barcode}
%\usepackage{auto-pst-pdf} % uncomment this if used with pdflatex
\begin{document}
\begin{pspicture}(1in,1in)
\psbarcode{test string}{}{qrcode}
\end{pspicture}
\end{document}


The empty {} between the string to encode, {test string}, and the barcode type, {qrcode}, can contain options that are detailed in the package documentation (but which, by definition, are not needed).

The result wil the look like this:

NB: This package relies on pstricks, which of course uses postscript, so you can't use pdflatex to compile unless you uncomment the auto-pst-pdf package.

• This seems to be a wrapper package for the Barcode Writer in Pure Postscript. I've been looking for a way of doing the same thing (QRcode) compatible with pdftex/pdflatex for quite some time. I even have a rather dirty series of macros that accomplishes the rendering end of it, writing a code generator for it is the tough part though. – Giel Aug 10 '10 at 14:39
• I was hoping for something compatible with tikz/pdflatex as well; perhaps it would not be too hard to write a wrapper for it, and throw in a ps2pdf command. If I ever do such a thing I'll post back here. – MattoxBeckman Aug 17 '10 at 14:18
• for pdflatex see How to use PSTricks in pdfLaTeX? – doncherry Aug 25 '11 at 23:42

Not TeX-related, but since you inquired about a Python script...

#!/usr/bin/env python
# Generates QR code from given text using Google charts API.
import urllib2
import sys

ENCODING='utf-8'
IMAGE_WIDTH=200
IMAGE_HEIGHT=200

def make_magic_url(text):
# spaces in the text should be replaced with "+". probably other control symbols need to be handled in special way.
return 'http://chart.apis.google.com/chart?chs=%dx%d&cht=qr&choe=%s&chl=%s' %(IMAGE_WIDTH, IMAGE_HEIGHT, ENCODING, text.replace(" ", "+"))

def makeQR(text, dest):
print make_magic_url(text)
myfig=urllib2.urlopen(make_magic_url(text))
output=open(dest, 'wb')
output.close()

text=sys.argv[1]
dest=sys.argv[2]
makeQR(text, dest)


Save this as fetchqr.py, or some such. Usage is simple:

python fetchqr.py 'http://tex.stackexchange.com/questions/1429/latex-package-to-generate-qr-codes' '/tmp/myfig.png'


And you'd get something like:

This works on Python 2.6 under Windows, but I imagine it shouldn't be a a problem with older Python versions. Not sure about Python 3.x though.

If you're using something like a Makefile to run your project, then you should be able to generate those on the fly as needed -- maybe create a file with stuff to QR-code, then run all the entries through the grinder in one go.

The code is free for the taking.

• Now I saw that this question is 4 months old. Oh well. Better late than never, I guess.. – Martin Tapankov Dec 9 '10 at 17:35
• It would be nice if a vector image is outputted instead of png, although, scaling it will not affect it worse off course. – Peter Smit Dec 9 '10 at 18:38

I made such a package in plainTeX ... it has a bit different interface. Your interface would have for example problems to include braces in the QRcode. The source code is not easy to understand as the math behind it is rather complicated, but I hope the initial comments explain usage well enough. See http://ktiml.mff.cuni.cz/~maj/QRcode.TeX. There could be changes in the file in the future, but current version seems to work well.

This isn't really what you're looking for, but an easy way to generate QR codes is with Google's chart tool: https://developers.google.com/chart/infographics/docs/qr_codes

You can then include the image using the graphicx package.

Helps to put some '\vspace' around it

\vspace{1cm}
% never g g y u
\vspace{1cm}


Full document example below, A4 article format with headers foots toc etc

\documentclass[a4paper,10pt]{article}
\usepackage[utf8x]{inputenc}
\usepackage{graphicx}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{amssymb}
\usepackage{color}
\usepackage{hyperref}
\usepackage{ifthen}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{xcolor,colortbl}
\usepackage{tikz}
\usepackage{bytefield}
\usepackage{qrcode}
\def\tightlist{} % for pandoc
%
\newboolean{european}
\setboolean{european}{true}
% Make the revision and doc number macro's then they are defined in one place
\ifthenelse {\boolean{european}}
{
\newcommand{\rev}{ PA1 }
\newcommand{\etcdoc}{ DOCNUMBER-DES }
}
{
%North American
\newcommand{\rev}{PA1}
\newcommand{\etcdoc}{ DOCNUMBER-DES }
}

\newcommand{\oc}[1]{\ensuremath{{{#1}^{o}{C}}}}
\newcommand{\ok}{\ensuremath{^{o}{K}}}
\newcommand{\degrees}[1]{\ensuremath{{#1}^{\circ}}}
\newcommand{\dg}[1]{\ensuremath{{#1}^{\circ}}}
\newcommand{\dactw}{\ensuremath{{\mathcal{DAC}}_{12}}}
\newcommand{\dacten}{\ensuremath{{\mathcal{DAC}}_{10}}}
\newcommand{\ohms}[1]{\ensuremath{#1\Omega}}
\newcommand{\tick}{{\checkmark}}

\title{ Some QR codes  \\ \etcdoc revision \rev}
\author{A.N. Other}
\begin{document}
\pagenumbering{roman}
\pagestyle{fancy}
\maketitle
\rfoot{\thepage}
\cfoot{}
\lfoot{\textbf{\small left footer \etcdoc Revision: \rev}}

\maketitle
\begin{abstract}
\ifthenelse {\boolean{european}}
{
European abstract
}
{
%% North American
North American abstract
}
\end{abstract}

\clearpage
\tableofcontents
\listoffigures
%\clearpage
\listoftables

\clearpage
\pagenumbering{arabic}
\rfoot{ \textbf{ Page $\;$ \thepage $\;$  of $\;$ \pageref{LastPage}} }
\lfoot{  \textbf{\etcdoc Revision: \rev} }

\section{Introduction}

Blah blah blah blah blah blah blah blah blah blah blah blah blah blah

\section{Software Overview}

% the best book on programming ever!
\nocite{f77}

In the C' language~\cite{DBLP:books/ph/KernighanR88} this can be very efficiently achieved by using some shifts and adds thus:
\begin{verbatim}
address = (v << 1) + ((v >> 2) << 3);   .
\end{verbatim}
To find the address of a 16-bit complement of a variable simply add 8.
\clearpage
\vspace{1cm}
% never g g y u

\vspace{1cm}
Last Page.
typeset in {\Huge \LaTeX} \today.
\end{document}


And here is a bib file

%
%
% $Id: mybib.bib,v 1.5 2008/12/18 17:05:23 robin Exp$
%
%

@book{Aho:1987:APL:29361,
author = {Aho, Alfred V. and Kernighan, Brian W. and Weinberger, Peter J.},
title = {The AWK programming language},
year = {1987},
isbn = {0-201-07981-X},
publisher = {Addison-Wesley Longman Publishing Co., Inc.},
}

@book{DBLP:books/ph/KernighanR88,
author    = {Brian W. Kernighan and
Dennis Ritchie},
title     = {The C Programming Language, Second Edition},
publisher = {Prentice-Hall},
year      = {1988},
isbn      = {0-13-110370-9},
bibsource = {DBLP, http://dblp.uni-trier.de}
}

@BOOK{f77,
AUTHOR =       "A.~Balfour D.H.~Marwick",
TITLE =        "Programming in Standard Fortran 77 ISBN 0-435-77486-7",
PUBLISHER =    "Heinemann Educational Books",
YEAR =         "1979"
};

@Book{aoe,
title = {The Art of Electronics},
publisher = {Cambridge},
year = {1989},
author = {Paul Horowitz, Winfield Hill},
OPTkey = {},
OPTvolume = {},
OPTnumber = {},
OPTseries = {},
OPTedition = {2nd},
OPTmonth = {},
OPTnote = {},
OPTannote = {},
OPTurl = {},
OPTdoi = {},
OPTissn = {ISBN 0-521-37095-7},
OPTlocalfile = {},
OPTabstracts = {},
}
`
• Welcome to TeX.SX! Could you please add a full working answer, for testing reasons? – Bobyandbob Oct 2 '18 at 9:12
• What is the difference to Teepeemm 's answer? – Bobyandbob Oct 2 '18 at 9:38
• Not alot, just nice spacing around it. Plus I have archived my template here so I can fetch it from other places on the internet. Also, have you followed the QR code on a phone say! – Robin Oct 2 '18 at 9:58
• You are welcome. - Your example is a little bit over loaded. If you like to add a example with bibliography-i don't know why it should be necessary here- be care of (MWEB), so everyone should compile it, without modifications. – Bobyandbob Oct 2 '18 at 11:22
• I can add a mybib.bib (cut down of course) ... should I? – Robin Oct 2 '18 at 11:23