mercredi 30 septembre 2009

Integrating a LaTeX/Beamer build system under Windows shell



I'm a LaTeX user for a couple of years now, but I only switched to beamer presentation this summer. I always was quite impressed in conferences or other events by the presentations that were made with beamer. At that time, I was happily using MS PowerPoint, but over the time, I felt more and more unsatisfied with mine.
"Beamer guys" presentations looked always cool, the slides were clean, clear and classy. Woah ! I had to switch...

So I recently gave it a try, and I must say at present, I don't think I will ever come back to MS.

I intend to prepare some kind of a tutorial on Beamer, as there's not that much out there specifically on beamer, and I already know some useful tricks. But today I will focus on another point, not directly LaTeX-related, but about a usability trick for building different pdf files, while needing an identical presentation standard for all the documents, and needing each document in different modes. This happened to me when preparing courses. I do a lot of teaching, and I always give a paper handout to the students, so they can focus on what I'm saying, rather than handwriting notes.

With LaTeX, when you are building several documents that must share the same presentation, keeping a consistent header can quickly become tedious. You often need to tweak it, add a package, change a package option, ... If you are working on several documents at once, it's easy (understand: unavoidable) to end up with differents headers in your different documents. And you get slight (or heavy...) aspects changes in the final documents. Moreover, you are likely to forget which header is the correct one.

One way to handle to this is to concentrate on your part of the document, that is, what starts after \begin{document} and ends before \end{document}. All the rest is about formating information, and should be common to all documents.
Just let an automated script do the painful job (adding the header, and calling the compiler). And as I like fooling around with windows shell, I present here an example of what can be done in such a context. For a quick idea, when I'm done editing my file, I just call the right-clic menu on it, and here it goes...

Before:



After:




And I get my ready-to-use pdf file, with the right formatting, and compiled in the right mode:



For now, three different building modes are available:

  • the standard beamer version, with all overlays,

  • a 4 on 1 handout printable version,

  • and a 1 on 1 version, same as the beamer version, but with no overlays, useful for quickly checking page rendering.


And of course, if I need fine-tuning of the LaTeX stuff, I still have the (generated) .tex file in my folder.

If you only want to get the thing working, you can skip the rest, and go directly at the bottom to download. Else, I'll explain how the trick works. Please be adviced that this needs some knowledge about how an OS and a computer works. In my case, it is MS Windows (XP for me, but should be fine on others) and its standard "cmd" script langage, but Linux users should be able to translate in their own shell (at least, experienced Linux users...)

First, your file. As I said, it starts with
\begin{document}
and ends with
\end{document}
You need a title page, on which will be written your name, date, and... the title. So the first lines of the document will look like:
\begin{document}
\title[small title in footer]{Main Title On First Page}
\begin{frame}
\titlepage
\end{frame}
The date is usually set up automatically, and the authors name isn't something that changes every day, so it can be lying in a the header template. These settings can be of course overridden.
\begin{document}
\title[small title in footer]{Main Title On First Page}
\date{2050}
\author{Gill Bates}
\begin{frame}
\titlepage
\end{frame}
Ok, now, how about compiling ? This is done by a simple batch file, that basically concatenates three files: "mode" header, regular LaTeX header, and your document. On Windows, it goes like this (Linux users will tweak this easily):
copy /A "%mp%\head_beamer.tex"+"%mp%\common_header.tex"+"%fn%.texb" "%fn%.tex" > nul
pdfLaTeX.exe --interaction=batchmode "%fn%.tex" 1> LaTeX_log_stdout.txt 2> LaTeX_log_stderr.txt
'mp' is the path where the script and template headers lie, anf 'fn' is the file name. 'head_beamer.tex' is the short header that defines the mode 'beamer', while 'common_header.tex' is where the "real" header stuff goes.

Ok, and how do you choose the right header ? Well, this is really windows specific, as it is about it's registry, and I don't know how you define this with Linux.

To make it short, with Windows, each file extension defines a "type" of file, and each type gets associated with some things you can do with it. This information is stored in the so-called "registry". This can be (badly) handled through the 'assoc' cmd command, or using the 'regedit' GUI. But the best way is to write a .reg file, that will automated this process.

First, we define a new file extension: ".texb"(.tex + B for beamer), in order to avoid changing the default .tex file behaviour you have on your system :
[HKEY_CLASSES_ROOT\.texb]
@="LaTeX.BeamerBody"
And associate this file type (LaTeX.BeamerBody) with the corresponding commands:
[HKEY_CLASSES_ROOT\LaTeX.BeamerBody\shell\build_B]
@="Build (Beamer version)"
[HKEY_CLASSES_ROOT\LaTeX.BeamerBody\shell\build_B\command]
@="\"C:\\program files\\sk_scripts\\BuildWithBeamer\\BuildWithBeamer.bat\" \"%1\" B"
The 'B' letter at the end of the command is an argument that is passed to the script, so it gets the right header. For example, the 'beamer' header will look like this:
\documentclass{beamer}
\usetheme{Madrid}
the 'handout' version like this:
\documentclass[handout]{beamer}
\usetheme{Madrid}% change this to whatever beamer theme you want
and the 'handout 4 on 1' like this:
\documentclass[handout]{beamer}
\usetheme{default}
\selectcolormodel{gray}
\usepackage{pgfpages}
\pgfpagesuselayout{4 on 1}[a4paper,landscape,border shrink=1mm]
\pgfpageslogicalpageoptions{1}{border code=\pgfsetlinewidth{1.5bp}\pgfusepath{stroke}}
\pgfpageslogicalpageoptions{2}{border code=\pgfsetlinewidth{1.5bp}\pgfusepath{stroke}}
\pgfpageslogicalpageoptions{3}{border code=\pgfsetlinewidth{1.5bp}\pgfusepath{stroke}}
\pgfpageslogicalpageoptions{4}{border code=\pgfsetlinewidth{1.5bp}\pgfusepath{stroke}}

% because the 'default' does not add page numbers
\addtobeamertemplate{footline}{\insertframenumber/\inserttotalframenumber}
All this 'pgf' stuff is there to define a solid border around each slide, as the 'default' beamer theme is quite sober (thanks to all the guys on this excellent french-spoken LaTeX mailing list for this trick.)

So what if you want to have this shell menu available on your system ? Well, all this configuration is provided in this zip file: download, unzip, and launch Install.bat. This will copy everything in convenient places, and import settings in registry. You're ready to go, assuming, of course, you have a working LaTeX installed, and available in the path (I use MikTeX).

And of course, an 'uninstall' is provided, to remove the settings from the registry. Once you have this installed, you can check it by going down in the 'demo' folder, and double-clic the file 'demo.texb': it should produce a nice example pdf !

Q & A

Q: what if I don't like the theme you choose ?
Q: what if I want to change colors ?
Q: what if I want to add a package ?
Q: what if I wan't to remove your stupid badly designed logo I get on every page ?
A: All these questions have the same answer: just edit the file c:\program files\sk_scripts\BuildWithBeamer\common_header.tex as you like. This can be conveniently done with the fourth contextual menu item. If you are english native, you will probably have to, as at present, this header is set for french-written documents. Of course, this needs some LaTeX knowledge.

This command calls notepad++, if you use another editor, just change corresponding lines in the file 'install.reg', et re-import it into the registry. And don't forget: with LaTeX, to get the summary correct, you need to compile twice.

If you like this trick, you can tell me about, add a comment, or just drop me a line (firstname DOT lastname AT univ-rouen DOT fr).

Aucun commentaire:

Enregistrer un commentaire