Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am a beginner, trying to make lists (and eventually count and make statistics) the LaTeX keywords appearing in some documents. Basically a LaTeX "valid" keyword can be identified by:

  1. The fact that it does not begin with a "%" sign (it would be commented therefore not in use)
  2. It starts with a backslash.
  3. Is is followed by either a left curly bracket (or a left bracket, in case there are options.)

I understand there are other "special" cases: keywords followed by nothing (a white space), or another keyword, but these are relatively rare and I do no mind the inaccuracy. My primary goal is to get eventually a rought idea of the number of different keywords in various types of documents.

I have tried various ways using awk, but could not get anything working... I have attached a MWE (meaningless and not functional!) of a LaTeX file for reference.

\documentclass[a4paper]{scrreprt} % Mode production
\begin{tikzpicture}[remember picture,overlay]
\includegraphics[width=#2]{#3}\caption {#4}
\caption {#3}
\multirow{2}{*}{Part number}&\multicolumn{2}{c|}{Cavity A}&\multicolumn{2}{c|}{Cavity B}\\
\end {tabular}
share|improve this question
up vote 2 down vote accepted

Here's a script that splits out the LaTeX commands in a source file. It strips comments beginning with %. It outputs all the commands with a leading \, and all the environment names as well.

sed -e 's/^\(\(\)*\)%.*/\1/' \
    -e 's/\([^\\]\(\\\\\)*\)%.*/\1/' \
    -e 's/\\./\n&/g' |
sed -n -e 's/^\\begin *{\([^{}]*\)}.*/\1/p' \
       -e 's/^\\end *{.*//' \
       -e 's/^\(\\[@A-Za-z][@A-Za-z]*\).*/\1/p' \
       -e 's/^\(\\[^@A-Za-z]\)/\1/p' |


  1. The first sed pass adds a newline before every backslash. The first two expressions strip off comments, taking care to retain \% but still strip comments that are preceded by \\.
  2. In the second sed pass, the first expression prints environment names from \begin commands and the second expression ignores \end commands. The third expression prints commands whose names are letters and the fourth expression prints commands whose name is a symbol.

This script does not handle verbatim environments.

share|improve this answer
Thanks. In the meanwhile, I have seen in a LaTeX course a reference to a utility called "texcount" [link] (app.uio.no/ifi/texcount) It seems to do the type of job I had in mind, while obviously far more ambitious... – Yves May 21 '12 at 17:37
After additional investigations, texcount eliminates from the count the works I am interested in. As it does not seem practical to operate a substraction, I am going to go the sed way. – Yves May 22 '12 at 16:34

This sounds like a variation on this question.

So I'll work on that answer:

for w in `cat test.tex`; do echo $w; done|sort|egrep '\\[a-z]*{'|uniq -c
  1 A}&\multicolumn{2}{c|}{Cavity
  1 \begin{figure}[H]
  1 \begin{minipage}[b]{.46\textwidth}
  1 \begin{tabular}{|c|cS|cS|}
  1 \begin{tikzpicture}[remember

The regular expression may need some work but this should get you started.

share|improve this answer
This isn't going to work well. You can see that \multicolumn isn't split off. The shell's globbing is going to fire up (try with a file containing $a * b$), you need to run set -f first. – Gilles May 21 '12 at 7:45
The nice part in this solution is the integrated counter. – Yves May 22 '12 at 16:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.