5

In this question someone asked if there was any way to eliminate user defined macros from a TeX document and replace them with the appropriate primitives. The answer seemed to be that while some programs did this for simple macros there was no general solution.

I'm in need of a less complex feature. Most journals I submit to are happy with user defined macros but they all must be included in the document to make it self-contained and the journals get upset if you throw in all sorts of unnecessary macros. This leads to the unpleasant task of manually discerning what macros from your files where used in a particular document and copying them over. I'd like to know if there is anything to automate this process.

This is potentially much easier. One has a finite list of \input{blah.tex} files that may contain the macro definition and one need only decide if the document might require that macro, i.e., it is referenced from a macro referenced in the document.

So under the assumptions that

1) No non-user code calls user macros that aren't explicitly passed in the arguments.

2) User code doesn't dynamically reference other user macros via \csname style constructs.

3) All user macros are either in the document itself or in the finite list of user macro files passed to the utility/package.

Is there any automatic way of listing those user macros that MIGHT be needed for compilation of the current document.

3
  • You could just dump the complete contents into your TeX file; at worst, this will result in slightly longer compilation time. Commented Jan 1, 2011 at 23:00
  • 1
    I've never used Lyx, but I thought someone told me once that Lyx did this. Can any Lyx-users verify this?
    – Zach N
    Commented May 6, 2012 at 13:18
  • This seems like a job for a LaTeX editor. I don't think TeXShop or TeXWorks does anything like this; maybe one of the popular Windows editors does?
    – Zach N
    Commented May 6, 2012 at 13:20

3 Answers 3

2

I'm also interested in having something like this, so I've written a short Python script, available from Github here. It's a bit crude, but for sensibly organized macro files (e.g. every \newcommand should appear at the beginning of a line) it seems to achieve what we want. Two notes:

  1. My script doesn't actually run TeX to see which macros are used (though it probably wouldn't be terribly difficult to incorporate Bruno's solution); I don't mind having one or two extra macros added, if they're picked up from occurrences in commented sections or something.

  2. It adds the necessary user-defined macros to the preamble of the document, but it could be easily modified to dump them elsewhere.

Feedback is welcome, and so are improvements. It would probably be an easy project for a seasoned Pythoner & TeXnician to make this ten times better.

6

The cmdtrack package will tell you which of your macros you have used (and where), and which you have not used.

1
  • The cmdtrack package seems to work only for macros defined in the preamble of the document. Is there a cmdtrack-like tool that will search some given macros.tex files?
    – Zach N
    Commented May 6, 2012 at 13:16
2

The code below lists all the macros that are expanded, starting at \begin{document} (we could start earlier, but it slows down by an order of magnitude). Once we have this list, we can maybe do what you want.

Add \AtBeginDocument{\tracingmacros=1} at the start of your document. Compile it normally: the log file will be much more verbose than usual. We then parse the log file by running TeX (or LaTeX, etc) on the following file:

\documentclass{minimal}

%sets the catcode of charcters in range [#2,#3] (included) to be #1.
\newcommand{\catXrange}[3]{%
  \count0=#2\relax %
  \loop
  \catcode\count0=#1\relax %
  \advance\count0 by 1\relax %
  \ifnum\count0>#3 \else%
  \repeat %
}


\newcommand{\extractcsnames}[1]{%
  \gdef\storage{}
  \begingroup
  \catXrange{12}{0}{255}
  \catcode`\^^M=13
  \catcode`\ =13
  \newwrite\cslistwrite
  \immediate\openout\cslistwrite #1.cslist 
  \input{#1.log}
  \immediate\closeout\cslistwrite
  \endgroup}


% ===================================================================
% Spaces and newlines are absolutely critical in the following code !
% ===================================================================
\begingroup\catcode`\^^M=13\catcode`\ =13%
\catcode`\/=0\catcode`\\=12%
/gdef #1

\#2 {/immediate/write/cslistwrite{{/string\#2}} }%
/endgroup
% ===================== end critical spacing ========================

\begin{document}

\extractcsnames{filetoparse}

\end{document} 

with filetoparse replaced by your file. This produces a file filetoparse with all the macros that TeX expanded when reading the file. For a typical 60 page document, that gives 600000 commands (with lots of repetitions, obviously).

If anyone has an idea for the next step...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .