TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.

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
up vote 7 down vote accepted

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


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


It supports optional arguments,


for example.

share|improve this answer

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