# How to add optional arguments to a package I'm writing?

I want to build a package that can optionally typeset line in double-space. Something like this:

\ProvidePackage{mypackage}
% actual functional codes


Then I can use the package in this way:

\usepackage[doublespc=yes]{mypackage}


So that all line will be typeset in double-space. If I omit that optional argument in the preamble, then all line will be typeset in single-space.

• Welcome to TeX.sx! – Peter Jansson Apr 4 '13 at 6:57
• That sounds like a class (design) not package (functionality) thing – Joseph Wright Apr 4 '13 at 7:07
• Is the keyval part here important, and are you going to have several keyval option? If you only have one, 'faking' keyval may be the easiest approach. – Joseph Wright Apr 4 '13 at 7:15

Here is a basic implementation, following the guide LaTeX 2e for class and package writers (section 4.3 Option declaration, p 18):

\documentclass{article}
\usepackage{filecontents}% http://ctan.org/pkg/filecontents

\begin{filecontents*}{mypackage.sty}
\ProvidesPackage{mypackage}
\newif\if@doublespace
\DeclareOption{doublespace}{\@doublespacetrue}
\ProcessOptions
\if@doublespace\RequirePackage{setspace}\doublespacing\fi
\endinput
\end{filecontents*}

\usepackage[doublespace]{mypackage}
\begin{document}
Here is some

text in two paragraphs.
\end{document}


The package mypackage.sty declares an option doublespace which sets the condition \@doublespacetrue. If this is set to true, it \RequiresPackage{setspace} and sets \doublespacing.

• Great! It's exactly what I want. A nice template for optional arguments. – capsensitive Apr 4 '13 at 7:18