list of all used commands in compiled *.tex-documents

Taking a file, xyz.tex, I just used as an answer to another question

\documentclass[12pt]{report}
\usepackage{array}
\usepackage{amsmath}
\begin{document}
\begin{tabular}{| >{$}c<{$} | c | c | c | c | c | c |c |}
\cline{2-8}
\multicolumn{1}{c|}{}&\multicolumn{7}{c|}{\rule{0mm}{0.4cm}{figure1 put here}}\\ \hline
\rule[-.5cm]{0mm}{1.2cm} x & -0.01 & -0.001 & -0.0001 & 0 & 0.0001 & 0.001 & 0.01 \\\hline
\rule[-.5cm]{0mm}{1.2cm} f(x) & 1.99499 & 1.9995 & 1.99995 & ? & 2.00005 & 2.0005 & 2.00499\\\hline
\multicolumn{1}{c|}{}&\multicolumn{7}{c|}{\rule{0mm}{0.4cm}\text{figure2 put here}}\\\cline{2-8}
\end{tabular}
addsomething with \emph{\$ and \^{a}}

\end{document}

Then a simple command line such as

 egrep  -o '\\[a-zA-Z]+|\\[^a-zA-Z]' xyz.tex | sort | uniq

produces

\$
\\
\^
\begin
\cline
\documentclass
\emph
\end
\hline
\multicolumn
\rule
\text
\usepackage

That's using unix-ish tools (although I'm on windows) but any editor ought to be able to do the same thing.


I stumbled on this question and thought that it would be amusing to list the control sequences within the document itself. Of course, it suffices to input verbatim the listing produced by D.C.’s answer, but let’s say we want to do it all in a single run; this can be done in the following way:

\documentclass[10pt,a4paper]{article} 
\usepackage[T1]{fontenc}

\newcommand*{\meta}[1]{\(\langle\textit{#1}\rangle\)}

\title{The \TeX\ control sequences\\
    used in this document}
\author{A.~U.~Thor}



\begin{document}

\maketitle

What follows is an alphabetized list of the names of all the \TeX\ control
sequences used in this same document:

\begin{flushleft}
\ttfamily
\catcode`\^ = 12
\catcode`$ = 0
\catcode`\\ = 12
$obeylines
$input{|"egrep  -o '\\[a-zA-Z]+|\\[^a-zA-Z]' $jobname.tex | sort | uniq"}
$end{flushleft}

Note that \verb*|\ | is handled correctly, but \verb|\|\meta{CR} is not.
(We have included a \verb|\|\meta{CR} right here:~$\to$\
!)
Note also that the alfabetical ordering used by \texttt{sort} is somewhat
questionable, and that, of course, control sequences beginning with non-standard
escape characters, like \verb|$obeylines| or \verb|$input|, are not recognized.

\end{document}

Of course, this example must be compiled with the shell-escape feature enabled.

Addition

Here’s the promised refined version (see the “comment to myself”, below):

\documentclass[10pt,a4paper]{article} 
\usepackage[T1]{fontenc}

\title{The \TeX\ control sequences\\
    used in this document}
\author{A.~U.~Thor}

\newcommand*{\meta}[1]{\(\langle\textit{#1}\rangle\)}
\newcommand*{\CR}{\meta{CR}}

\newcommand*{\ListMyOwnCSNames}{%
    \begin{flushleft}%
        \ttfamily
        \def\do##1{\catcode`##1=12\relax}%
        \dospecials
        \obeylines
        \input{|"\CommandLine\jobname"}%
    \end{flushleft}%
}
\newcommand*{\CommandLine}{} % let's behave well and declare the name
\begingroup
    \catcode`\^ = 12
    \catcode`\$ = 0
    \catcode`\\ = 12
    $gdef $CommandLine #1{%
        egrep  -o '\\[a-zA-Z]+|\\[^a-zA-Z]' #1.tex | sort | uniq%
    }
$endgroup



\begin{document}

\maketitle

What follows is an alphabetized list of the names of all the \TeX\ control
sequences used in this same document:

\ListMyOwnCSNames

Note that \verb*|\ | is handled correctly, but \verb|\|\CR\ is not.
(We have included a \verb|\|\CR\ right here:~$\to$\
!)
Note also that the alfabetical ordering used by \texttt{sort} is somewhat
questionable, and that, of course, control sequences beginning with non-standard
escape characters, like \verb|$gdef| or \verb|$endgroup|, are not recognized.

Let's try a few special characters.  It costs 100\$.  Procter \& Gamble.  You
are the~\#1!.  De~l'H\^{o}pital.  A variable named \textit{total\_cost}.
A~10\%~discount.  \emph{Muchas gracias, se\~{n}orita!} (I~hope the Spanish is 
correct!)

Moreover: let \( P = \{\,x\mid\mbox{$x$ is prime}\,\} \), and\\a line break.

\end{document}

Please note that I wouldn’t have dared to write an answer like this, hadn’t the question already been catalogued under the “fun” tag.