List of newcommands used

\documentclass[12pt]{article}

\usepackage{etoolbox}

\makeatletter
 % This macro will contain all the tracked commands:
 \def\@mycommands{}
 % These macros enable and disable tracking the commands:
 \def\starttrackingnewcommands{%
     \let\old@@newcommand\@newcommand
     \def\@newcommand##1{%
         \expandafter\def\expandafter\@mycommands\expandafter{\@mycommands\oneof@mycommands##1}%
         \old@@newcommand##1%
     }%
 }
 \def\stoptrackingnewcommands{%
     \let\@newcommand\old@@newcommand
 }
 % These macros are used to write to the log file:
 \def\mycommand@used#1{\typeout{My command `\string #1' was used.}}
 \def\mycommand@unused#1{%
     \GenericWarning{(mycommands)}{LaTeX Warning:
         My command `\string #1' was not used!%
     }%
 }
 % These macros mark a command as used or unused:
 \def\mycommand@markunused#1{%
     \expandafter\gdef\csname mycommand@status@\expandafter\@gobble\string #1\endcsname{\mycommand@unused #1}%
     \pretocmd #1{\mycommand@markused #1}{}{\GenericWarning{(mycommands)}{Could not patch `\string #1' as unused!}}%
     \aftergroup\mycommand@markunused\aftergroup #1%
 }
 \def\mycommand@markused#1{%
     \expandafter\gdef\csname mycommand@status@\expandafter\@gobble\string #1\endcsname{\mycommand@used #1}%
     \patchcmd #1{\mycommand@markused #1}{}{}{\GenericWarning{(mycommands)}{Could not patch `\string #1' as used!}}%
     \aftergroup\mycommand@markused\aftergroup #1%
 }
 % This macro calls the appropriate logging macro for a command:
 \def\mycommand@evaluateuse#1{%
     \csname mycommand@status@\expandafter\@gobble\string#1\endcsname
 }
 % Mark all commands as unused at \begin{document}:
 \AtBeginDocument{%
     \let\oneof@mycommands\mycommand@markunused
     \@mycommands
 }
 % Evaluate the use of the commands at \end{document}:
 \AtEndDocument{%
     \let\oneof@mycommands\mycommand@evaluateuse
     {\let\mycommand@unused\@gobble% first, only the used commands
         \@mycommands
     }{\let\mycommand@used\@gobble% then, only the unused commands
         \@mycommands
     }%
 }
\makeatother

\starttrackingnewcommands
 \newcommand{\foo}{foo}
 \newcommand{\baz}{bar}
 \newcommand{\titi}{Hemixos castanonotus}
 \newcommand{\eag}{Haliaeetus leucocephalus}
\stoptrackingnewcommands

\begin{document}

I my travels, I have seen many birds of type \titi.

\end{document}

Towards the end of the log file you will find

My command `\titi' was used.

LaTeX Warning: My command `\foo' was not used! on input line 68.


LaTeX Warning: My command `\baz' was not used! on input line 68.


LaTeX Warning: My command `\eag' was not used! on input line 68.

Some Notes

This will break something if the first usage of one of your commands is used in a context like this:

\def\somethingwithargument#1{expansion}
\expandafter\somethingwithargument\oneofyourcommands

That is, if some command (\somethingwithargument) consumes the start of the expansion of one of your commands, the fact that you inserted something at the start of that command to keep track of its usage will break this code. I do not know of any safer way to do this, though.


I chose to format the message for unused commands as a warning and the message for a used command as just that. You can of course choose to do this differently (or even write to an entirely different file if you prefer this).


Since this is a rather invasive process (modifying all your commands temporarily), there may well be side effects i cannot think of right now. If you spot any, feel free to add them here.

In any case, you should not use this as a permanent addition to your preamble, only as a technique to get a quick overview over which commands you use.