22

We often encounter multiple definitions of certain commands/macros. Then, we often get the error, e.g.: ! LaTeX Error: Command \P already defined. Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation. Type H for immediate help. ...

l.225 \newcommand{\P}{{\mathcal P}}

When we get such an error, is there anyway to know which package or file predefined that particular command/macro? I'm talking about general cases, not the particular case of '\P' listed above. It is quite cumbersome if we have to go through all possible files via 'grep' manually. I hope that there's a way to see which file already defined the command that produces the conflict/error.

Thanks a lot!

BVP

2
  • 3
    grep is your friend, sorry:-) Jun 19, 2014 at 18:34
  • 2
    For people (like me) who come here later: \P is the command to get the paragraph symbol. Jan 22, 2016 at 18:57

2 Answers 2

17

You can add

\show\P

to your document as in

 \show\P
\documentclass{article}
 \show\P
\usepackage{graphics}
 \show\P
\usepackage{foo}
 \show\P
\usepackage{bar}
 \show\P
\begin{document}
 \show\P

This will make the TeX stop and show the definition of \P at each step, if it is already defined in the format the first one will show the definition, otherwise it will show undefined until the package that defines it is loaded. (The definition might be delayed with \AtBeginDocument which is why I put the final \show at that point.

3
  • When I did \show\vref, I get: > \vref=macro: \ ->\protect \vref . \ l.234 \show\vref Is there a solution to seeing the underlying definition?
    – Gus
    May 26, 2021 at 22:18
  • 2
    \ShowCommand (if you have a recent latex) @Gus May 26, 2021 at 22:21
  • According to tex.stackexchange.com/a/47372/1362 , it needs to be "The LaTeX kernel from the 2020-10-01". Thanks!
    – Gus
    May 26, 2021 at 22:28
4

The command-line utility latexdef shows not only the definition, but also the location of the definition.

For example, I wanted to see the definition of \url in my document. To be sure, this is not defined by default:

$latexdef --find \url

\url:
undefined

$

but it is defined by hyperref:

$latexdef --package hyperref --find \url
\url first defined in "/usr/local/texlive/2020/texmf-dist/tex/latex/url/url.sty".

\url:
macro:->\protect \url

\url  first defined in "/usr/local/texlive/2020/texmf-dist/tex/latex/hyperref/hyperref.sty".

\url :
macro:->\hyper@normalise \url@

$

and that package is loaded by some document classes:

$latexdef --class tufte-book --find \url
\url first defined in "/usr/local/texlive/2020/texmf-dist/tex/latex/url/url.sty".

\url:
macro:->\protect \url

\url  first defined in "/usr/local/texlive/2020/texmf-dist/tex/latex/hyperref/hyperref.sty".

\url :
macro:->\hyper@normalise \url@

$

See also https://tex.stackexchange.com/a/17840/1362

3
  • wow! this is amazingly useful. Jun 3, 2022 at 23:08
  • How do you use this in a document? I don't understand if you're doing command line how do you get what a command is in the middle of running a tex file?
    – Hao S
    Oct 30, 2023 at 5:22
  • @HaoS, this answer doesn't address that use case, as far as I can tell.
    – Gus
    Feb 19 at 21:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .