83

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.

4

6 Answers 6

70

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}

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

\end{document}

produces the output

qrcode examples

2
  • 3
    FWIW, this package works fine with lualatex as well.
    – Raphael
    Jul 3, 2018 at 13:13
  • Anyone knows if there is such a module for ConTeXt? Apr 25, 2021 at 20:23
47

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:

enter image description here

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.

3
  • 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, 2010 at 14:39
  • 4
    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. Aug 17, 2010 at 14:18
  • 3
    for pdflatex see How to use PSTricks in pdfLaTeX?
    – doncherry
    Aug 25, 2011 at 23:42
13

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

# change those to your heart's content. See http://code.google.com/apis/chart/docs/gallery/qr_codes.html for more info
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.write(myfig.read())
    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:

http://tex.stackexchange.com/questions/1429/latex-package-to-generate-qr-codes

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.

2
  • Now I saw that this question is 4 months old. Oh well. Better late than never, I guess.. Dec 9, 2010 at 17:35
  • 1
    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, 2010 at 18:38
9

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.

2
3

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.

0
0

That works really nicely, well done CTAN

Helps to put some '\vspace' around it

\vspace{1cm}
% never g g y u
\quad
\qrcode{https://www.youtube.com/watch?v=dQw4w9WgXcQ}
\qquad
\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{\adctw}{\ensuremath{{\mathcal{ADC}}_{12}}}
\newcommand{\adcten}{\ensuremath{{\mathcal{ADC}}_{10}}}
\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
\lhead{}
\chead{\textbf{Central header design notes.}}
\rhead{}
\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}

\subsection{Additional Software Tools}

In the `C' language~\cite{DBLP:books/ph/KernighanR88} this can be very efficiently achieved by using some shifts and adds thus:
  \begin{verbatim} 
     int address,v=var_number;
     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
\quad
\qrcode{https://www.youtube.com/watch?v=dQw4w9WgXcQ}
\qquad

\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.},
 address = {Boston, MA, USA},
} 

@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 = {},
 OPTaddress = {},
 OPTedition = {2nd},
 OPTmonth = {},
 OPTnote = {},
 OPTannote = {},
 OPTurl = {},
 OPTdoi = {},
 OPTissn = {ISBN 0-521-37095-7},
 OPTlocalfile = {},
 OPTabstracts = {},
}
6
  • 1
    Welcome to TeX.SX! Could you please add a full working answer, for testing reasons?
    – Bobyandbob
    Oct 2, 2018 at 9:12
  • 1
    What is the difference to Teepeemm 's answer?
    – Bobyandbob
    Oct 2, 2018 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, 2018 at 9:58
  • 1
    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, 2018 at 11:22
  • I can add a mybib.bib (cut down of course) ... should I?
    – Robin
    Oct 2, 2018 at 11:23

You must log in to answer this question.

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