Featured Post

We've moved to tex.my!

It’s been great writing on Blogger.com, but we’ve decided to move to a new platform, thanks to some valuable help from a great, great friend...

Friday, January 20, 2012

Mail-Merge Batch Generating Documents with datatool Package

If you aren’t aware of the possibility before this, you read the title right, it’s possible to do mail-merging in LaTeX with e.g. the versatile datatool package. This makes LaTeX quite a handy Swiss knife in a pinch.

The example scenario: you need to produce a batch of letters, name cards, or certificates really quickly. Let’s say certificates of appreciation for some long-serving employees. The recipients list has most probably been compiled by someone, as a spreadsheet like this:


Now all spreadsheet applications should be able to export the worksheet as a comma-separated values (CSV) plain text file, so we have namelist.csv with the following contents:

Name,ID,Gender,Years in Service
Abdul Ali,382473856,M,15
Francesca Joestar,461276432,F,10
Chan Ker Mei,463724631,F,5
Hikaru Yagami,154954739,M,10

The datatool package can then load namelist.csv as a simple database, which each line being a record, consisting of fields delimited by commas. By default, the field names are given by the first line of the .csv file. After assigning macros to the field names, we can then use the macros to insert ‘mail merge’ fields into a LaTeX document.

Here’s a quick example, using the wallpaper package and an external image (image courtesy of fromoldbooks.org) for the decorative frame:

% Use scrartcl to allow larger base font size
\documentclass[16pt]{scrartcl}
\usepackage[left=3cm,right=3cm,top=6cm,bottom=5cm]{geometry}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgschola}
\usepackage{fourier-orns}
\usepackage{graphicx}
\usepackage{wallpaper} % For background image frame

\usepackage{datatool}
% Load database 'names' from file 'namelist.csv'
\DTLloaddb{names}{namelist.csv}

\begin{document}
\linespread{2}\selectfont
\pagestyle{empty}
% Set old-border.jpg as background image
\TileWallPaper{\paperwidth}{\paperheight}{old-border}

% For each record (line) in database 'names'
% Assign field values by name to macros
\DTLforeach{names}
{\name=Name, \ID=ID, \gender=Gender, \yr={Years in Service}}{

  {\centering
    {\LARGE\bfseries Certificate of Appreciation}\par
    {\LARGE\decofourleft\quad\decoone\quad\decofourright}\par
    \vskip2em
    We thank\par
    % Insert '\name' field
    {\large\bfseries\MakeUppercase{\name}}\par
    % Insert '\ID' field
    {\large (ID: \ID)}\par
    for having been with\par
    {\large XYZ Company}\par
    % Insert '\yr' field
    {for \emph{\LARGE\yr} glorious years}\par
    \vskip1em
    % Test '\gender' field and insert him/her, his/her
    We commend \DTLifeq{\gender}{M}{him}{her} on
    \DTLifeq{\gender}{M}{his}{her} excellent service.\par
  }
  \vfill
  \hfill
  \begin{minipage}{.4\textwidth}
    Managing Director\par
    \vskip1cm
    \rule{8em}{.5pt}
    \vskip-1em
    (MD's Name)\par
  \end{minipage}

  \pagebreak
}
\end{document}

And voilĂ , the output document has 4 pages, each containing the certificate for a recipient listed in namelist.csv.



Just for fun, here’re the same certifates using ornaments from the adforn font package. For more flamboyant ornaments, see the webomints fonts (installable via getnonfreescripts) or the psvectorian package.



The .tex source codes can be downloaded here.

datatool has many more macros for testing field values and even arithmetics. Other accompanying packages in the bundle, such as datapie, dataplot and databar, even lets you draw charts from .csv files via TikZ/PGF, so do check out the documentation.

Now I’m off to my holidays! Happy Chinese New Year to everyone, and may the Year of the Dragon brings only good tidings for you!

Thursday, January 12, 2012

Use of LaTeX in Industry

A question on TeX.SX about “How is LaTeX used in industry and what are some examples?” has garnered over 6k views since posted yesterday. I had mentioned during my MOSC talk that LaTeX would be very useful in scenarios where some printable content has to be batch-generated or on-the-fly, and now some actual real life examples are given in the answers.

For example, this website allows users to query transportation timetables and returns the results as a pdfTeX-generated PDF:


And here’s another schedule generated by another German transportation website (PDF):


If you read the other answers, you’ll see what and how LaTeX is used in other companies (besides academic publishers). For example, this answer by Peter Flynn:

As a LaTeX consultancy, we have produced classes and styles for many organisations, including companies, government departments, and non-profits. A London auction house uses LaTeX to generate invoices; an electronic systems training company uses LyX and LaTeX to create their course workbooks and white papers; a Dublin printer uses LaTeX for pharmaceutical labelling; a local government organisation uses TeX to generate the Register of Voters; a professional scientific association uses LaTeX for its regular series of technical reports; and of course we use it internally for client reports, newsletters, and invoicing.

Monday, January 9, 2012

Latex on Mobile Smart Devices

I believe most of us got a smartphone which you may use for surfing, taking pictures, emails and playing games. But somehow you might as well use your tablet or perhaps phone (let say, as big as Samsung Galaxy Note) for your productivity tasks.

Since I am more into Android devices (the reason is, most Apple stuffs are pricey, and I could get several Android devices with the price), I managed to find some applications which allow the users to do their Latex activity. 

I found VerbTex[APK file], which uses Verbosus, (which, unfortunately means, you need an internet access to send your Tex file for compilation). 

Also, I found this for the iPad. I am not sure whether it compiles natively or work like VerbTex. But throughout my readings on the page, nothing being mentioned on sending the compilation over the network (perhaps it could be compiled natively?)

How about you, tell us about your "mobile latexing" experience!


Sunday, January 8, 2012

Text-to-Path Conversion with Ghostscript

So you’ve prepared some PDF material with LaTeX, which you’re going to send to the printing company.

If they’ using the PDF as it is, fine and dandy. But if they need to import the PDF into, say, Illustrator for some further processing, you might be in some kind of a fix if the fonts you used (in LaTeX) aren’t easily obtainable as TTF OTF forms.

One solution is to convert all texts into paths before submitting the PDF to the printers. I don’t have Illustrator, so I tried Inkscape instead. Now Inkscape does have a convert-text-to-path function, but for the texts to display properly in Inkscape in the first place, I’d still need the TTF OTF files. Inkscape cannot make use of fonts embedded in the PDF to import the text as paths directly. Not very helpful, then.

Fortunately, suv gave this workaround using Ghostscript:

As a workaround, you can use Ghostscript to convert text in a PDF (or EPS/PS) file to outlines. It requires to convert the PDF to an intermediary PS file, and then back to PDF (thus losing PDF features not supported by PostScript, like transparency).
E.g. use a shell script, containing a command similar to this one:

$ gs -sDEVICE=pswrite -dNOCACHE \
-sOutputFile=- -q -dbatch -dNOPAUSE \
-dQUIET "$1" -c quit | ps2pdf - \
"`echo $1 | cut -f1 -d'.'`"-nofont.pdf


So long as your file does not contain any transparency, the script works just fine, and you can submit the file*-nofont.pdf to your printer. And of course, expect some increase in file size.