Goal
I'm working on a Python script that will try to find the packages really used in a (La)TeX file or a piece of a (La)TeX file. I can't just keep the packages indicated via \usepackage because if I am interesting in a part of a (La)TeX file, only some packages will be used.
I am not looking for an exact solution. In other words, my script will try to do the best, and trying to do the best will be good enough as Radiohead could say in an Optimistic day.
If there is only one part of packages founded, typically if a new LaTeX file is built with only the packages found by my script and the compilation failed, then my script will try brutally to add the packages unkept that are imported via \usepackage in the original file. I will try do clever things later but not right now.
Method used
In a (La)TeX file or a piece of a (La)TeX file, I look for macros used and then I search in a kind of BDD, maybe a NoSQL one, build by my script.
So I need to associate a name of macro to packages, one name can be associated to several packages.
For the moment, I am not concerned with user's packages (that will be done later).
What am I looking for ?
- Can I know the packages really used by a LaTeX compiler ? It seems that in the log file all packages imported by the LaTeX file are used by the compiler. That looks normal but if the compiler can know which packages are really used, I will happy to use that (because my script will have no reason to birth).
- Where can I find the source codes where the macros are defined in packages available in the TeX Live Utility ?
PS: I know the use of \csname that complexifies the search but for the moment I will try to work without this hoping that searching for hard coding names of macros will be enough.
kpsewhich, e.g.kpsewhich book.clswill give you the path to thebookclass file. But I'm not sure if that is what you're asking for. – Torbjørn T. May 26 '18 at 21:38\csnameas a braced argument or even a delimited one ... – Joseph Wright May 26 '18 at 21:59\csnamethat will be done in a second time. – projetmbc May 26 '18 at 22:05grep/findstr/... over<install root>/<year>/texmf-dist/texthen ... – Joseph Wright May 26 '18 at 22:06ls /usr/local/texlive/2018/texmf-dist/source/latex | wc -l, the shell returns 1199. Good luck. Do you really would like to classify thousands of macros? And what about when you discover that the definition of\sectioninarticle.clsis “interestingly cryptic”? – egreg May 26 '18 at 22:11tlmgrto get teh list of files used by a document 9or you could use the--recorderoption to get the file list on a different format. Your question is not really clear enough to answer. – David Carlisle May 26 '18 at 22:39\section, doesn't mean that it defines it in the right way. Moreover, a package might redefine one of the macros on which\sectiondepends or might redefine\sectionto depend on a new macro, which will not ever be mentioned in the document's source. There is no way to determine the required packages by looking at the *macros* in a document. At least, I cannot see how this could possibly work. – cfr May 27 '18 at 02:47It seems that in the log file all packages imported by the LaTeX file are used by the compiler.? For any document the files tha are input are listed in the log, it is not just that seems to be the case, it is the case. SimilarlyWhere can I find the source codeshas already been answered in comments where the installed packages may be found. the final comment about\csnamedoes not seem related to package names at all???? – David Carlisle May 27 '18 at 10:28The compiler does the job but it does not communicate this to the userit seems that you have an idea of what is happening that does not match what actually happens, what "job" do you have in mind that latex does that is not logged here? – David Carlisle May 27 '18 at 10:31\usepackage{tikz}but I don't use TiKZ in my LaTeX file. Does the compiler "knows" that or just it imports the package brutally ? – projetmbc May 27 '18 at 10:38\csnameso as to not have comments saying that I can't find easily every names of macros in sources. Having only a part of names of macros should do the job in my case. – projetmbc May 27 '18 at 10:46\usepackage{tikz}it never holds the whole document in memory.\usepackage{tikz}is just\input{tikz.sty}and it happens at that point. – David Carlisle May 27 '18 at 11:16\usepackage" (slightly paraphrased). You can start with all included packages, verify that it compiles without error, delete one, see if it still compiles, if yes, the package is not really used, if no, the package is used and it should be kept - repeat for each package. Of course this is not fail-safe, i.e., a package can modify the result of an existing macro without changing the syntax, but as a 'good enough' it may be sufficient. – Marijn May 28 '18 at 08:53