如何找出能够编译包含给定\begin{X}...\end{X}
环境的 tex 文件的包?
答案1
我想出了这个宏\findpackagebycommand
:
\makeatletter
% patched command of loading a package
\def\find@load#1[#2]#3[#4]{%
% load the package
\find@fileswith@pti@ns{#1}[#2]{#3}[#4]%
% check if commands exists now
\ifcsname\find@command\endcsname
\typeout{Package #3 introduces command \find@command.}%
% command has been found, revert to original version without checks
\let\@fileswith@pti@ns\find@fileswith@pti@ns
\else
% somehow, \@fileswith@pti@ns is restored after loading a package
% thus, patch it again
\let\@fileswith@pti@ns\find@load
\fi
}%
\newcommand*{\findpackagebycommand}[1]{%
% using this multiple times - esp. when the package has not been found yet -
% will break things. Thus, check first that \find@command has never been defined before
\ifx\find@command\undefined
\def\find@command{#1}%
% first, check if this command is already defined
\ifcsname\find@command\endcsname
% in this case, just issue a warning and do nothing
\@latex@warning@no@line{Command \find@command \space is already defined}%
\else
% overwrite the internal \@fileswith@pti@ns command, which does the actual loading
% \@fileswith@pti@ns is used internally by \usepackage and \RequirePackage
\let\find@fileswith@pti@ns\@fileswith@pti@ns
\let\@fileswith@pti@ns\find@load
\fi
\else
% used multiple times - prevent and give a warning
\@latex@warning@no@line{You can use \protect\findpackagebycommand \space only once}%
\errmessage{Invalid use of command findpackagebycommand.}%
\fi
}
\makeatother
\usepackage
如果在加载包后定义了命令/环境的名称,它会用简单测试覆盖所使用的内部命令。
这并不完美,并且存在一些缺点:
- 如果您使用 ,它不会分离软件包
\usepackage{foo,bar,baz}
。它只是向您显示此命令由其中一个软件包加载。 - 它不能多次使用。需要更多逻辑才能同时搜索多个命令。
在加载任何其他包(以及文档类,如果您愿意的话)之前,像这样使用它:
\makeatletter
\newcommand*{\findpackagebycommand}[1]{
… like above …
}
\makeatother
\findpackagebycommand{lstlisting}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{listings}
\usepackage[T1]{fontenc}
\begin{document}
Test
\end{document}
包的名称将显示在 latex 输出中,行“包列表引入命令 lstlisting。”:
This is pdfTeX, Version 3.14159265-2.6-1.40.17 (TeX Live 2016/Arch Linux) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
(./packagetest.tex
[…]
(/usr/share/texmf-dist/tex/latex/base/inputenc.sty […])
(/usr/share/texmf-dist/tex/latex/amsmath/amsmath.sty […])
(/usr/share/texmf-dist/tex/latex/listings/listings.sty
(/usr/share/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/share/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/share/texmf-dist/tex/latex/listings/listings.cfg))
Package listings introduces command lstlisting.
(/usr/share/texmf-dist/tex/latex/base/fontenc.sty
(/usr/share/texmf-dist/tex/latex/base/t1enc.def))
[…]
(当通过间接包加载(例如\define@key
由某些包加载定义keyval
)加载时,它会向后打印已加载包的链,但在不同的行。这可以改进。)