# How to render the definition of a macro on the PDF output?

For example we have a macro defined in a package as follows.

\newcommand{\test}{this is a test macro}


I want to render this definition on the PDF output as follows.

\documentclass{minimal}
\usepackage{test}
\begin{document}
Hi \somecommandtorendermacrodefinition{\test}
\end{document}


Such that I get

Hi \newcommand{\test}{this is a test macro}

in the PDF output.

How to do this with minimal hassle? Is there a package to do so?

-
I don't think this is possible. By the time it gets to \somcommand... the definition has been read and it can't tell whether it was a \def or a \newcommand or a \DeclareDocumentCommand (from xparse)... – Seamus Jul 16 '11 at 14:15
You might looking for \texttt{\string\test: \meaning\test}, but it won't output the \newcommand syntax. – Martin Scharrer Jul 16 '11 at 14:18

You can insert the definition of a macro into the document using the \meaning primitive. However you won't get the \newcommand syntax from it. You need to switch to tt font to get it correctly displayed, otherwise the backslashes and other character won't be displayed correctly. You can display the macro name using \string.

\newcommand*{\showmacro}[1]{\texttt{\string#1: \meaning#1 }


This will give you e.g. for \label the following output:

\label: macro:#1->\@bsphack \protected@write \@auxout {}{\string \newlabel {#1}{{\@currentlabel }{\thepage }}}\@esphack


Note that many macros use further macros, especially if they have optional arguments or are "protected" macros. So sometimes only the first step will be shown.

A better definition might be to use \csname .. \endcsname internally and also allow macro names with @ and spaces (protected macros define a second macro which includes a space at the end).

\newcommand*{\showmacro}[1]{\texttt{\expandafter\string\csname #1\endcsname: \expandafter\meaning\csname#1\endcsname}


Examples:

\showmacro{nobreakspace}

\showmacro{nobreakspace }


will give:

\nobreakspace: macro:->\protect \nobreakspace

\nobreakspace : \long macro:->\leavevmode \nobreak \


Note that there is also the texdef script which allows you to display macro definitions easily on the command line (However, I never tested it on non-Linux machines).

There is also the show2e package which gives you \showcmd which will show the definition of a macro including its underlying sub-macros as debugging output, i.e. stops the compilation and displays them in the output window but not in the document.

-
\relax cannot be expanded anymore? – xport Jul 16 '11 at 14:44
@xport: \relax is a primitive, so no, it can't be expanded. – Martin Scharrer Jul 16 '11 at 15:01