# Where to find LaTeX commands source code?

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.

-
(For the silly example the cleveref package exists. hyperref also defines a few ref commands for this purpose.) For the definition of \ref, you can take a look at latex.ltx. But any package can overwrite this itself so you may be better off using \show/\meaning and/or latexdef (see texdef). –  Qrrbrbirlbel Oct 15 '13 at 21:04
On the specific 'auto-magic' label/ref case, see package cleveref (\cref) and facilities in the hyperref package (\autoref). –  Joseph Wright Oct 15 '13 at 21:04
One of the amazing features of LaTeX is that there are hundreds of packages providing facilities for many tasks; if you need something not really very special, it's likely that somebody has already solved the problem and issued a package for it. –  egreg Oct 15 '13 at 21:20
For the raw definition (no documentation), there is always the texdef tool: texdef -t latex \ref. As for redefining it, simply \renewcommand to a copy of the definition in your preamble and make your necessary changes. –  Sean Allred Oct 15 '13 at 21:28

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.

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.

-