# Passing an if-else option to a custom sty file

I have a custom .sty file loading needed packages and defining some commands and environments. Is there an easy way to either:

• define an if-else option
• define option's default value

I am using the babel package and want to specify it's options:

\usepackage[english, czech]{babel}


But sometimes, I need the opposite:

\usepackage[czech, english]{babel}


Is there a way to use it like this?

\usepackage{myPackage} % loads "english, czech"

-
You need a bool-option. More specifications will be possible if you provide a small MWE with your used keyval package. –  Marco Daniel Jan 5 '14 at 12:15

\ProvidesPackage{mypackage}[2014/01/05 ...]
...
\newif\if@mylang\@mylangfalse
\DeclareOption{en}{\@mylangtrue}
...
\DeclareOption*{\OptionNotUsed}
\ProcessOptions\relax
...
\if@mylang
\RequirePackage[english,czech]{babel}
\else
\RequirePackage[czech,english]{babel}%% the default
\fi
...


If you need the default the other way round then change the definition.

-

Here is a simple way of doing it. The following code is a minimalist .sty file called yourpackagename. It defines a switch \ifyourpkgprefix@enoption and declares an option called en which, if passed to your package, sets the switch to true. After the option(s) have been processed, the babel package is loaded with options english and czech in the desired order, on the basis of the switch's value.

Of course, you should choose different package name, prefix, description, etc.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{yourpackagename}[2014/01/05 v0.1 yourpackagedescription]

% switch for 'en' option
\newif\ifyourpkgprefix@enoption\yourpkgprefix@enoptionfalse

\DeclareOption{en}{\yourpkgprefix@enoptiontrue}
\DeclareOption*{\OptionNotUsed} % discard any undeclared option

\ProcessOptions\relax

\ifyourpkgprefix@enoption
\RequirePackage[czech, english]{babel}
\else
\RequirePackage[english, czech]{babel}
\fi

\endinput


Here is a MWE using that package:

\documentclass{article}
%\usepackage{yourpackagename}                   % comment one of those two lines
\usepackage[en]{yourpackagename}

\begin{document}

% --- sanity check ---
The \texttt{babel} options were loaded in the following order:
\makeatletter
\ifyourpkgprefix@enoption%
\texttt{czech, english} (option~\texttt{en} was passed).
\else%
\texttt{english, czech} (option~\texttt{en} was NOT passed).
\fi
\makeatother
% --------------------

foobar

\end{document}


Useful resources:

-