9

I am preparing a document for my university and need to insert a list of used packages and their corresponding versions/dates in the appendix of the document.

One possibility is to just copy the output of \listfiles from the logs and format it by hand before actually handing in the final version of the document. But that feels error prone...

So is there a way to include such a list, table or whatever automagically?

NobbZ
  • 1,243

2 Answers2

14

The macros I propose just show the information for the document class and the packages, so only those with extension .cls or .sty, discarding the other files.

\documentclass{article}
\usepackage{longtable,array}

\listfiles % initialize \AtEndDocument{\printfilelist}

\ExplSyntaxOn \NewDocumentCommand{\printfilelist}{} { \cleardoublepage \section*{Used ~ packages} \setlength{\LTleft}{0pt} \pagestyle{empty} \footnotesize \begin{longtable}{@{} >{\ttfamily}l >{\raggedright}p{.6\textwidth} @{}} \nobbz_print_filelist: \end{longtable} }

\tl_new:N \l_nobbz_filelist_body_tl

\cs_new_protected:Npn \nobbz_print_filelist: { \clist_map_inline:cn { @filelist } { \nobbz_print_fileinfo:n { ##1 } } \tl_use:N \l_nobbz_filelist_body_tl }

\cs_new_protected:Npn \nobbz_print_fileinfo:n #1 { \regex_match:nnT { .(sty|cls)\Z } { #1 } { \nobbz_print_pkginfo:n { #1 } } }

\cs_new_protected:Npn \nobbz_print_pkginfo:n #1 { \tl_put_right:Nx \l_nobbz_filelist_body_tl { \exp_not:n { #1 } & \exp_not:v { ver@#1 } \exp_not:N \tabularnewline } } \ExplSyntaxOff

\begin{document}

Some text

\end{document}

enter image description here

A variation for separating the file date from the information:

\documentclass{article}
\usepackage{longtable,array,booktabs}

\listfiles % initialize \AtEndDocument{\printfilelist}

\ExplSyntaxOn \NewDocumentCommand{\printfilelist}{} { \cleardoublepage \section*{Used ~ packages} \setlength{\LTleft}{0pt} \footnotesize \begin{longtable}{@{} >{\ttfamily}l l >{\raggedright}p{.6\textwidth} @{}} \toprule \multicolumn{1}{@{}l}{\bfseries File~name} & \multicolumn{1}{c}{\bfseries Date} & \bfseries File~info \tabularnewline \midrule \endhead \bottomrule \endfoot \nobbz_print_filelist: \end{longtable} }

\tl_new:N \l_nobbz_filelist_body_tl \tl_new:N \l_nobbz_fileinfo_tl

\cs_new_protected:Npn \nobbz_print_filelist: { \clist_map_inline:cn { @filelist } { \nobbz_print_fileinfo:n { ##1 } } \tl_use:N \l_nobbz_filelist_body_tl }

\cs_new_protected:Npn \nobbz_print_fileinfo:n #1 { \regex_match:nnT { .(sty|cls)\Z } { #1 } { \nobbz_print_pkginfo:n { #1 } } }

\cs_new_protected:Npn \nobbz_print_pkginfo:n #1 { \tl_set_eq:Nc \l_nobbz_fileinfo_tl { ver@#1 } \tl_replace_all:Nnn \l_nobbz_fileinfo_tl { & } { & } \tl_replace_once:Nnn \l_nobbz_fileinfo_tl { ~ } { & } \tl_put_right:Nx \l_nobbz_filelist_body_tl { \exp_not:n { #1 } & \exp_not:V \l_nobbz_fileinfo_tl \exp_not:N \tabularnewline } } \ExplSyntaxOff

\begin{document}

Some text

\end{document}

enter image description here

egreg
  • 1,121,712
  • This solution lists all dependencies. Is there a way to get only those that are explicitly included in the document's \usepackage statement? cc @touhami – Erwann Jun 14 '20 at 19:47
  • 1
    @Erwann Short answer: no. One might think to patch \usepackage. – egreg Jun 14 '20 at 20:33
  • The solution should be updated with the solution of question https://tex.stackexchange.com/questions/479650/l3regex-sty-not-found replace \usepackage{xparse,l3regex,longtable,array,booktabs} by \usepackage{xparse,longtable,array,booktabs} then the solution works. – c05772 Mar 06 '24 at 17:30
  • @c05772 Yes, thanks! Done. – egreg Mar 06 '24 at 18:06
  • @c05772 What kind of error? – egreg Mar 06 '24 at 21:00
  • Using the second solution on a very large document (638 pages) I get an error at the last line when compiling: Extra alignment tab has been changed to \cr. \printfilelist But the pdf is created and the list (3.5 pages) appended, correctly, at the end of the document. I wonder if the extra tab is not coming from the fact that some packages have no date and/or no file info or something non standard (e.g. package yfonts.sty) – c05772 Mar 06 '24 at 21:24
  • I confirm that if you add the line \usepackage{yfonts} to the solution, it fails (but you can have the pdf with a return..) so this package is the culprit :-( and of course, if you check the file list you find yfonts.sty 2019/04/04 v1.4 (WaS&NP) ! – c05772 Mar 06 '24 at 21:45
  • @c05772 I added a protection against this in the second implementation – egreg Mar 06 '24 at 22:06
  • @egreg, thanks, it works. I am just baffled by this type of bugs: in my youth I learned with an example of similar bugs in a program having faithfully worked for more than 3 years, ... it was coming coming from a leap year (in COBOL!). This question is 8 years old, how could you imagine testing all the packages ? – c05772 Mar 06 '24 at 22:25
  • @c05772 I didn't test all packages, of course. The package info was never meant to be pretty printed, I guess. An & in a console message has no problem. – egreg Mar 06 '24 at 22:35
2

Another solution

\documentclass{article}
%-- just for the example
\usepackage[a4paper]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
%------------------------
\newlength\mtl
\makeatletter
\newcommand{\mtlistfiles}{%
\par\noindent
%%-- for measuring
\@for\@currname:=\@filelist\do{%
\setbox0=\hbox{\@currname}%
\ifdim\wd0>\mtl\relax\mtl=\wd0\fi}%
%%-- formating list
\@for\@currname:=\@filelist\do{%
\makebox[\mtl][l]{\@currname}
     \expandafter\ifx\csname ver@\@currname\endcsname\relax
     \else\@spaces\csname ver@\@currname\endcsname\fi
     \par\noindent}}
\makeatother
\listfiles
\begin{document}
bla bla
\mtlistfiles
\end{document}

enter image description here

touhami
  • 19,520
  • The example works but as above if you add the package yfonts, with the line \usepackage{yfonts} (line 6 for example) it does not work anymore. The reason is that in the file info field yfonts.sty 2019/04/04 v1.4 (WaS&NP) there is an ampersand &! This is a nice example of bugs for a programming course, how can you imagine that? – c05772 Mar 06 '24 at 22:00