9

I have always wondered how certain macros are made and I usually use the \show command to get a glimps of its definition. Is it possible to print the definition of a macro with a command, say \printmacro{\quad}, which will in turn create a .pdf (or .txt) file with the definition and the location (the file) in which it is defined?

Update:

The following features would be great:

  1. Create the \printmacro command with the option [printhere]. That is, it will emulate the behaviour of \meaning.
  2. Create the options printtopdf or printtotxt file.
  3. Note that all of these options should automatically indicate which file contains the definition of the command. If the exact location of the file which contains the command definition can be extracted, that would be great.
azetina
  • 28,884
  • There's \meaning: \meaning\quad – cgnieder Nov 18 '13 at 20:12
  • @cgnieder Interesting, first time am seeing it, but it does not print the command properly. I get -¿“hskip 1em“relax but its close to it. – azetina Nov 18 '13 at 20:14
  • Use \texttt{\meaning\quad}, or then \let\printmacro\texttt would also work. Providing file location would be difficult, period. – Werner Nov 18 '13 at 20:18
  • or add \usepackage[T1]{fontenc} if you don't want to use teletype – cgnieder Nov 18 '13 at 20:22
  • @cgnieder and @werner, those are wonderful ideas. Now how can I obtain the source file of each command? Say, Location: some.sty file. If the exact location in my TeX distribution can be printed it would be perfect. – azetina Nov 18 '13 at 20:25
  • 2
    you could use find and grep but it would be slow and hammer your disk, but if you use \meaning and it shows the definition either it is defined in the latex format or in one of the files loaded in the current document the full path name of each is shown in the log after ( so just look in those (and in latex.ltx which is the source of the latex format) – David Carlisle Nov 18 '13 at 20:52
  • @DavidCarlisle True but maybe like you said, have the command search for you directly in the most obvious files like latex.ltx and then in the loaded packages to run fewer searches and then test other packages somewhat like what TeX.SX does with the tags. – azetina Nov 18 '13 at 20:59
  • 3
    Here is what latexdef can do for you in command line console: xxx$ latexdef -s tabular % latex.ltx, line 5006: \def\tabular{\let\@halignto\@empty\@tabular}. But I usually do texdoc source2e and use the search of my pdf viewer. –  Nov 18 '13 at 21:01
  • \printmacro\section would probably disappoint you; and \printmacro\makebox will make you cry in dismay. Printing the definition of such macros is rather pointless, because you'd have to restart adding the definition of \@startsection only to discover the beauties of \@sect, \@ssect, \@xsect, … Looking for the definition in latex.ltx during a LaTeX run is next to impossible (unless you want to scan the file line by line). – egreg Nov 18 '13 at 22:23
  • Related: \meaning is only for display, you can't get the definition itself (as a sequence of TeX tokens). See also tex core - How to get number of arguments in a macro? - TeX - LaTeX Stack Exchange – user202729 Dec 10 '21 at 07:01

1 Answers1

7

I think most of your concerns are addressed by the very useful texdef and latexdef. But you seem to want a way to print each definition to a file from a .tex input. I usually use \texttt{\meaning\stuff} but the following is perhaps what you were referring to:

\documentclass{article}

\newwrite\definition

\def\printmacro #1{%
    \begingroup\escapechar=-1
    % better as .txt file
    \immediate\openout\definition=\jobname-meaning-\string#1.txt
    \endgroup
    \immediate\write\definition{\meaning#1}%
    \immediate\closeout\definition
}

% or perhaps you prefer:

\def\printmacro #1{%
    \begingroup\escapechar=-1
    \immediate\openout\definition=\jobname-meaning-\string#1.txt
    \endgroup
    (printing definition of \texttt{\string#1} to file \texttt{\escapechar-1
    \jobname-meaning-\string#1.txt})
    \immediate\write\definition{\meaning#1}%
    \immediate\closeout\definition
}

\def\testmacro #1#2#3{$#2^{#3}$ (#1)}


\begin{document}

\printmacro\testmacro % can also be used in preamble

\printmacro\tabular
\expandafter\printmacro\csname @tabular\endcsname

\begin{tabular}{c}
  Hello\printmacro\\
\end{tabular}

\end{document}

writedef

For pdf output however one would need shell-escape enabled (I think).

  • 1
    How can I get to print the location of where it is defined? The pdf file produced does not have the macro printed onto a pdf file? This seams to answer the part where you write the macro to a txt file. Thanks – azetina Nov 18 '13 at 20:38
  • the meaning of macro \macro is saved in file filename-meaning-macro.txt in the same repertory as file filename.tex which contains the code above. –  Nov 18 '13 at 20:40
  • what do you mean by "printed onto a pdf file"? one pdf file for each macro definition? –  Nov 18 '13 at 20:41
  • see my updated question. you have part of it already. – azetina Nov 18 '13 at 20:49