Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

As others certainly do, I generally use a template for my papers, which contains everything I usually need (packages, macros, ...) to typeset my documents. However, my needs sometimes vary, and in the end I find myself including packages that I did not need.

What I generally do in the preamble is \usepackage{P} % R, where R is the reason (e.g. set of commands) for including package P. This then allows me to check that R applies to my document, and remove P if not, but this is still a rather tedious process. Does anyone know of a script that checks for unneeded packages or commands in a document?

share|improve this question

3 Answers 3

no not possible, unless you wrote a script which collects all commands and environments from the packages and compare it with your document.

share|improve this answer

If you have Linux (or some other system where to run bash and gnu-version on grep), make a script like this:

#!/bin/sh

> notneeded
> needed

fgrep '\usepackage' $1 | cut -f 2 -d '{' | cut -f 1 -d '}' |
while read a; do
    egrep -v "\\usepackage(\[.+\])?\{$a\}" $1 > tmp.tex
    latex -halt-on-error tmp.tex &&
        echo $a >> notneeded || echo $a >> needed
done

If you save this as unused.sh, run it with unused.sh mydoc.tex and you will get file called notneeded.

This breaks if you have, say, two \usepackage in same line. That can be corrected with -o to grep. However, more complicated situations are usually better done with perl.

E: There is more to think. First, there might be unused tikzlibaries. You might have \usetikzlibrary{shapes,arrows} where arrows is needes but shapes is not.

Then, some packages are used but not "needed" as "does not compile without", for example microtype. OK, you can check if document changes at all when you remove a package, but then, hypersetup put ModDate to PDF metadata.

share|improve this answer
    
Thanks, this seems like a good option. –  Anthony Labarre May 8 '13 at 11:23

Patching \newcommand to keep a record of what has been defined by what package, and what has been used, might work here but I'm not sure it's worth the effort. Just don't include so much. Or, live with a few more unecessary milliseconds of compile time.

share|improve this answer
    
\newcommand, \def, \edef, \gdef, \xdef, \protected@edef, \def\mynewcommand{... aso and counters, boxes, tokens, dimens ... –  Herbert Dec 24 '10 at 12:19
    
Well, probably just \def, \edef, \xdef, and \gdef. the others eventually expand to those 4. –  TH. Dec 24 '10 at 12:24
    
"eventually" ... –  Herbert Dec 24 '10 at 12:41
    
I think you could get 95% of what the OP wanted by patching \newcommand since most LaTeX packages will provide top-level commands this way. But like I said, I'm not recommending this. –  Matthew Leingang Dec 24 '10 at 12:42
    
then let us compare a real latex package like hyperref, which doesn't work with TeX: \newcommand 29 and \def 1701 –  Herbert Dec 24 '10 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.