How to make a list of used custom macros

I used to have a separate file where I store my custom macros. It is nothing more than a huge list of \newcommands and \NewDocumentCommands.

Now, I have a little note that I would like to share with a buddy of mine. I would like to send him a selfcontained LaTeX file, where all the custom macros that I use in the this particular note are included in the header. I don't like to copy-paste all the macros in the header, because I use only a small number of them. Is there a better way than to scan manually through my note and include the macros one by one?

For a better understanding here a MWE:

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{macros.tex}
\newcommand{\macroA}{A}
\newcommand{\macroB}{B}
\newcommand{\macroC}{C}
\newcommand{\macroD}{D}
\end{filecontents*}

\begin{document}
\input{macros.tex}
\macroA and \macroD
\end{document}


I would like to generate the following list:

• \macroA
• \macroD

I found a partial solution with the help of this.

I use the package cmdtrack and afterwards awk to scan my log-File. (grep or similar program would also be possible.)

The slightly changed MWE looks like

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{macros.tex}
\newcommand{\macroA}{A}
\newcommand{\macroB}{B}
\newcommand{\macroC}{C}
\newcommand{\macroD}{D}
\end{filecontents*}

\usepackage{cmdtrack}
\input{macros.tex}

\begin{document}
\macroA{} and \macroD{}
\end{document}


After compilation I run the following awk-command:

awk ' /was used on line/ { print \$1 }' <file>.log


The result is the following list:

"macroA"
"macroD"


For macros with \newcommands this works but not for macros with \NewDocumentsCommands.

• Where does \NewDocumentCommand come from? – daleif Jul 1 '14 at 15:21
• @daleif the \NewDocumentCommand comes from the package xparse. – crixstox Jul 2 '14 at 10:01
• ahh, my bad, I've always just \DeclareDocumentCommand when using xparse. Since xparse is LTX3, there might be a module for it, otherwise ask on the LTX3 list. Though, I've forgotten how I subscribed to it. – daleif Jul 2 '14 at 10:21
• @daleif You should consider switching to \NewDocumentCommand, because \DeclareDocumentCommand overwrites any previous definition (like \def) while \NewDocumentCommand checks if there is already one defined (like \newcommand). – Henri Menke Jul 31 '14 at 16:30