Speed up the work by shell scripts

When writing LaTeX and redefining macros I frequently have to look at the source code of macros in LaTeX class files or plain TeX sources. I got used to kpsewhich, find, grep and xargs, but because I needed it often it became necessary to speed it up.
In order to find and edit tex files of the tex distribution I normally used kpsewhich with backticks like
gedit `kpsewhich scrartcl.cls`
And in order to find the source of a certain macro (beside show) I used find and grep together with xargs.

A simple way to speed it up is to use shell scripts. For the tasks above I wrote two bash scripts today. The first equivalent to the one-liner above called texedit is:

#!/bin/bash
# texedit - find one or several tex related files
#   and open them in the editor
if [ $# -eq 0 ]; then
  echo 1>&2 Usage: texedit file1 [file2] ...
  exit 1
fi
gedit `kpsewhich $@`
exit 0

With it I just call for instance
texedit latex.ltx report.cls article.cls
to open these three files in the editor, no matter where they are in the texmf tree.
The other script called texgrep is:

#!/bin/bash
# texgrep - searches for a text pattern contained in files
#   located inside the texmf trees
# usage: texgrep pattern [extension]
# usage examples:
#   texgrep phantomsection sty
#   texgrep \\def\\phantomsection
# Stefan Kottwitz, 2008
if [ $# -eq 0 ]; then
  echo 1>&2 Usage: texgrep pattern [extension]
  exit 1
fi
for path in TEXMFMAIN TEXMFDIST TEXMFHOME
do
 find `kpsewhich --var-value=$path` -name "*$2" |xargs grep $1
done
exit 0

I’ve described the usage inside the comments of the script together with an example. I’ve already used those scripts several times and will use similar commands for similar tasks.

Just another example, calles texls:

#!/bin/bash
# texls - list the content of the directory
#   corresponding to a certain tex related file
if [ $# -eq 0 ]; then
  echo 1>&2 Usage:    texls filename [pattern]
  echo 1>&2 examples: texls babel.sty
  echo 1>&2           texls book.cls *clo
  exit 1
fi
ls `kpsewhich $1 | sed 's/(.*)/.*$/1//'`$2
exit 0

If I want to list all files inside the directory of the babel package, I just type:
texls babel.sty
If I want to list all the language definition files of babel, I call:
texls babel.sty *ldf
Or to list all class option files of the LaTeX base classes:
texls book.cls *clo
After I found what I was looking for I usually call texedit.

The scripts are written for my own use with Ubuntu Linux and TeXlive, it should be easy to customize them for other needs and different Unix platforms.

I’ve posted similar scripts to a discussion on mrunix.de.

08. July 2008 by stefan
Categories: Tools for LaTeX | Leave a comment

Leave a Reply