# LaTeX

## A perl script for indenting .tex files

As LaTeX users we are confident that the output of our documents will almost always be beautiful. Best practices also dictate that we try to construct our code to be as robust and portable as possible; one of the things that sometimes gets overlooked, especially when working on larger documents, is the somewhat-tedious task of indenting source code, and aligning delimiter tabs.

Of course, some editors do this kind of thing for us; for example, vim users can (in Normal mode)  press gg=G which will indent the code automagically (although this action doesn’t align the delimiter tabs); I’m sure it can be done in other editors too. I had also read LaTeX code sniffer and or beautifier but found the linked Perl script didn’t behave as I wanted it to.

So, I found that I was interested in taking my own approach that I could ‘easily’ customize, which is why I wrote the Perl Script indent.plx which can be found at https://github.com/cmhughes/latexindent.plx. Note that you should click on “indent.plx” and then download it by clicking on the “Raw” button.

## Demonstration

We’ll talk about how it works a little later, but first here’s a visual demonstration of what it does. Let’s say that we have a block of code that uses alignment delimiters, something like the align, or tabular environment that we know and love. Some typical source code might look like the following:

Of course, if we’re working on a small document (or posting an answer to tex exchange) and had a fair bit of time, then we might take the time to align all of the &, but it’s not usually a good use of our time.  If we’re working on a larger document, perhaps a chapter file that has around 10,000 lines, then taking care of such alignment is extremely tedious and probably not very worthwhile, especially early in the editing process.

After running the above code through indent.plx, we obtain

which, I hope you’ll agree, is a lot nicer to look at.

Here’s another example- a fairly standard TikZ picture; this example demonstrates that nested environments indent as we wish. First of all, here’s the code we start with (ugh!)

and after running it through indent.plx, we obtain the following

## How does it work?

The script loops through the input file, and checks for matches of the form \begin{.*} and \end{.*}. If it finds a match, then it will (by default) add a level of indentation for the block within the environment. If there are environments that you don’t wish to have indented, then you can specify them at the beginning of indent.plx; furthermore, if you have a special rule for indentation of a certain environment (perhaps double tab? or a single space?) you can specify that as well.

If the environment matches one that has alignment delimiters, such as tabular, align, or any other type that you specify, then the block will be stored for processing in the subroutine format_block. This subroutine loops through the rows, splits each row at the &amp; and measures the size of the column. This allows it to output to a formatted string using sprintf.

The script will also match commands such as \parbox{} that may split their braces across lines, provided that the opening brace starts on the same line as the command, \parbox{... and the finishing brace finishes on its own line.

The script is mainly designed for ‘chapter’ files; by default it will not operate on the preamble.

## Is this script for you?

Possibly :) Hopefully. But maybe not. Whenever you’re working with a script that modifies a text file, you should be very careful. Before you try it out on your important documents, try it on the file sampleBEFORE.tex and see what you think. Inspect the code, and see if my (what I hope is fairly orthodox) style fits with yours. If it does, then take it for a spin on one of you own files- but please always make a back up first! The script does work in a lot of situations, but there are others when it may fail, I don’t claim that it will work for every .tex file ever produced.

## How do you use the script?

If you’re on .*nix then you can run it from the command line using

perl indent.plx myfile.tex

and if you want to output to a file, then you can use

perl indent.plx myfile.tex&gt; outputfile.tex.

Of course, you could make it executable (chmod +x indent.plx) and put it with your other executable files- I use /usr/local/bin but other choices are available.

Windows users need a Perl installation. Paulo’s excellent answer to How to make the rmligs script available globally on windows will be helpful.

## Moving forward

As a huge fan of open source material, I encourage anyone and everyone to dive in and make improvements. If it’s useful enough that anyone would like to collaborate and improve the script, I’d be very interested in doing so. For the moment, let’s communicate through the chat here on tex exchange.

I hope it’s useful; if it’s not directly useful, I hope that perhaps it gives you an idea of how you might make it useful, or create something of your own :)

Final disclaimer: I don’t claim to be a Perl expert- I’m sure that the code could be improved :)

## Representing TeX.SX in TeX and LaTeX Contests

2012-06-15 by Stefan Kottwitz. 0 comments

Contests are a nice way of pursuing our TeX passion. We have an enjoyable competition, show our TeX skills and learn something new, while producing our contribution, and by seeing the results of other fellow TeXnicians. We already had a contest on TeX.SX, celebrating the first birthday of our site.

Contests are also a way to reach out to TeX users who did not visit our site yet: if such a contest happens somewhere else, and if we go there and show our skills.

Currently there’s one running on LaTeX-Community.org. LaTeX users are invited to write a small article, the best articles will be rewarded with a prize. Packt Publishing has sponsored two copies of the gnuplot Cookbook written by Lee Phillips for the winners. Matching the subject of the book, the topic of the contest is LaTeX and graphics.

The contest will close June 20th. So, there are some days left. Even small articles just like blog posts are very welcome. I would appreciate if some of our fellow TeX.SX users would join the competition. I know, we have very capable TeXnicians here on the site, also regarding the contest topic.

For more details about the competition and for a contact, have a look at the LaTeX Contest announcement.

## The Hitch-Hiker’s Guide to LaTeX3

2012-05-23 by Andrew Stacey. 1 comments

## Introduction

As with the original Hitch-Hiker’s Guide, this blog post won’t actually be all that useful to someone wanting to truly explore LaTeX3. It’s more of a “What I did on my holidays” kind of guide. I’ve recently had my first go at doing some coding with LaTeX3 and I thought it might be interesting to record my experiences.

The main site has certainly made me more aware of the LaTeX3 project. Familiarity, in this case, has not bred contempt but rather it has meant that LaTeX3 has seeped into my consciousness so that I’ve become vaguely familiar with it without ever actually having to think about it. Before this current experience then I had had a bit of exposure to LaTeX3 through making a few adaptations to the (most excellent) unicode-math package, but that was very definitely a case of “Monkey see, Monkey not really have a clue what he’s doing so it’s a million to one chance but it might just work.”. To really get to grips with something new, I need a problem to work on. Finally, along came a problem that I wanted to solve in TeX but which was very definitely a regular programming problem. So the prospect of doing it in ordinary TeX was too daunting to contemplate. For this problem, learning LaTeX3 seemed the lesser of two evils.

more »

## KOMAscript: new English manual

Markus Kohm, maintainer of KOMAscript has published a new version (3.11) of the KOMAscript bundle. There is one extraordinary issue: The new version includes a new manual in English. Markus said on his webpage:

»A new English user manual with the same structure and completely translated content of the German user manual is provided. There are still several issues with the translation, nevertheless, it’s much better than the previous English manual.«

As far as I can see, all the features of KOMAscript 3 are documented, the same as in the German version.

As TeXLive 2011 is frozen right now , you probably need to download the new version yourself from Berlios; follow the links on Markus’ website. Users of MiKTeX will probably get it soon.

Alexander

## Using sprite sheets with LaTeX

2012-05-04 by Paulo Cereda. 4 comments

Sprite sheet is the name of a big image containing several smaller images or icons. It’s a technique usually employed by webdesigners to reduce the number of requests the browser makes to the server – reducing the number of HTTP requests can make a Web page load much faster. It’s also used a lot in animation engines.

I was wondering if I could use a sprite sheet in a LaTeX document, so I got a sample image from the Tango Desktop Project and mapped it:

All icons are placed in a big image, like a matrix, and the size is fixed at 24px × 24px per icon. How can I select one of the icons from a LaTeX document? Thanks to our friend and moderator Martin Scharrer, including sprite sheets is a breeze with adjustbox!

This amazing package, amongst other features, allows us to adjust images with \adjustimage. We can also use \includegraphics with the clip option which we can use for mapping our sprite sheet. For instance, let’s include the warning sign in our document. We need to collect some data first:

We have all the parameters for including the warning sign. It’s now a simple call to \includegraphics[clip,trim=48px 96px 120px 48px]{sampletango.png} or \adjustimage{Clip=48px 96px 120px 48px}{sampletango.png} and the correct icon is included! The parameters to the trim/Clip option are:

• 2 icons from left to right: 2 × 24px = 48px
• 4 icons from bottom to top: 4 × 24 = 96px
• 5 icons from right to left: 5 × 24 = 120px
• 2 icons from top to bottom: 2 × 24px = 48px

The full code is as follows:

\documentclass{article}

\usepackage{graphicx}

\begin{document}

\begin{figure}
\centering
% using includegraphics
% \includegraphics[clip,trim=48px 96px 120px 48px]{sampletango.png}
\caption{A warning sign.}
\end{figure}

\end{document}
The output:

There we go, sprite sheets with LaTeX. :)

## Generating LaTeX code with a template engine

2012-03-15 by Paulo Cereda. 7 comments

Sometimes, you get stuck with repetitive tasks, like generating the very same document over and over with a few changes in the content. I’m the coordinator of a small choir in a parish, and twice a month I have to print songsheets for the community. I usually try to automate as many tasks as I can, but LaTeX output is quite tricky. Here’s my attempt to write a program that spits out a .tex file.

more »

## The cake is a lie

2012-01-30 by Paulo Cereda. 6 comments

It all started a few days ago, when we had a question on TeX.sx about using TeX for (nearly) any kind of document. In one of the answers, our friend Andrew Stacey mentioned that he even does his kids birthday invitations in TeX; in the comments, Stefan Kottwitz asked for seeing this TikZ birthday cake, so Andrew updated his answer with a picture of the cake:

Yummy! An epic TikZ cake.

We all got very excited by this elegant cake, so our friend N.N. posted a follow-up question on how can I draw a cake using TikZ; Andrew then posted his code, to the joy of the community!

We all love cake.

I know only a few things about TikZ, as I only use the automata library. But  I was tempted to draw another cake. And if you play games like I do, by the title of this blog post, you already know what kind of cake I’m talking about.

more »

## Introduction

After answering the question about macro for \left( and \right) I realized that there’s a discussion worth considering behind the scene here. In particular, one can consider a beautiful solution, like the one of Herbert’s. This kind of answer, by all means, addresses the question posed. However, alongside, it also introduces several issues which might suggest that a better course of action is to be taken. Non standard code examples, like these which can be found in the question mention, as well as in many other threads in TeX.SE, have one prominent drawback, namely, they turn the code into a non standard one. I believe that for simple standard tasks (like the one dealt with in the question I started this post with) a more standard solution, in terms of TeX should be used. Let it either be code reuse or collaboration, non standard code serves as a problem. On the other hand, typing the full and standard TeX code can be sometimes be lengthy and tedious. Just like one won’t change the syntax of a for loop in any programming language, although it is being extensively used, one should not change, in my mind, the essence of TeX code. The alternative’s, which I would like to introduce and account my way of using, goal is to enable efficient typesetting process on the one hand, and maintain the resulting source code as standard as possible. It is called code snippets. more »

## Building documents with rubber

2011-12-04 by Paulo Cereda. 15 comments

It’s common to compile our documents a couple of times to ensure, amongst other things, correct cross-referencing and indices. Sometimes, we also rely on a Makefile in order to make things easier. Now enters rubber, a powerful tool to help us on our building adventures.

more »