[comp.emacs] My definitive [La]TeX spell checker

mike@ists (Mike Clarkson) (05/24/88)

There is a really easy way of spell checking [La]TeX files without
deTeX'ing and the like.  The idea is to make a new spell dictionary
that contains all of the [La]TeX keywords, and then use this 
dictionary with spell.  All you need to do is get sed to replace
all occurences of \ with \\ so that you don't fool spell, and
then pipe the result to spell with the new dictionary.  The new dictionary
will contain all the ususal words, plus all the TeX or LaTeX words.
This gives the added advantage of spell checking your [La]TeX
commands at the same time, which can be a big win.

You can define a command called texspell with something like

alias texspell 'sed -e "s|\\|\\\\|g" \!* | spell -d ~/tex/tex.dic'

This escapes the \'s for spell's benefit, and feeds the rest to spell
using your custom tex dictionary.

To generate the dictionary file put the following grep incantation into
an executable shell file, and run it over the relevant tex sources:

cat > latexwords << EOF
grep '\\def\\' $* | sed -e 's/{.*//' \
		-e 's/#.*//' \
		-e 's/(.*//' \
		-e 's/\[.*//' \
		-e 's/^.*\\def\\//' \
		-e '/@/d' 
EOF

chmod +x latexwords

If you are interested in LaTeX commands, the relevant files to
run latexwords on would be something like

% cat > latex.files << EOF
/usr/local/src/tex/tex82/LaTeX/latex.tex
/usr/local/src/tex/tex82/LaTeX/lplain.tex
EOF

Run latexwords on these files, and pipe the output to spell, and
you are left with a list of LaTeX macro names that spell considers
to be errors.  Save these words in the file latex.errs

% latexwords < latex.files | spell > latex.errs

The file latex.errs will contain all the plain LaTeX commands that are
not in the dictionary.  Now all you have to do is to make your own
dictionary with all of these words added.  Assuming you want to keep
the dictionary in the directory ~/tex, then

% spellin /usr/dict/hlista < latex.errs > ~/tex/latex.dict

Voila!  Put the following line in your .cshrc and you are all set

% alias latexspell 'sed -e "s|\\|\\\\|g" \!* | spell -d ~/tex/latex.dic'


For tex commands, it's a little harder because you need to get at the
TeX primitives.  But if you have a full Unix TeX distribution, there should
be a file in the tex82/doc directory called csname.txt, that has the full
list of all plain TeX commands:

grep '^\\' /usr/local/src/tex/tex82/doc/csname.txt | \
	sed -e 's/	.*//' 	\
	    -e 's/\\//'		\ 
	    -e '/@/d' 		|
	spell > tex.errs

By the way, that's a <TAB> (^I) character in the first sed line here.
Now do like before:

% spellin /usr/dict/hlista < tex.errs > ~/tex/tex.dict

Voila!  Put the following line in your .cshrc and you are all set

% alias texspell 'sed -e "s|\\|\\\\|g" \!* | spell -d ~/tex/tex.dic'

You can improve on this in many ways:
	make the sed script for latex smarter, bigger better.
	run the sed script for latex on the style files.
	combine tex.errs and latex.errs to make a mixed dictionary
		for people who add TeX commands into LaTeX.


For convenience, the script below should create the dictionairies.
You may have to change the source file locations.  Edit the install script
and sh install. Add the contents of cshrc to your .cshrc and your set.
It's pretty trivial to hack it into gnu in spell.el

#--------------------------------CUT HERE-------------------------------------
#! /bin/sh
#
# This is a shell archive.  Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command "sh file".  The files
# will be extracted into the current directory owned by
# you with default permissions.
#
# The files contained herein are:
#
# -rw-r--r--  1 mike          140 May 24 03:05 cshrc
# -rw-r--r--  1 mike          321 May 24 03:00 latexwords
# -rw-r--r--  1 mike          205 May 24 03:00 texwords
# -rw-r--r--  1 mike           70 May 24 03:03 install
#
echo 'x - cshrc'
if test -f cshrc; then echo 'shar: not overwriting cshrc'; else
sed 's/^X//' << '________This_Is_The_END________' > cshrc
Xalias latexspell 'sed -e "s|\\|\\\\|g" \!* | spell -d ~/tex/latex.dict'
Xalias texspell 'sed -e "s|\\|\\\\|g" \!* | spell -d ~/tex/tex.dict'
________This_Is_The_END________
if test `wc -l < cshrc` -ne 2; then
	echo 'shar: cshrc was damaged during transit (should have been 2 lines)'
fi
fi		; : end of overwriting check
echo 'x - latexwords'
if test -f latexwords; then echo 'shar: not overwriting latexwords'; else
sed 's/^X//' << '________This_Is_The_END________' > latexwords
X#!/bin/sh
Xrm -f tex.errs
Xgrep '\\def\\' 			\
X	/usr/local/src/tex/tex82/latex/latex.tex 	\
X	/usr/local/src/tex/tex82/latex/lplain.tex	\
X	| sed -e 's/{.*//' \
X		-e 's/#.*//' \
X		-e 's/(.*//' \
X		-e 's/\[.*//' \
X		-e 's/^.*\\def\\//' \
X		-e '/@/d' \
X	| spell > latex.errs
X
Xspellin /usr/dict/hlista < latex.errs > latex.dict
________This_Is_The_END________
if test `wc -l < latexwords` -ne 14; then
	echo 'shar: latexwords was damaged during transit (should have been 14 lines)'
fi
fi		; : end of overwriting check
echo 'x - texwords'
if test -f texwords; then echo 'shar: not overwriting texwords'; else
sed 's/^X//' << '________This_Is_The_END________' > texwords
X#!/bin/sh
Xrm -f tex.errs
Xgrep '^\\' /usr/local/src/tex/tex82/doc/csname.txt | \
X	sed -e 's/	.*//' 	\
X	    -e 's/\\//'		\
X	    -e '/@/d' 		|
X	spell > tex.errs
Xspellin /usr/dict/hlista < tex.errs > tex.dict
________This_Is_The_END________
if test `wc -l < texwords` -ne 8; then
	echo 'shar: texwords was damaged during transit (should have been 8 lines)'
fi
fi		; : end of overwriting check
echo 'x - install'
if test -f install; then echo 'shar: not overwriting install'; else
sed 's/^X//' << '________This_Is_The_END________' > install
XDICT=$HOME/tex
Xsh latexwords
Xsh texwords
Xmv latex.dict tex.dict $DICT
________This_Is_The_END________
if test `wc -l < install` -ne 4; then
	echo 'shar: install was damaged during transit (should have been 4 lines)'
fi
fi		; : end of overwriting check
exit 0

-- 
Mike Clarkson					mike@ists.UUCP
Institute for Space and Terrestrial Science	mike@ists.yorku.ca
York University, North York, Ontario,		uunet!mnetor!yunexus!ists!mike
CANADA M3J 1P3					+1 (416) 736-5611