11

Is it possible to get a list of all the commands used in the compiled documents? I mean, if I use in my various .tex-documents commands such as \emph, \section, \cite, \begin, \documentclass, \footnote, and \MyCommandX, can I generate a list similar to the following?

\begin
\cite
\documentclass
\emph
\footnote
\MyCommandX
\section

Similar question: Automatically return list of all uses of a command, without repeats e.g. \compound using chemstyle

  • You just mean the commands used explicitly in the source, not used in definitions? Any editor ought to be able to show that I'd have thought. – David Carlisle Jan 19 '16 at 9:07
  • Yes, that's what I mean. I am using TexShop. It never occurred to me that this might be a feature of the editor. In any case, I don't see anything similar in TexShop. Any other editors which have that feature on mac? – ClintEastwood Jan 19 '16 at 9:09
18

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.

| improve this answer | |
  • that was quick -- and seems so simple. – ClintEastwood Jan 19 '16 at 9:28
  • @ClintEastwood You'll feel that often when you see these command line tools. – naiveai Apr 1 '16 at 7:31
7

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.

| improve this answer | |
  • A comment to myself: other “special” characters should be \catcoded 12 too. Imagine the document uses \_, \%, etc. Also, it’s better to switch to an indirect approach, in order to “decouple” the \catcode regime: define a macro in the preamble, etc.; shall fix tomorrow. BTW: \$ does work as the code stands, but produces a somewhat unexpected result: can you guess what it is? – GuM Mar 20 '16 at 23:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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