Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
You could just dump the complete contents into your TeX file; at worst, this will result in slightly longer compilation time. –  Antal S-Z Jan 1 '11 at 23:00
    
I've never used Lyx, but I thought someone told me once that Lyx did this. Can any Lyx-users verify this? –  Z Norwood May 6 '12 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? –  Z Norwood May 6 '12 at 13:20
add comment

2 Answers

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

share|improve this answer
    
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? –  Z Norwood May 6 '12 at 13:16
add comment

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...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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