Showing posts with label LaTeX. Show all posts
Showing posts with label LaTeX. Show all posts

QR codes in LaTeX

If you are adding QR codes to print media, in order to make them look really sharp, you want the QR codes to be generated in a vector format rather than a bitmap format. It turns out that the pst-barcode package allows you to easily add vectorized QR codes to your LaTeX documents.

Here are some minimal steps to generate a PDF with a QR code in it:

1. Install dependencies:

$ aptitude install texlive-latex-{base,extra}

(This works on Ubuntu 11.04, at least.)

2. Add the following to a .tex file:

\documentclass{article}
\usepackage{pst-barcode}
\usepackage{auto-pst-pdf}
\begin{document}
\begin{pspicture}(1in,1in)
  \psbarcode{PAYLOAD}{eclevel=M width=1.0 height=1.0}{qrcode}
\end{pspicture}
\end{document}

where PAYLOAD gives the data to be encoded. For a business card you might have something like:

MECARD:N:Sung,Phil;TEL:+14085551234;EMAIL:philbert@gmail.com;URL:http://web.psung.name;;

See this page for more MECARD options and for descriptions of the other protocols (URLs, email addresses, etc.) that barcode readers understand.

3. Compile your file as follows:

$ pdflatex --shell-escape yourfile.tex

Some notes:

  • eclevel specifies the level of error correction, and is one of L, M, Q, H (low to high)
  • width and height specify the dimensions of the barcode.
  • pst-barcode knows how to generate barcodes in many other formats; see the documentation for details.

You can also change the color of the barcode by adding something like the following:

\usepackage{color}
[...]
  \psbarcode[linecolor=blue]{PAYLOAD}[...]

Sources: StackExchange, Thomas Widmann, Andrew Brampton (who has a nice template for a business card)

AUCTeX and preview-latex

AUCTeX is a TeX mode for Emacs that adds various conveniences for editing LaTeX (and other macro packages). One of its killer features is inline previews of math and graphics, using preview-latex:

With AUCTeX and preview-latex, you can properly edit your document and equations in their high-level (TeX source) representation, while getting near-instant confirmation that your equations will turn out the way you want. It's pretty much the best of both worlds.

AUCTeX is easy to set up on Ubuntu, although it takes slightly more work than the usual apt-get invocation. Here is a HOWTO for my future reference (and yours). This has been tested on Ubuntu 9.04 with a post-v23-release emacs-snapshot:

Install AUCTeX.

$ sudo aptitude install auctex

Add the following init code to your .emacs file.

(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)

Open a LaTeX file in Emacs and press C-c C-p C-b (M-x preview-buffer) to generate math/graphics previews for the entire buffer. After you update an equation, press C-c C-p C-p (M-x preview-at-point) to refresh the preview.

Further reading: AUCTeX, AUCTeX manual

Making your own page-a-day calendar

Anomalously, today's post is about a DIY physical artifact.

A while back, I made a custom page-a-day calendar as a gift for my girlfriend. Each page tears off and has a picture on it. (Unfortunately, I don't have any photos of the finished product.)

With just a little effort, you can make one of these things and have it look quite professional. You can fill the pages with whatever photos, comics, etc. you want. And, I can virtually guarantee you, your recipient has never had a page-a-day calendar typeset in Computer Modern.

Here are skeletal instructions for making your own. You'll need a printer, ink or toner, most of a ream of paper, some padding compound, a paper cutting device/facility, cardboard, LaTeX, and time.

1. Get source images

Acquire 365 images from Flickr, your photo collection, your favorite CC-licensed webcomic, or whatever strikes your fancy. This post on curl may come in handy. Some notes: (1) Layout is much easier if the images are the same aspect ratio. (2) Consider upsampling the images if needed, e.g. with imagemagick, so you can print at a respectable DPI. Henceforth I'll assume you've named the images imgs/001.jpg, imgs/002.jpg, etc. If this is not the case, simply adjust the code in Steps 2 and 3 accordingly.

2. Use this LaTeX skeleton

Make a new TeX file and fill it with this:

\documentclass[17pt,oneside,final,showtrims]{memoir}
\usepackage{marvosym}

\setstocksize{11in}{8.5in}

\settrims{0in}{0in}

\settrimmedsize{4in}{6in}{*}
\settypeblocksize{3.5in}{1.75in}{*}
\setlrmargins{0.25in}{*}{*}
\setulmargins{0.05in}{*}{*}
\setheadfoot{0.01in}{0.1in}
\setheaderspaces{*}{*}{*}
\setmarginnotes{0.25in}{3.5in}{0in}

\checkandfixthelayout

\pagestyle{empty}

\usepackage[final]{graphicx}

\pagestyle{empty}

\newcommand{\daypage}[6] {
  \marginpar{\includegraphics[height=3.4in]{imgs/#1.jpg}}
  \begin{center}
    \Large{#2} \\
    \HUGE{\textbf{#3}} \\
    \large{#4}

    \vspace{0.4in}
    \small{#5}

    \vspace{0.2in}
    \scriptsize{\textit{#6}}
  \end{center}
  \newpage
}

\begin{document}
  % Cover page
  \marginpar{\includegraphics[height=3.4in]{imgs/cover.png}}
  \newpage

  \include{tex-days}
\end{document}

Salient points:

  • The \daypage command generates a new page. You supply arguments specifying the parameters for each page: the filename of the image to include, the day and date, a line indicating whatever holiday it might be, etc. Play around with the layout, especially if you're using images of different aspect ratios than I did or if you have a calendar stand of a particular size.
  • If you want a cover page, supply a cover.png; otherwise, remove the corresponding lines from the template.

3. Generate the pages

The template above includes tex-days.tex, which might look something like this:

[...]
  \daypage{182}{Sunday}{03}{Jul 2005}{~}{~}
  \daypage{183}{Monday}{04}{Jul 2005}{Independence Day}{~}
  \daypage{184}{Tuesday}{05}{Jul 2005}{~}{~}
  \daypage{185}{Wednesday}{06}{Jul 2005}{~}{~}
  \daypage{186}{Thursday}{07}{Jul 2005}{~}{~}
[...]

You can generate a skeletal version of this, sans holidays, with a quick Python program. I've provided a sample tex-days.tex file for the year 2010.

The first argument on each line indicates the filename, e.g. 182 indicates that 182.jpg should be included. Make sure these match the filenames you are using. The sample file assumes your images are named 1.jpg, 2.jpg, etc. If this is not the case, either create your own version or rename your files.

If you're interested in embellishing the output, the 5th and 6th arguments on each line provide supplementary text to go on each page (#6 is printed in smaller type than #5). You can fill in, by hand or programmatically, whatever notations you want here, e.g., holidays, birthdays, anniversaries, or a countdown to whatever.

Arguments 2, 3, and 4 give the day of week, date, and month/year respectively that are displayed, in case that wasn't clear.

4. Produce and print

Run the file through pdflatex and print it! Make sure the alignment is consistent across pages.

The showtrims argument in the template file makes LaTeX print trim marks on each page. However, you really only need trim marks on the first page. If you're obsessive-compulsive, you could print the first page with trim marks and the rest without to guarantee the marks won't show on the finished product.

5. Trim it

I took the stack of paper, with pieces of thin cardboard above and below it, to my local Kinko's (now Fedex Kinko's, I guess). I asked them to cut the stack along the trim marks (2 cuts, since 2 of the edges already run up again against the page edges). They did this for a fee of just $1/cut.

6. Bind it

Get some padding compound, e.g. Sparco padding compound. (I bought a quart, so I can probably make gift calendars/notepads for years.) Align the cut pages, leaving one piece of cardboard on the bottom, and put the stack in a vise. (In a jam, "under a pile of hardcover books" will do.) Using a paintbrush, paint the top edge of the stack with padding compound. Wait for it to dry. Paint another coat.

If you have random loose paper, padding compound is also handy for recycling it into notepads.

7. Mount it

This is not really needed, but is a nice touch. Find an old stand for a page-a-day calendar. Glue the cardboard backing in.

I hereby place the LaTeX template and LaTeX snippets in this post into the public domain.

Making gorgeous LaTeX plots with Octave

I previously wrote about using the epslatex terminal in Gnuplot to generate beautiful plots for inclusion in a LaTeX document. The secret is that the epslatex terminal produces a combination of (1) EPS vector graphics and (2) TeX instructions to overlay all the text (axis labels, legends, etc.) in whatever font you are using in the rest of your document. So typically you get that super slick looking Computer Modern Roman (cmr) font.

Now, there are some things that are beyond the ken of Gnuplot. So it was a relief when I learned that GNU Octave can produce similarly formatted EPS + TeX graphics. What's nice about using Octave instead of Gnuplot is that, not only can you take advantage of Octave's more advanced (as I understand it) graphics facilities, but you can also bring to bear all the power of a full mathematical/simulation language for preprocessing your data or whatnot. I still usually use Gnuplot, but I break out Octave for making plots when necessary.

All you have to do is produce your plot in Octave as normal (e.g. plot(...)), and use a command like the following to output in EPS + Tex:

print('my_plot.tex', '-dtex');

As an example, here's some minimal code to produce a heatmap with contours and a legend:

x_values = [0.10 : 0.005 : 0.60];
y_values = [0.10 : 0.005 : 0.60];
contourf(x_values, y_values, data); % supply your own data...
axis square;
colorbar;
xlim([0.1 0.6]);
ylim([0.1 0.6]);
print('-dtex', 'my_plot.tex');

Octave really saves the day here. To the best of my knowledge it is difficult or impossible to do this using just plain Gnuplot, especially if you are not plotting over a square area.

Gnuplot plots in LaTeX

I was pleasantly surprised to learn that gnuplot and LaTeX play very well with each other. Gnuplot makes it quite easy to insert professional-looking (LaTeX-worthy) graphs into your documents. To do this, set your gnuplot terminal to epslatex:

set term epslatex
set output "graph1.eps"
plot ...

When gnuplot creates your graph, it will generate not only a graph (in EPS), but also a snippet of TeX (named graph1.tex in this case) that you can include in your document to insert the graph. Then add this to your LaTeX file:

\input{graph1.tex}

The text (axes, labels, title) on the graph are all rendered in TeX, not on the EPS, so it looks very sharp. Typically you will want to wrap the above code in a LaTeX figure as well:

\begin{figure}[tbp]
  \begin{center}
    \input{graph1.tex}
    \caption{Graph caption}
    \label{graph:graph1}
  \end{center}
\end{figure}

You can control the size of the graph with:

set size 1.0, 1.0
in the Gnuplot code.

Update: you can also generate this style plot in GNU Octave. Octave is preferable in some cases because it has a few more plot options/styles than Gnuplot does, and, of course, is a full-fledged programming language.