11

I trying to write a package, and I'd like to specify whether there should be colour or not.
I'd like to do:

\usepackage[color=false]{foo}

and have this set

\RequirePackage[color=false]{hyperref}

or true as the case may be. What's the best way to do this?

4
  • I would us two options : the default one would be bw for black and white, and the second one would be color.
    – projetmbc
    Apr 12, 2012 at 18:56
  • My comment is a general one which doesn't take care of the package hyperref.
    – projetmbc
    Apr 12, 2012 at 19:09
  • Duplicate: tex.stackexchange.com/q/12631/5763 Apr 13, 2012 at 8:19
  • I saw tex.stackexchange.com/q/12631/5763, but I wanted to know how to expand out the text, which it didn't outline. However, I did reread the documentation for pgfopts, and used that to do what I wanted. I think the main issue with pgfopts was not understanding how pgfkeys worked. Apr 13, 2012 at 14:08

3 Answers 3

13

If you want all the options to your package to be passed to hyperref then you can just do

\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref}
\ProcessOptions\relax
\RequirePackage{hyperref}

see clsguide.tex documentation in the LaTeX base.

I just read clsguide:-) and if this is all you want to do, it points out that there is a more efficient shortcut to all the above three lines

\RequirePackageWithOptions{hyperref}
4

Another option is to use keyreader

\begin{filecontents}{mypack.sty}
\ProvidesPackage{mypack}

\usepackage{keyreader}

% Define the keys in family MYFAM and set the
% macor prefix to my@
\krddefinekeys{MYFAM}[my@]{
    % define a command key 'say' that sores
    % it’s value in \my@say
    cmd/say/jump/;
    % [1] define a boolean 
    bool/colori/true/;
    % [2] define a boolean and store the value
    bool/colorii/true/\def\my@colorii{#1};
}
% booleand are not preset by defualt so we
% do it manually
\krdsetkeys{MYFAM}{colori=false,colorii=false}

% Process the package options
\krdProcessOptions<MYFAM>\relax

% with version [1]
%\ifmy@colori
%   \RequirePackage[colorlinks=true]{hyperref}
%\else
%   \RequirePackage[colorlinks=false]{hyperref}
%\fi

% with version [2]
\RequirePackage[colorlinks=\my@colorii]{hyperref}

\newcommand{\mycmd}[1]{%
    % use the value of 'say'
    #1 says \my@say.
}
\end{filecontents}

\documentclass{article}

\usepackage[%
    say={sit down},
    colori=true,% [1]
    colorii=false,% [2]
]{mypack}

\begin{document}
\mycmd{Simon}

\url{www.myurl.com}
\end{document}

Notes

  • hyperref has no color option (at least my version hasn’t)

  • Version [1] is the shorter was if you want to access only one hyperref key

  • Version [2] is better if you want to access more then one option.

    • For that we store the value of the option an pass it to hyperref later. Booleans don’t store their value by default, they only create new \if…

    • Why not use a cmd for this version? Because bool key checks if it’t value matches true or false


Maybe it’s better to use the pdf key processing since it’s more related with the beamer class, but I’m not familiar with that.

0

Another option is to use kvoptions and etoolbox, in your .sty file, write:

...
\RequirePackage{kvoptions}
\RequirePackage{etoolbox}

%% if the variable 'color' is
%% 1. not included                  : it will be set to 'false'
%% 2. included but not given a value: it will be set to 'true'
%% 3. included and given a value    : it will be set to the value that is given
\DeclareStringOption[false]{color}[true]

\newif \ifcolor %% new Boolean variable is default to 'false'
\ifdefstring{\foo@color}{true}{\colortrue}{} %% set the Boolean variable to 'true' if '\foo@color' is 'true'

\ifcolor
\RequirePackage[color = true]{hyperref}
\else
\RequirePackage[color = false]{hyperref}
\fi
...

Then in your document you can specify the option 'color' with:

...
\usepackage[color]{foo}         %% color = true
\usepackage[color = true]{foo}  %% color = true
\usepackage[color = false]{foo} %% color = false
...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .