19

I'd like to do something like the following (in pseudo-TeX)

\ifpackageavailable{packagefoo}
\usefoo
\else
\usefooreplacement
\fi

For example, I know that a template file I wrote is used by people who don't have the mdframed package. So I want to write:

\ifpackageavailable{mdframed}
\usepackage{mdframed}
\newenvironment{framedbox}...
\else
\usepackage{framed}
\newenvironment{framedbox}... 
\fi

Is there anything that provides that kind of functionality?

15

Use \IfFileExists. Here's a code snippet from my idxlayout package that tests for the availability of ragged2e.

\AtBeginDocument{%
  \IfFileExists{ragged2e.sty}{%
    \RequirePackage{ragged2e}%
    \renewcommand*{\ila@RaggedRight}{\RaggedRight}%
  }{%
    \PackageWarning{idxlayout}{%
      Package ragged2e not available, therefore\MessageBreak
      substituting command raggedright for RaggedRight\MessageBreak
    }%
    \renewcommand*{\ila@RaggedRight}{\raggedright}%
  }%
}
  • I added code like this to enable optional features in my packages which depend on other packages. However I got the impression that \IfFileExists already triggers MikTeX auto-install feature (I didn't do deeper tests). – Martin Scharrer Jan 22 '13 at 22:03
7
\documentclass{article}

\IfFileExists{mdframed.sty}
  {\usepackage{mdframed}
   %\newenvironment{framedbox}
   \let\framed\mdframed
   \let\endframed\endmdframed
  }
  {\usepackage{framed}
   %\newenvironment{framedbox}... 
  }

\begin{document}

\begin{framed}
foo
\end{framed}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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