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?
- 13,603
- 23
- 116
- 219
3 Answers
Use
\meaning,\showto get the meaning of a macro. (See TeXbook or TeX by Topic)Use
\the,\showtheto get the value of registers. (See TeXbook or TeX by Topic)Use
\tracingcommands,\tracingmacros(See TeXbook or TeX by Topic) andtracepackage to get more information in the log file.Use eTeX's
\ifdefined,\ifcsname(See eTeX's manual) or LaTeX command\@ifundefined(Seesource2e) or\ifx\foo\undefinedtrick to check whether a macro is defined.Use LaTeX command
\CheckCommand(described inclsguide) to check the definition of a LaTeX macro.
- 77,365
-
\show\bigfailed for me (TeXLive 2018 on macOS). The solution based ontexdefworked, however (see below). – Marius Hofert Mar 08 '19 at 15:53 -
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\cswhere\csis any control sequence, TeX will respond with its current meaning. For example,\show\inputresults in> \input=\input., because\inputis primitive. On the other hand,\show\thinspaceyields> \thinspace=macro: ->\kern .16667em .This means that
\thinspacehas been defined as an abbreviation for\kern .16667em. By typing\show\kernyou can verify that\kernis primitive. The results of\showappear on your terminal and in the.logfile that you get after running TeX.
- 603,163
-
9Addon: Note: If
\show\fooonly results in\protect\fooit's a protected command. You may either show the definition of that by\expandafter\show\csname foo \endcsnameor the tricky{\let\protect\show\foo}(see http://tex.stackexchange.com/questions/19746/cunning-latex-tricks). – Schweinebacke Dec 03 '11 at 10:34 -
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.
- 36,163
-
This is the only way that worked for me for
\bigand\bigl, for example. – Marius Hofert Mar 08 '19 at 15:51
\showand\meaning. See how do i use show and equivalent of show to display the latex code in the document – Peter Grill Dec 03 '11 at 04:11