Generating barcodes and QR codes with LaTeX

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Tom Grundy, 123RF.com

Tom Grundy, 123RF.com

Easy Codes

The pst-barcode package in LaTeX lets you generate a wide variety of professional-quality barcodes and QR codes for many different purposes.

Barcodes and 2D codes are a daily part of life. For a long time now, they have been responsible for shorter wait times in checkout lanes, because they make it possible to scan product prices instead of manually entering a price into a cash register. Ordering a book online is also much easier because the purchaser can simply scan in a barcode containing the ISBN. Such 2D codes have also begun to replace stamps and appear on travel tickets and movie tickets, for example. Information about tourist attractions can now be read into a smartphone or tablet as a QR code. WiFi connections also become easier when the access code comes in the form of a QR code. Professional contact information can be shared via a QR code.

The pst-barcode package in LaTeX [1] is a powerful tool that lets you easily and conveniently generate many different types of codes. Listing 1 shows a basic example of a LaTeX file with pst-barcode.

Listing 1

Pst-barcode

&&nonumber
01 \documentclass{article}
02 \usepackage{pst-barcode}
03 \begin{document}
04 \begin{pspicture}(3.5,3)
05 \psbarcode{4196067008502}{includetext}{ean13}
06 \end{pspicture}
07 \end{document}

Once you have indicated the document class (line 1), you next enter the package in the preamble of the LaTeX document (line 2). Pst-barcode uses PSTricks [2]. This loads automatically, making all of its features available.

You should use the pspicture environment in the main part of the LaTeX file (lines 4 to 6 ). The two numbers inside the parentheses in line 4 are the coordinates for the upper right corner of the barcode or the 2D code. PST barcodes provides a macro (line 5), with which you can generate any number of bar and 2D codes.

The value that the bar or 2D code should represent sits inside the first set of curly braces. The second set of curly braces includes additional options. In the example here, you can see the specification that it should also be readable. In the last set of curly braces, you should determine the type of code to be generated.

Depending on the bar or 2D code you want to generate, you may need to modify Listing 1 by only changing the coordinates for the right upper corner (line 4) and the values for the code (line 5). Note that in the interest of saving space, the listings shown here are limited to portions of LaTeX Code pertinent to the example. The full listings can be downloaded from the Ubuntu User website [3].

Because PostScript is being used, it is not possible to simply have PDFLaTeX generate a PDF document from the LaTeX file. Instead, you should use Xe(La)Tex [4], which will automatically recognize the PostScript code and process it accordingly.

Alternatively, you could generate a DVI file from the LaTeX file via the command pslatex <file> . Then, you would convert the DVI file into a PostScript file via dvips <file> . Depending on how you would like to process the barcode or 2D code, you can generate a PDF document via ps2pdf <file> .

For an image file in a format such as SVG or PNG, you should use ImageMagick [5] and issue the following command:

$ convert Postscript-file graphics-file

If you would rather use PDFLaTeX, you would also need to load the auto-pst-pdf package [6] in the LaTeX document and then generate the desired PDF file via:

$ pdflatex --shell-escape file

PST barcode offers many possibilities for generating barcodes. In this article, we will focus on two types of codes. These are ISBN barcodes and QR codes because these are typically used most frequently.

Commercial Practice

The Universal Product Code (UPC) is commonly used in the United States to uniquely identify a commercially available product. The UPC appears as a barcode on product packaging so that it can be scanned by a cashier. The UPC may also allow you to retrieve product information online, for example, via upcdatabase.org.

In Europe, products are identified with a European Article Number (EAN). This number is likewise used at the point of payment and also for finding product information online, for example, via the OpenEANDB [7].

The International Standard Book Number (ISBN) is a special form of the EAN and the UPC numbers. If the first three digits of a code consist of 978 or 979, then an ISBN is involved.Next come digits for the registration group element designating the language sharing group for the book, followed by the publication element, the title, and a check digit. In addition to the ISBN barcode, you will often see a code that designates the suggested retail price for the book.

Listing 2 contains the LaTeX code that generates a barcode for an ISBN (line 2), as well as an additional barcode with the book price (line 5). In the command on line 2, you will see the ISBN itself in the first set of curly braces. You can leave out the 978 at the beginning and also the check digit at the end. LaTeX adds these numbers automatically.

Listing 2

Barcodes for Books

&&nonumber
01 \begin{pspicture}(3.5,3)
02 \psbarcode{3-88309-455}{includetext}{isbn}
03 \end{pspicture}
04 \begin{pspicture}(1.5,3)
05 \psbarcode{00700}{}{ean5}
06 \end{pspicture}

The command in the second set of curly braces specifies that the ISBN is to also be rendered in digits. LaTeX concludes from the third set of curly braces that it should generate an ISBN.

The suggested retail price for the book is found in line 5 as a five-digit number with leading zeros in the first set of curly braces. Likewise, you will find the type of code that LaTeX should generate in the last set of curly braces. Figure 1 shows the result that gets generated.

Figure 1: Barcodes with ISBN and book price can be conveniently generated using LaTeX.

Convenience Squared

QR codes are found all over the world and are used for many different purposes. The simplest example is when you receive a text that gets displayed on your smartphone or tablet. These types of codes are also used for informational content about tourist attractions. In LinuxUser magazine, for example, a QR code at the end of each article contains the links for the article that otherwise would take considerable space to print due to the modern custom of using long English-language URLs.

Listing 3 contains an example with a short text. The command psbarcode referred to above is issued. Just as with barcodes, the first set of curly braces contains the value to be coded, which in this case would be text.

Listing 3

Generating QR Codes

&&nonumber
\psbarcode[rotate=45]{Hello World!}{}{qrcode}

The last set of curly braces specifies the type of code to be generated. I also have added an instruction in square brackets to rotate the QR code by 45 degrees. You can see the results in Figure 2 at the far left.

Figure 2: Here are QR codes with a variety of content. From left to right, you see text, a URL, WiFi access data, and contact information. LaTeX makes it quick and easy to generate these codes.

Buy this article as PDF

Express-Checkout as PDF

Pages: 3

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Online LaTeX Editors

    You can edit your LaTeX documents with online editors that run across platforms and are device independent. Documents get saved online, thus giving you and other members of a work group easy access from anywhere.

  • Fine-tuning LaTeX documents

    Proofreading, wordsmithing, and finalizing the layout are all steps in the final editing phase of a document. LaTeX used with additional software can help you master these steps.

  • Creating vector graphics with LaTeX and TikZ

    Using LaTeX and the TikZ package, you can create high-quality vector graphics to improve your documents by making them more visually appealing.

  • Minimizing load time for web pages

    Web page loading time relies on a complex interplay among the web server, the web page, and the web browser. Learning a few tricks can help speed up load times for the pages you create.

  • Software for scientific texts

    Scientific documents are where open source tools show their greatest strengths. To save yourself a lot of trouble, however, you need to know how to combine formulas and data into your work. Linux provides the appropriate tools.