Barcodes Galore

So you may need to include a barcode on some reports or forms that you’re generating. Or you might want to include a QR code for your organisation’s newsletter or flyer, to indicate a URL, or an event. Or even one for your personal contact details on your business card.

You can, of course, seek out a barcode generator to get a PNG of your barcode to be included in your document. But did you know about the pst-barcode package that’ll do the job nicely?

Example Barcodes

I mentioned the ean13isbn package in a previous post, but that package is only for generating ISBN barcodes. pst-barcode, on the other hand, can generate many different types of barcodes. Some simple examples below, taken from the pst-barcode package documentation. Remember to \usepackage{pst-barcode}.

\begin{pspicture}(-12pt,-6pt)(3.4,1in)
\psbarcode{9781860742712}{includetext}{ean13}
\end{pspicture}
\begin{pspicture}
\psbarcode{^453^178^121^239}{columns=2 rows=10}{pdf417}
\end{pspicture}
\begin{pspicture}(1in,1in)
\psbarcode{Herbert Voss ^142^164^186}
{rows=48 columns=48 parse}{datamatrix}
\end{pspicture}
\begin{pspicture}(-.5cm,-.7cm)(8,0.3in)
\psbarcode{0123456709498765432101234567891}
{includetext}{onecode}
\end{pspicture}

\begin{pspicture}(2in,2in)
\psbarcode{http://latex-my.blogspot.com/}
{}{qrcode}
\end{pspicture}

Encode Almost Anything with QR Codes

QR barcodes can be used to encode a wide range of data. You can use a camera phones to scan and decode them using a reader application (e.g. ZXing, Kaywa, Qrafter, etc.). (You can also use the ZXing online barcode decoder for testing purposes.) Depending on the decoded data, specific actions will be triggered.

For example, the QR code above encodes the URL of this blog. Scan it with a camera phone, and an Internet browser will open and load this blog automatically.

The following code encodes my contact details as a MeCard. After scanning it, a good barcode reader application will automatically decode the details and prompt you to add my contact details to your address book. (BTW my address and phone number in this barcode is fake.)

% Do not put newlines in the MECARD text
\begin{pspicture}(2in,2in)
\psbarcode{MECARD:N:Lian Tze,Lim;TEL:+60123456789;EMAIL:liantze\string@gmail.com;ADR:,109 Lorong Oak 47,Taman Oak,Butterworth,Pulau Pinang,13000,Malaysia;URL:http://liantze.penguinattack.org/;;}
{eclevel=M height=2 width=2}{qrcode}
\end{pspicture}

It seems MeCard is used more widely than vCard for QR codes on business cards, but if you want a vCard anyway, here’s the code. vCard does allow you to add a photo, which is nice. Note that you need to represent newline characters with ^^J for the code to compile.

% Line breaks occur only after ^^J%
\begin{pspicture}(2in,2in)
\psbarcode{BEGIN:VCARD^^J%
VERSION:3.0^^J%
N:Lian Tze;Lim^^J%
FN:Lim Lian Tze^^J%
PHOTO;VALUE=URL;TYPE=JPG:http://liantze.penguinattack.org/images/LLT-profile.jpg^^J%
TEL:+60123456789^^J%
ADR:;;109 Lorong Oak 47, Taman Oak;Butterworth;Pulau Pinang;13000;Malaysia^^J%
EMAIL:liantze\string@gmail.com^^J%
URL:http://liantze.penguinattack.org/^^J%
END:VCARD}
{eclevel=M height=2 width=2}{qrcode}
\end{pspicture}

You can also add a barcode encoding an iCalendar for an event, e.g. in a newsletter. A good reader app would be able to add the details automatically to your calendar. (Qrafter for the iOS worked for me.)

% Line breaks occur only after ^^J%
\begin{pspicture}(2in,2in)
\psbarcode{BEGIN:VCALENDAR^^J%
VERSION:2.0^^J%
BEGIN:VEVENT^^J%
SUMMARY:25th Pacific Asia Conference on Language, Information and Computation^^J%
ORGANIZER;CN=Assoc Prof Francis Bond:MAILTO:fcbond@ntu.edu.sg^^J%
URL:http://portal.cohass.ntu.edu.sg/PACLIC25/^^J%
LOCATION:Nanyang Technological University, Singapore^^J%
DTSTART;TZID=Asia/Singapore:20111216T090000^^J%
DTEND;TZID=Asia/Singapore:20111218T180000^^J%
END:VEVENT^^J%
END:VCALENDAR}
{eclevel=M height=2 width=2}{qrcode}
\end{pspicture}

What If I’m Generating to PDF directly?

If you’re using xelatex, apparently all the pstricks packages work smoothly and you get a flawless PDF.

If you’re using pdflatex, you can still use pst-barcode (and other pstricks packages) with a little extra bit of work. Here’s what your preamble should include:

\usepackage{pst-barcode}
\usepackage{auto-pdf-pst}

You will also need to install pdfcrop, available from CTAN; install with your TeXLive/MikTeX package manager). This is actually a Perl script that crops away extra whitespace around each page of a PDF file.

You must add the -shell-escape option when running pdflatex for this to work. What happens is that a ps file is generated and converted to a PDF on-the-fly, then cropped and included in the final output PDF file. Understandably, the whole process may take a bit longer than usual.

Hopefully this’ll help anyone who’s looking to batch generate reports or forms that need a barcode or two!

Shameless Barcode Ads

\documentclass{article}

\usepackage{fancyhdr}
\usepackage{soul}
% Font für Code 39
\font\xlix=wlc39 scaled 1200
\newcommand{\barcode}[1]{{\xlix@#1@}}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0.4pt}

\rhead{\begin{tabular}{ll|ll}
\multicolumn{2}{c|}{\barcode{0193870980}} & \multicolumn{2}{c}{\barcode{0361965666}} \\
\textsc{\textbf{\sc{{{\so{ Muhammad}}}}}} & \so{019$\;$3870980} & \textsc{\textbf{\sc{{{\so{Najmi}}}}}} &\so{03$\;$61965666}
\end{tabular}}

\begin{document}

\centering{\ul{Cheap}\so { \LaTeX{} trainer for hire, at risk}}\\
\centering{\texttt{\small{najmi.zabidi@gmail.com}}}
\end{document}

Code was modified from somebody else on the web, I don’t remember the link. I invoke the soul package for space between characters.

Front and Back Covers (and ISBN Bar Codes)

Sorry this post is so late after the last one, it was hard finding time to write between work and a toddler. We’re drawing to a close for this bookdesign series now… just as we’re starting a new year.

I’m not very imaginative, nor am I a graphic designer. So for my book cover, I go for a huge title across the top, a background illustration, and author/editor and publisher information at the bottom. Yes, very boring, but playing it safe — I think with a careful choice of fonts, colours and illustration, the end result wouldn’t look too bad at all. 🙂 I personally find the wallpaper package very handy for the cover page. So it might go something like this:

%% No header nor footer on the cover
\thispagestyle{empty}

%% Cover illustration
\ThisLLCornerWallPaper{1}{grapes-in-my-studio-little-too-much-dust}

%% Bar across the top
\tikz[remember picture,overlay]%
\node[fill=Sienna,text=white,font=\LARGE\bfseries,
text=Cornsilk,minimum width=\paperwidth,
minimum height=5em,anchor=north]%
at (current page.north){Exercises in \LaTeX};

\vspace*{2\baselineskip}

{\bfseries\itshape\color{LightGoldenrod!50!Gold}
\fontsize{36pt}{46pt}\selectfont
The Wonderful Calmness\par
of Still Life Photos\par}

\vspace*{2\baselineskip}

{\LARGE\color{LightGoldenrod}
A small dummy example book by
\scshape{Curutari}\par
}

\tikz[remember picture,overlay]%
\node[fill=Sienna,font=\LARGE\bfseries,
text=Cornsilk,minimum width=\paperwidth,
minimum height=3em,anchor=south]%
at (current page.south) {Malaysian \LaTeX\ User Group};

\begin{center}
\LARGE\bfseries\color{SaddleBrown!30!black}

\end{center}

%% Clear to next odd page
\cleardoublepage

Then comes the back cover. Now if the book is published with an ISBN, you’d want to put a bar code for it. I was wondering where I could get a free bar code generator, when a gut instinct told me to look around CTAN for a LaTeX solution. And sure enough, the ean13isbn package does the trick without any hassle:

%% In preamble
\usepackage[ISBN=978-80-85955-35-4]{ean13isbn}

%% Print the ISBN bar code
%% See the documentation for other sizes e.g. SC0, SC1…
\EANisbn[SC4]

So here’s the code for my backcover:

%% Temporarily enlarge this page to push
%% down the bottom margin
\enlargethispage{3\baselineskip}
\thispagestyle{empty}
\pagecolor[HTML]{0E0407}

\begin{center}
\begin{minipage}{.8\textwidth}
\color{Cornsilk}\Large\bfseries
\lipsum[1]

\begin{center}
\huge\bfseries\sffamily\color{lime}`So Calming.’
\end{center}

\lipsum[2]

\end{minipage}
\end{center}

\vspace*{\stretch{1}}

\begin{center}
\colorbox{white}{\EANisbn[SC4]}

\vspace*{\baselineskip}

\textbf{\textcolor{LightGoldenrod!50!Gold}{Malaysian \LaTeX\ User Group \textbullet\ \texttt{http://latex-my.blogspot.com}}}

\textbf{\textcolor{LightGoldenrod}{Cover Illustration by Dusan Bicanski \textbullet\ \texttt{http://www.public-domain-image.com}}}
\end{center}

I’ve also added a table of contents with a simple \tableofcontents. The ToC heading is printed as a chapter heading, so I created a fancy style for it, without the chapter number and background picture, by modifying our fancy chapter code from earlier.

All in all, here’s what our sample book now looks like (empty pages omitted):



Download links of the LaTeX code producing the above:

Illustrations courtesy of PublicDomainImage.com here, here and here; OpenClipart Project here.

Epilogue

Well it’s been fun writing this series. It sure took me some time to prepare the sample code, search for appropriate illustrations and write up the posts, but I’m glad I documented how I achieved some of the effects in that Grid Computing Cluster book. Many thanks to all the readers and especially those who kept the comments section lively. You sure motivated me to finish writing this series! 😀