11

I want to dynamically create a list of LaTeX commands that are valid in a particular file based on the packages used so that I can use them for code completion.

Does LaTeX provide a method for checking whether commands are commands (besides attempting to compile), or better, for obtaining a list of commands in a package? I don't care what kind of argument they have to take etc for my purpose (though if they have to have certain arrangements of braces afterward, that could be relevant). It is significant whether they work in math mode and whether they work out of it.

Failing that, is there a simple way to parse TeX files and packages to obtain all packages used and all commands therein?

  • 3
    No, this is not possible. Packages may define commands dynamically or in very contorted ways. Just by way of example, algpseudocode uses \algdef or \algnewcommand for defining its commands. – egreg Dec 15 '15 at 21:11
  • 2
    this question tells how to identify all the packages used in a job: Create list of all external files used by master LaTeX document?. getting a list of all commands is an entirely different matter. – barbara beeton Dec 15 '15 at 21:19
  • 2
    AUCTeX for Emacs does syntax coloring (and completion although I never tried to learn the key strokes for that) of know commands, but the developers must maintain a database (sort of) of the known packages ; by necessity this is only partial. Possibly other integrated editors for LaTeX have such database incorporated. I don't know actually if "database" is the technically appropriate term. AUCTeX must have "style" files for each LaTeX package it knows. – user4686 Dec 15 '15 at 23:00
5

No, it is not possible. Packages may define commands in several ways, besides the standard \def (and variants) or \newcommand and \DeclareRobustCommand. Consider for instance, \DeclareMathSymbol or \DeclareTextCommand (but there are other ways even in the LaTeX kernel).

Just to make a few examples, algpseudocode uses \algdef and \algnewcommand; other package have their own methods. They can even define commands dynamically based on user input. Other commands may have “limited life” (think to the unit names for siunitx) and different syntax depending on where they're called.

The list should also exclude all internal commands and others such as \temp \next or \do that are used as scratch macro names.

As regards to syntax, consider that, for instance, memoir redefines \chapter and the other sectional commands to have two optional arguments, rather than one. Other packages might define *-variants of existing commands, and in order to discover this you need to analyze the definition itself: basically, if \foo is defined and a package wants to add a *-variant, we would find something like

\let\org@foo\foo
\renewcommand{\foo}{\@ifstar\foo@star\org@foo}
\newcommand{\foo@star}{...<whatever>...}

The number of packages and commands they define is huge. Really. Doing a lexical analysis of just the packages loaded by the user would mean following all \RequirePackage in called ones. And maybe some of them are only loaded conditionally. Not to mention that commands might be defined in other files input by packages, so you should follow also \input and similar commands.

Good luck. ;-)

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.