[Tex/LaTex] Where to find LaTeX commands source code

documentationsourcecode

I would like to know where can I find the definition of an original command. That is, let's say I want to redefine the command \ref so when I type it, instead of showing just the number of the label of input, also says 'figure' or 'table' or whatever the \label refers to (This may be a silly example but try to see my point).

Well, I imagine how I could modify \ref given the code, but as I don't have deep knowledge of LaTeX, or I am lazy, I can't figure how to renew it out of thin air.

So, is there anywhere one may have a look at the original code and well change this and that to work this sort of situations out?

Thanks to everyone for your attention.

Best Answer

There are a number of ways you can go about solving this problem, mostly depending on how much detail you would like.

Getting the raw code definition of any command is trivial; there is a TeX primitive \meaning, which will expand to the current one-level expansion of the macro (that is, it will not attempt to expand those macros inside of it). If you are using a complicated package setup or if the definition of the macro is changing, this is the most reliable way to go. Otherwise, you can save some time with the texdef Perl script:

$ texdef -t latex large

\large:
\long macro:->\@setfontsize \large \@xiipt {14}

Note that this only parses the output of TeX as seen below, but it can be a huge timesaver for simple queries.

meaning output

See its man or info page for documentation on the tool, or use texdoc texdef, which brings us to the next level.

With TeX being a typesetting language, it just so happens that many package authors/teams are also very good, thorough writers. The texdoc tool will take its argument and search all of the documentation that is available to it for your term. Now, it doesn't do a search in depth but in breadth; it will not search the contents of all the documentation available to it. (That would take forever!) Most of my needs are met with texdoc; normally you can provide the package name that your command belongs to and violà: your documentation opens in your default reader.

The documentation for the LaTeX kernel is also available with texdoc source2e (and a pared-down, unfinished macros2e); as for the detail of this documentation—well—it's turtles all the way down. See man texdoc, info texdoc, and—you guessed it—texdoc texdoc for more information on this tool.

If texdoc yet does not satisfy you, then the last resorts are printed books and Google, where printed books generally only compile information already available for free.