I tried making a macro that checks if some macro is defined or not, and if it is it will print the definition of the macro. If the macro is undefined, it will showcase this. I am somewhat content with how it works, but I want to do some improvements. Here is a MWE of the macro:
\documentclass{article}
\newcommand{\defcheck}[1]{
\ifdefined#1
\noindent\texttt{: --> \meaning#1}\vspace{\baselineskip}
\else
\noindent\texttt{!: --> Undefined macro}\vspace{\baselineskip}
\fi
}
\begin{document}
% \section*{Macro Definitions: Output}
% Logged output from \texttt{\textbackslash defcheck}:\vspace{\baselineskip}
\defcheck{\defcheck{\usepackage}}
\defcheck{\undefined}
\end{document}
Output:
There are 3 things I would like to fix:
1.
Unfortuantly, if I do \defcheck{\usepackage} it will return an error, saying LaTeX Error: Can be used only in preamble. How can I avoid any errors inside \defcheck? Is there a way to not execute any commands that is given as an arguemt of \defcheck? I was thinking maybe expl3 syntax may solve this problem? Or maybe just give the text and in some way make it into a command where needed? (as in \defcheck{usepackage} and then insert a \ before #1 in some way to make it a command)
2.
When the macro is defined the command should print the macro before the colon, like this:
\defcheck : --> \long macro:#1-> \ifdefined (...) . This may be fixed with \textbacklash and the solution mentioned above (\defcheck{usepackage} instead of \defcheck{\usepackage}). In the case of an undefined macro, I would like it to be something like: ! : --> Undefined macro; "\undefined"
3.
I would like to sort of "collect" all \defchecks in the same place in the document (under a section or chapter), like this:
Preferably this should be at the end of the PDF (on its own page), and not appear in TOC. All \defchecks should automaticually go under this section in the order they were called.
Side note: How do I get other commands printed under the same section? For example; if I were to make a similar command to check if a colour is defined, how could I print the results in the same section (but maybe with its own subsection)?
Edit: when I tried \hbox it printed : --> \hbox. Why is it unable to show the definition of \hbox? A similar thing happened when i tried \csname.
Edit 2: I see now that I may not have expressed myself clearly at number 3. If I use \defcheck in the middle of a paragraph on the first page, I still want it to appear on a separate page last in the document. Preferably, the first use of the macro would automaticually create a new page at the end of the document where all the other defcheck commands also go.






\defcheck{\usepackage}would not error, you have\defcheck{\defcheck{\usepackage}}but#1should be a single token – David Carlisle Nov 21 '22 at 22:22\hboxand\csnameare primitives. – Ian Thompson Nov 21 '22 at 22:23\hboxhas no definition, it is a primitve – David Carlisle Nov 21 '22 at 22:23\defcheck{\a} \defcheck{\b} xxxwould show\aand\bthenxxxthen have a vertical space of2\baselineskip? – David Carlisle Nov 21 '22 at 23:46--> Undefined macro-- If it is undefined according to\ifdefined, then it is not a macro, it's just an undefined control sequence. If it is not undefined according to\ifdefined, then it might be an explicit character token or a macro or a primitive or an unexpandable non-primitive like a\chardef-token or a\toksdef-token or an implicit character or whatever... How to handle the case of the token in question not being a control sequence at all but an explicit non-active character token? – Ulrich Diez Nov 23 '22 at 19:08