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.

Possible Duplicate:
Conditional “if a package is available”

I'm using a package "mcode.sty" that enhances/colors listings output; typesetting will work perfectly without it. However, I'm often typesetting in an environment where I can't include the package file anywhere LaTeX can find it except the current directory. I'd like to include the \usepackage statement in my default preamble (I'm using LyX), but often the file won't be there, and LaTeX will quit with an error, even though I have no need for the functionality in my current project. In effect, I'd like to include the package only if the .sty file is in the current directory, or somewhere on the LaTeX path, and not exit if it can't be found. Something like \begin{ignore_errors} \usepackage{mcode} \end{ignore_errors}. Is there a way to do this?

share|improve this question
1  
I think this is a duplicate –  Seamus Apr 15 '11 at 10:26
    
Thanks, Seamus! I think I searched for every combination of terms except that one :) –  btown Apr 16 '11 at 0:59
add comment

marked as duplicate by Seamus, Martin Scharrer Apr 15 '11 at 11:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 6 down vote accepted

If you don't need the package, you shouldn't be including it. That said, this does what you want.

\IfFileExists{foo.sty}{\usepackage{foo}}{}

If you want a macro for this, then you can use something like the following.

\newcommand*\usepackageifpresent[2][]{%
    \IfFileExists{#2.sty}{\usepackage[#1]{#2}}{}%
}

It supports optional arguments,

\usepackageifpresent[margin=3in]{geometry}

for example.

share|improve this answer
add comment

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