Bulk updating TikZ pictures

I’m a strong ‘believer’ of PGF/TikZ as you might have noticed in my post about the folding library. For my PhD thesis I have dozens of TikZ pictures in separate files which I give a .tikz extension. This has proven to be a useful and practical way for me to reuse images and update them without to much searching.

Recently I decided to update my TeX Live installation — after all, I was still using the 2009 version. Unfortunately this had the unpleasant side effect of removing all the formatting from lines in TikZ pictures. Luckily there were still a few lines left which had kept their formatting allowing me to quickly realize what was going wrong. I had developed the habit to write a formatted line as follows

\draw (a) to [thick,teal] (b);

Unfortunately the only correct way now appears to be

\draw [thick,teal] (a) to (b);

This meant I had to go and change all my files, some of which contain a few hundred formatted lines. But let’s try to be smart about this.

The first step was to see how big the problem was. So the first thing that comes to mind is grep. Executing the following command in the images directory showed me that there were more than 500 offending lines that needed to be updated.

grep -r '\draw (\([^)]*\)) to \[\([^]]*\)\] (\([^)]*\));' . | wc -l

I didn’t really feel like doing those lines one-by-one. Fortunately this is not necessary thanks to the wonder that is sed. Just use the following command in the images directory.

sed -i .bak -e 's|\draw (\([^)]*\)) to \[\([^]]*\)\] (\([^)]*\));|
                  \draw [\2] (\1) to (\3);|' *.tikz

Note: This command is split over two lines to fit it in the limited space available in this column, but on the command line this should of course be single command.

This command will look for the wrong lines in the files and replace the file by a new corrected version. The old version will be backed up in a file with the extension .bak.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: