6

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.
  • There's \meaning: \meaning\quad – clemens 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
  • 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
  • 2
    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. – user4686 Nov 18 '13 at 21:01
3

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. – user4686 Nov 18 '13 at 20:40
  • what do you mean by "printed onto a pdf file"? one pdf file for each macro definition? – user4686 Nov 18 '13 at 20:41
  • see my updated question. you have part of it already. – azetina Nov 18 '13 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.