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.

Encouraged by a super response to my previous question on a similar topic, I shall persevere with a similar line of inquiry. I think it would be super if, in my LaTeX editor, I could click on a macro name:

\blob{4}

and the cursor would jump to the corresponding definition:

\newcommand{\blob}[1]{...}

Do any LaTeX editors have this feature? I mainly use Aquamacs/AucTeX myself, but am interested in other editors too. I reckon it would be quite challenging to implement, because \blob might not just be defined in the current document - it might be:

  • in a master file that includes the current document,
  • in a slave file that the current document includes, or
  • in a package that the document uses.
share|improve this question
2  
... or defined in the latex format ... or be a tex primitive. depends how much you want to thrash your disk. Recently to answer a less than complete question on this site we had to do find . -name *.sty -exec grep zzzz \{\}\; to search every file in the input tree for a command to find out where it came from. You probably don't want to hide that behind a single keystroke. –  David Carlisle May 9 '13 at 14:56
3  
What is easier of course is to show the definition as that just means executing \show\blob –  David Carlisle May 9 '13 at 14:58
3  
You could collect all the package names and then execute something like this in the shell texdef -t latex -p longtable -p tabularx longtable which returns the definition, texdef has options to return instead the filename at which the definition was found (it has lots of options:-) –  David Carlisle May 9 '13 at 15:33
1  
Just yesterday I examined \bar as redefined by amsmath; would you believe me when I say that the redefinition by amsmath is performed by \@tempa{\bar}? Really, apart from very simple cases, this is impossible. –  egreg May 13 '13 at 8:36
1  
This feature would be awesome. –  Nicholas Hamilton May 15 '13 at 7:48
show 2 more comments

3 Answers

The general problem of finding where a command is defined has no viable solution. Macros can and do change their meaning; a typical example is \\. This simple document

\documentclass{article}
\begin{document}

\show\\

{\centering\show\\}

\begin{tabular}{c}
\show\\
\end{tabular}
\end{document}

gives the following output in the terminal window:

> \\=macro:
->\x@protect \\\protect \\  .
l.4 \show\\

?
> \\=macro:
->\ifhmode \unskip \else \@nolnerr \fi \par \@ifstar {\nobreak \@xcentercr }\@xcentercr .
l.6 {\centering\show\\
                      }
?
> \\=macro:
->{\ifnum 0=`}\fi \@ifstar \@xtabularcr \@xtabularcr .
l.9 \show\\

Thus, besides caching your entire preamble, a safe routine should also compile your document in order to get the actual meaning of a macro.

Another example. Suppose you load siunitx and do \show\SI; the output is

> \SI=\protected macro:
->\int_zero:N \l__xparse_processor_int \tl_set:Nn \l__xparse_args_tl {\SI code }\tl_set:Nn \l__xparse_fn_tl {\SI  }\__xparse_grab_D:w []{-NoValue-}\__xparse_grab_m_1:w \__xparse_grab_D:w []{-NoValue-}\__xparse_grab_m_1:w \l__xparse_args_tl .

the usefulness of which is very dubious.

Another example. Suppose you're using amsmath and want to access the definition of \bar; a \show command would print

> \bar=macro:
->\protect \mathaccentV {bar}016.

but you'll never find where this definition is performed, because there's no \def, \newcommand or any other similar command that does it: indeed in amsmath.sty you find (line numbers for convenience)

574 \def\set@mathaccent#1#2#3#4{%
575   \xdef#2{\@nx\protect\@nx\mathaccentV
576     {\@xp\@gobble\string#2}\hexnumber@#1#4}%
577 }
578 \def\@tempa#1{\@xp\@tempb\meaning#1\@nil#1}
579 \def\@tempb#1>#2#3 #4\@nil#5{%
580   \@xp\ifx\csname#3\endcsname\mathaccent
581     \@tempc#4?"7777\@nil#5%
582   \else
583     \PackageWarningNoLine{amsmath}{%
584       Unable to redefine math accent \string#5}%
585   \fi
586 }
587 \def\@tempc#1"#2#3#4#5#6\@nil#7{%
588   \chardef\@tempd="#3\relax\set@mathaccent\@tempd{#7}{#2}{#4#5}}
589 \@tempa{\hat}
590 \@tempa{\check}
591 \@tempa{\tilde}
592 \@tempa{\acute}
593 \@tempa{\grave}
594 \@tempa{\dot}
595 \@tempa{\ddot}
596 \@tempa{\breve}
597 \@tempa{\bar}
598 \@tempa{\vec}

and it's line 597 that performs the redefinition from the kernel's meaning

\mathaccent "7016\relax

that's assigned in fontmath.ltx by the high level instruction

\DeclareMathAccent{\bar}{\mathalpha}{operators}{"16}

Final example. You can't even guess the syntax of a command from its definition: here's the output of \show\makebox

> \makebox=macro:
->\leavevmode \@ifnextchar (\@makepicbox {\@ifnextchar [\@makebox \mbox }.
share|improve this answer
add comment

I'll mention TeXlipse, which includes this feature, though only partially. You can press F3 when the cursor is on a macro name, to jump to its definition. But...

  • TeXlipse does not take redefinitions via \renewcommand into account.

  • It does not understand macros defined by \def. Only \newcommand seems to work.

Nevertheless...

  • It does work if the macro is defined outside of the current file, in a package that you include via \usepackage.
share|improve this answer
add comment

You could try a regex (PCRE) similar to /(\\def|\\renewcommand|\\newkeycommand).*$cmdname/.

Hope this helps, I can't write any lisp for it right now, but it should be trivial to implement.

share|improve this answer
add comment

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.