Let's say I have a file foo.sty that contains the following:
\newcommand{\a}{x}
\newcommand{\b}[2]{x}
\newcommand{\c}[2][y]{x}
\newenvironment{d}[3]{}{}
I would like to be able to run this through some software and produce a data file saying what commands and environments have been defined, and how many optional and mandatory arguments they have -- something like this:
command,a,0,0
command,b,0,2
command,c,1,1
environment,d,0,3
I could code up something in perl that would work most of the time, but I'm wondering if there's any way to do it so that it's highly reliable. The impression I get is that completely correct parsing of LaTeX is hard, and basically the only thing that does it is LaTeX. Is there some way of getting LaTeX to do this, maybe with an appropriately constructed .tex file that pulls in foo.sty, introspects, and then does write18's...?
I'm looking for a solution that's open-source and that I can run on linux in an automated way (not a GUI).
\newcommand
and\newenvironment
to add a hook. Generally speaking, we have various ways to define macros with optional arguments, and it is a bit difficult to detect all of them.\newcommand
,\renewcommand
, etc. in the top-level files. Or, perhaps better, do something trivial like\outer\let\countednewcommand=\newcommand
and so on: then use these "counted" commands for the macros you want to track, and since they are\outer
, you can be sure they are all visible to a naive scanner.