LaTeX equivalent to R's `help foo`?

TeX isn't an object oriented programming language which can attach documentation data to classes and objects like in Python or most likely in R. There is no backtrace in which file/package/class a certain macro was defined or what its input specification is.

My way is (1) Google for unknown macros to find out in with package they are defined, (2) texdoc <package> + search for that macro, if this doesn't help (3) I look at the source code of the package, either by (3a) opening the source file or by (3b) using \show\macro on the macro in question. IMHO that's the closest you can get to something help macro. It shows you the definition of the macro which tells advanced users much of what they need to know.

In the case of macros with optional arguments this doesn't help much because \show only shows the special internal macros like \\macro and \macro (note the space in the macro name). I, of course, programmed myself a Perl+TeX script which shows me this definitions from the command line (texdef -p <package> <macro>). Publishing it is on my ToDo list.

However, if you only want the user description of the macro, i.e. how many and which arguments it needs, you are better of using an editor like Kile with command completion. This of course only works with commands the editor knows, which will be limited to the standard and some more common packages.


This is not really an answer, but way too long for a comment:

The problem with looking for documentation for the macro \foo is that it's (almost) impossible to know where \foo was defined. For example, \section is most likely defined by the document class your using, so if it's article, you would need to say texdoc article (or whatever the equivalent is in miktex), and if it's memoir, you use texdoc memoir. But if you are using some package that extends or changes the functionality of \section, the information from the article documentation might be insufficient, irrelevant or even misleading.

The best advice I can give is to keep your preamble clean, write a small comment about each and every package you use, and if you don't know why you \usepackage{foobar}, it's probably because you don't use it [1]. Something like

% babel - hyphenation rules and other localization stuff
\usepackage[danish]{babel}

% The geometry package makes it easy to set the margins
\usepackage{geometry}

If you at some point find out that you need to add the package mathrsfs to get a certain script math font (\mathscr), make sure to make a note of that near \usepackage{mathrsfs}. This way, you have a much better chance of knowing where to look for documentation.

[1] This is not quite true, but I believe it's important to have some idea about what each package is doing.


Unlike in R, in LaTeX you almost never use the built-in command line except when handling errors, and even errors are usually handled by LaTeX IDE's and editors. Almost every time you need help with some LaTeX command, you are inside a text editor. Perhaps that's why LaTeX does not provide the kind of help you are talking about. If there is any help for LaTeX commands, it is usually integrated into the editor you are using. For example in vim, if you have LaTeX support installed, all standard LaTeX commands (those that are available when using LaTeX with one of the standard classes and no additional packages) are directly accessible from vim help system, just as any vim commands. I can simply type :he \mbox and get help on the LaTeX \mbox command. Different editors have different capabilities, and different ways to access the help system.

As other answers point out, if you are using other classes and packages, it is usually hard to know where are your commands defined. With different classes and packages you get different commands, and sometimes a class or a package redefines one of the standard commands in its own way (for example, the exam class uses \part in a way that is completely different from its use in the standard LaTeX classes). That means that any help system would have to watch your file as you are typing it, and keep track of the packages that you are using. Again, the text editor is probably the best suited for that. Vim with LaTeX support does in a way keep track of your packages, and although it does not provide help on them, it will create a menu with the most important commands from each package it recognizes. In theory this could be extended so that the editor would load help files as you add more packages to your file, and extend its help system to cover those packages. It would probably be possible to write a program that would parse the .dtx files and try to extract information about commands defined in each of them. It probably would not be easy, and I would expect it to be somewhat unreliable.