# Italicize all words that doesn't belong to the dictionary

I'm a spanish Computer Science student and I'm doing my final degree project with Latex. I'm using TexMaker on Windows and I want to know if I can italicize all the words that doesn't belong to the dictionary (in my case, those words are english words, because they make us italicize all non spanish words)

• Welcome! On a Unix-type system, you could hack together a script to do this - sed & grep or whatever. Presumably there's some equivalent on Windows. But you would have to very, very carefully read through the resulting text because there will inevitably be both false positives and false negatives. Even figuring out what to count as a word is going to be problematic. Should the contents of all maths environments be ignored? Obviously, you'll need to ignore all macros, environment names .... Really, it is almost certainly easier to use a spell check interactively to find the possible candidates. – cfr Jun 1 '16 at 23:05
• @cfr I've hacked together a script that relies on aspell. However I don't recommend using all of it. – Chris H Jun 3 '16 at 20:12

I suggest using aspell as your dictionary. It's a TeX-aware commandline spell checker. If you write a macro \newcommand[1]{\foreign}{\emph{#1}} and add add-tex-cmmand foreign p to your aspell.conf then aspell will ignore any words wrapped in \foreign{}. Then you can spell check your document with aspell -t -c myfile.tex in your own language and replace the words it finds with \foreign{word}

You can further enhance this by using the list mode of aspell. This time your command is cat myfile.tex | aspell list > foreign.txt (on Windows, use type instead of cat). Then you have a list of misspelt words, which you can find/replace.

This is probably the place to start working manually on a Windows system (unless you have cygwin as I do or want to write a Python script). On *nix systems you can improve this list by using cat myfile.tex |aspell list |sort |uniq > foreign.txt (tested under windows using cygwin. At this point I'd do a find/replace for each of these words using a regex, though a further pipe through |tr -s '\r\n' '|' could be used to produce a list delimited byt the pipe character used to separate options in a regex. The output could then be fed to sed, though I'd apply it manually as my editor supports regex.

If you really want to automate it here's a shell script:

echo -n "s/(" >temp.txt
cat myfile.tex | aspell -t list | sort |uniq |tr '\r\n' '|' |sed 's/.$//'>>temp.txt echo -n ")/\\\foreign{\1}/g" >>temp.txt sed -r -f temp.txt myfile.tex  Output is to stdout for testing, so redirect to myfile_new.tex. On the following test myfile.tex: \documentclass{article} \begin{document} one dos tres four one dos tres four uno two three cuatro uno two three cuatro$\cos(x)=\sin(y)$\end{document}  the effect is: \documentclass{article} \begin{document} one dos \foreign{tres} four one dos \foreign{tres} four \foreign{uno} two three \foreign{cuatro} \foreign{uno} two three \foreign{cuatro}$\cos(x)=\sin(y)\$
\end{document}


This also indicates one of the flaws of a too-automatic approach: As well as being the Spanish for two, dos is a valid English word (e.g. in the phrase "dos and don'ts" or meaning parties). No spell checker can deal with this, a human would need context.

• Now it's complete, but I don't really recommend automating the find/replace part. Even if you do, you should trawl through the list of foreign words to check it, and you'll still need to manually deal with words that exist in both languages. – Chris H Jun 3 '16 at 20:13
• Neat. Though I think your advice not to use it is probably wise! – cfr Jun 3 '16 at 21:40