Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When using renewcommand, sometimes I wish to first check what the original source code for a particular command, such as chapter was before I change it, so that I can be certain that I do not make unintended changes. When I have a lot of packages loaded, sometimes looking through the source code of a package to find the answer is not so simple. Is there a way to print out the source code used to define a particular command?

share|improve this question
4  
1  
1  
Related tex.stackexchange.com/questions/4327/… –  N.N. Jan 30 '12 at 18:31

3 Answers 3

up vote 21 down vote accepted
  • Use \meaning, \show to get the meaning of a macro. (See TeXbook or TeX by Topic)

  • Use \the, \showthe to get the value of registers. (See TeXbook or TeX by Topic)

  • Use \tracingcommands, \tracingmacros (See TeXbook or TeX by Topic) and trace package to get more information in the log file.

  • Use eTeX's \ifdefined, \ifcsname (See eTeX's manual) or LaTeX command \@ifundefined (See source2e) or \ifx\foo\undefined trick to check whether a macro is defined.

  • Use LaTeX command \CheckCommand (described in clsguide) to check the definition of a LaTeX macro.

share|improve this answer

Yes, using \show\mycommand will print the macro definition of \mycommand to the console (your .log file).

For example, compiling

\documentclass{book}
\begin{document}
test
\show\chapter
\end{document}

outputs

> \chapter=\long macro:
->\if@openright \cleardoublepage \else \clearpage \fi \thispagestyle {plain}\gl
obal \@topnum \z@ \@afterindentfalse \secdef \@chapter \@schapter .

to the console. Note that the alignment of the output is not the same as when it is typed in code. As such, it is sometimes easier seeing the exact structure (if properly indented) when viewing the source directly. Here is the source entry from book.cls for \chapter:

\newcommand\chapter{\if@openright\cleardoublepage\else\clearpage\fi
                    \thispagestyle{plain}%
                    \global\@topnum\z@
                    \@afterindentfalse
                    \secdef\@chapter\@schapter}

Also note that using \show only shows one level of expansion for the given macro. Using the above as example, one would have to issue (say) \show\@chapter and \show\@schapter (with the appropriate category code modification via \makeatletter and \makeatother pairs) to see the meaning of subsequent calls within \chapter.

\meaning can be used in a similar context.


From the TeX Book (p 10):

You can display the meaning of a control sequence while running TeX. If you type \show\cs where \cs is any control sequence, TeX will respond with its current meaning. For example, \show\input results in > \input=\input., because \input is primitive. On the other hand, \show\thinspace yields

> \thinspace=macro:
->\kern .16667em .

This means that \thinspace has been defined as an abbreviation for \kern .16667em. By typing \show\kern you can verify that \kern is primitive. The results of \show appear on your terminal and in the .log file that you get after running TeX.

share|improve this answer
4  
Addon: Note: If \show\foo only results in \protect\foo it's a protected command. You may either show the definition of that by \expandafter\show\csname foo \endcsname or the tricky {\let\protect\show\foo} (see tex.stackexchange.com/questions/19746/cunning-latex-tricks). –  Schweinebacke Dec 3 '11 at 10:34
    
This is awesome. I had no idea you could do this! –  qubyte Dec 3 '11 at 11:30

You can use texdef to print definitions in a terminal. To use it with LaTeX definitions you have to either call it with the option -t latex or use the corresponding alias latexdef (if the alias is defined on your system).

To check the definition for \chapter you can issue texdef -t latex -c book chapter. The reason that you have to invoke it with the option -c book is that the \chapter you seem to be looking for is defined in that class. To also check in which file the definition is to be found add the option -f or -F for the full path. On my system texdef -t latex -c book -F chapter returns

\chapter first defined in "/usr/local/texlive/2011/texmf-dist/tex/latex/base/book.cls".

\chapter:
\long macro:->\if@openright \cleardoublepage \else \clearpage \fi \thispagestyle {plain}\global \@topnum \z@ \@afterindentfalse \secdef \@chapter \@schapter

If you want to see the available options for texdef or learn more about it you can access its documentation with texdoc texdef.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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