I am not an advanced latex user, and I'm often questioning in which package a given known command is defined. For example, where are the symbols \sfrac, \mathbb and \substack defined? I believe answering this question is something natural for accustomed latex users. For me, I keep forgetting that \mathbb is defined in the package amssymb.
I wrote a small command line utility in Bash to try to find this for me. It scans through my local latex installation files and attempts to find where a given command is defined by looking for lines with likely command definitions. This search is done in the internal sty, tex and cls files.
To do this, the script employs a simple heuristic. Say you want to find where \sfrac is defined. The script looks for lines containing one of "def", "decl", "cmd", "command", "new" somewhere before the command name, and it takes this as good enough for "likely command definition". To search a file.sty for possible definitions of \sfrac, we'd do:
grep -Ei "(def|decl|cmd|command|new).*\\\\sfrac[^a-zA-Z]" file.sty | grep "\\\\sfrac[^a-zA-Z]"
This is a "strict" mode, in which we're less willing to accept false positives. If you don't exactly remember the capitalization of the command and/or want to allow for prefix/suffix searches, you could consider a looser search:
grep -Ei "(def|decl|cmd|command|new).*sfrac" file.sty
Variations of this are possible, of course. I'm going for the more strict search myself. A more interesting direction would be improving the search heuristics. For example, we could count how many '\' there are before the searched command. You should get the one for the command, and another one for its respective \def or \newcommand, and no more (right?). It seems like a valid heuristic to look at. The adapted grep search would be:
grep -Ei "(command|cmd|new|def|decl)[^\\\\]*\\\\sfrac[^a-zA-Z]" file.sty | grep "\\\\sfrac[^a-zA-Z]"
Latex offers great flexibility and dynamicity for defining new commands, therefore such heuristics based on simple source analysis won't fully work, but they help with many common simpler cases.
To turn this type of grep search into a script, we can simply loop through the .tex, .sty and .cls files within the latex distribution, and apply the filter at each file. This batching can be done through find.
The current version of the script I'm running is in https://pastebin.com/FSdQtfVt (raw: https://pastebin.com/raw/FSdQtfVt). I intend to make a small github repository for this later on. However, first, I'll see if I can find already made tools that do a better job at this.
This came about while I was trying to remember where \mathbb is defined. Of course, a simple web search solves this, but in my third search (in different days, hehe), and after searching for many commands (and not remembering where they where), I thought enough is enough and decided to look for a tool to do this. Not initially finding one and having this idea, I decided to write this script.
pedro@pedro-Inspiron-15-3520:~$ tex-which substack Looking for \substack in /usr/share/texlive/texmf-dist/tex/ ... (done) Found possible symbol definition for \substack in files: amsmath-2018-12-01.sty amsmath.sty Full results: amsmath-2018-12-01.sty in latex-dev/amsmath \newcommand{\substack}[1]{\subarray{c}#1\endsubarray} amsmath.sty in latex-dev/amsmath \DeclareRobustCommand{\substack}[1]{\subarray{c}#1\endsubarray} amsmath-2018-12-01.sty in latex/amsmath \newcommand{\substack}[1]{\subarray{c}#1\endsubarray} amsmath.sty in latex/amsmath \DeclareRobustCommand{\substack}[1]{\subarray{c}#1\endsubarray}