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.
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.
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.
The process of including references to links in a print publication can be difficult because the references must be typed in. However, if they are printed as a QR code, then it is easy to scan the code and use it to quickly call up the website.
Listing 4 shows an example for converting a URL into a QR code that in principle, functions exactly like text. The scanner app can figure out that it is dealing with a link from the prefix http://. In this example, I colored the QR code with blue using the command in the square brackets.
Listing 4
URL to QR Code
&&nonumber \psbarcode[linecolor=blue]{http://www.linuxuser.de}{}{qrcode}
Figure 2 shows the result in the second position from the left. Although black QR codes offer the greatest amount of contrast and therefore also readability, scanner apps can typically deal with colored QR codes.
The color does need to be dark enough to stand out against the background color. Light-colored QR codes on a dark background present problems. The scanner app must first invert the colors in order to read the code. Not all apps are up to the task of inverting colors.
WiFi access data can be just as problematic as links when they have to be typed in by hand. However, when access data is represented as a QR code, it becomes easy to quickly create a connection. Listing 5 contains an example for the creation of a corresponding QR code.
Listing 5
Connect to WiFi
&&nonumber \psbarcode{WIFI:T:WPA;S:MyWLAN;P:MyPassword;;}{}{qrcode} \uput{0pt}[90]{90}(2.25,0){\textsf{\tiny{WLAN-Zugangsdaten}}}
Once again, you will see the well-known command for the QR code in the first line – except that here the value represents WiFi access data. The scanner app recognizes this from the WIFI: prefix and then typically generates the WiFi connection after a button push. Next comes the type of encryption, the SSID, and the password.
This example includes a command from the PSTricks package in line 2. The command inside the third set of braces is responsible for making the text WiFi access data also appear in the smallest possible san serif font size. It appears at a distance of zero points (value in the first set of curly braces) from the lower left corner (value in square brackets) at an angle of 90 degrees and rotated by 90 degrees (second set of curly braces). You will find the result in Figure 2 in the position second from the right.
QR codes are increasing found on business cards. This makes it easy to read and save contact information in an address book. You will find an example of the relevant LaTeX code in Listing 6.
Listing 6
QR Business Card
&&nonumber 01 \obeylines 02 \psbarcode{ 03 BEGIN:VCARD^^J 04 VERSION:4.0^^J 05 N:Mustermann;Max;;^^J 06 FN:Max Mustermann^^J 07 ADR;TYPE=home:;;Hauptstr. 42;Musterstadt;;12345;Germany^^J 08 TEL;TYPE=home:+49123456789^^J 09 TEL;TYPE=cell:+49170123456789^^J 10 EMAIL:max.mustermann@example.com^^J 11 URL:http://www.example.com^^J 12 END:VCARD^^J}{}{qrcode} 13 \rput(2.15,2.2){\includegraphics{postkarte.eps}}
This time the psbarcode command contains the value for the contents of a VCard file in the first set of curly braces (lines 2 to 12). Here you must ensure that every entry in the VCard occurs on its own line. For LaTeX to make line breaks correctly, the command has to include obeylines (line 1). Additionally, you will have to specify the ends of the lines with the sequence ^^J .
QR codes have built-in error correction. Even when the scanner is unable to read a larger or smaller part of the QR code, the code can still be decrypted. This property can be used to insert a picture into the QR code. Therefore, you can add things like a photo, a logo or even an icon with a postcard as shown to the far right in the example in Figure 2.
The command from the PSTricks package in line 13 places the contents of the image file given in the set of curly braces at the coordinates specified in the parentheses. For this command to function, you will also need to add the package graphicx [7]>>8 in the preamble. Although the image now covers up a portion of the QR code, the contact information is still readable provided the photo is not too large.
Bar and QR codes are all around us and they provide many opportunities to quickly and conveniently read and process all different kinds of information using a smartphone or a tablet. Pst-barcode lets you create high-quality barcodes and QR codes on your own.
Infos