1

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 ?

  1. 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).
  2. 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.

projetmbc
  • 13,315
  • 3
    Not clear what the question is. If you have the package, you have the source. There is no concept of a 'compiled' package in TeX. – Alan Munn May 26 '18 at 21:38
  • 1
    If you've installed TeX Live then you have all the source code ... To find a specific file you can use kpsewhich, e.g. kpsewhich book.cls will give you the path to the book class file. But I'm not sure if that is what you're asking for. – Torbjørn T. May 26 '18 at 21:38
  • You will also have to check every macro: they could be passing something to \csname as a braced argument or even a delimited one ... – Joseph Wright May 26 '18 at 21:59
  • @JosephWright For the \csname that will be done in a second time. – projetmbc May 26 '18 at 22:05
  • @egreg I'm looking for source of package that can be installed using the TeX Live Utility. – projetmbc May 26 '18 at 22:05
  • Well just grep/findstr/... over <install root>/<year>/texmf-dist/tex then ... – Joseph Wright May 26 '18 at 22:06
  • 1
    @projetmbc If I issue ls /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 \section in article.cls is “interestingly cryptic”? – egreg May 26 '18 at 22:11
  • @egreg I do not care about the definition. I am just concern in trying to find which packages are really used by a (La)TeX file. – projetmbc May 26 '18 at 22:18
  • the latex log lists every file input by that document, you do not need tlmgr to get teh list of files used by a document 9or you could use the --recorder option 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
  • You do know that the same macro is often defined in different ways by different packages/classes, right? So just because a package defines \section, doesn't mean that it defines it in the right way. Moreover, a package might redefine one of the macros on which \section depends or might redefine \section to 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:47
  • @cfr The compiler does the job but it does not communicate this to the user. – projetmbc May 27 '18 at 09:40
  • 1
    even after the edit this question is completely unclear. what do you mean by It 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. Similarly Where can I find the source codes has already been answered in comments where the installed packages may be found. the final comment about \csname does not seem related to package names at all???? – David Carlisle May 27 '18 at 10:28
  • 1
    @projetmbc what do you mean by The compiler does the job but it does not communicate this to the user it 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
  • @DavidCarlisle Let's suppose that I indicate \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
  • @DavidCarlisle I talk about \csname so 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
  • 1
    @projetmbc latex is not a compiler and has not even loaded the body of the document by the time it sees \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
  • In final, there is no way to know the parts used by the file. By compilation, I was talking about the building for example of a PDF file from a LaTeX file : going from one language to another, going from LaTeX language to the PDF one, can be seen as a compilation (even if the term is traditionally used for language machine). – projetmbc May 27 '18 at 11:21
  • I have to go to praticiens sport and then I have to work on my lessons for totommirow. Tell when you can tomorrow ? – projetmbc May 27 '18 at 13:30
  • It seems to me that you can extend your current approach to a possible solution: "if the compilation fails, my script will add packages imported via \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
  • Yes that is what I want to do if a previous analysis fails. This approach being time consuming, I would like to minimize the use of this "brutal" tests. – projetmbc May 28 '18 at 13:41
  • I’m afraid that the action of finding out which packages are *actually* needed to compile a given input file requires (artificial) intelligence — and keen intelligence, too! If writing a script like the one you are planning to write was not such a hard task, then, well, somebody would have already written it many years ago! :-) – GuM Sep 09 '19 at 10:34

1 Answers1

1

Following the comments, I don't know if it's a good idea to classify all the macros by package...a good option is what was commented by Marijn.

Maybe you don't need to reinvent the wheel from scratch, you can read a little about texliveonfly (https://tex.stackexchange.com/a/463842/7832) whose repository is located at (https://github.com/maphy-psd/texliveonfly) It's a little out of date, but it's a good starting point.

I really don't think you can have a definitive solution to create the list of necessary packages, the collections in TeXLive install more than one package, and some packages have circular dependencies with others, but, you can try and get the best possible result (perhaps create a list of known packages using the collections provided by TeXLive which have dependency dependencies, or something like that).

For trial and error tests, I recommend using travis (https://github.com/PHPirates/travis-ci-latex-pdf), we don't want to hinder our local distribution and you can try a really minimal distribution as many times as necessary.

Best of luck with your script.