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.

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"
\usepackage[en]{myPackage} % loads "czech, english"
share|improve this question
1  
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 at 12:15

2 Answers 2

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

share|improve this answer

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:

enter image description here

\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:

share|improve this answer

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.