5

I am playing around with making a package for some of my LaTeX commands and environments. The problem I am encountering is that the package will need to take a numerical value, which have a default value if it is not set. So I want to be able to write:

\usepackage[XXXX]{myPackage}

or

\usepackage[val=XXXX]{myPackage}

With XXXX being any 4 digit number from 0000 to 9999.

I have looked at kvoptions, but I haven't been able to figure out how to do this from the examples I have seen. So what I need is:

  • A way for my package to take a numerical value or key-value pair.
  • A way to check that the value is between 0000 and 9999

2 Answers 2

4

The xkeyval package sets a family of keys that has the same name like the package.

For this family keys can be defined, like

\define@key{mypackage.sty}{val}[100]{%
      \ifnum#1 < -1
      \GenericError{Error!}{The value is not in the correct range}{See blabla}{or other blabla}
      \fi
      \ifnum#1 > 10000
      \GenericError{Error!}{The value is not in the correct range}{See blabla}{or other blabla}
      \fi
      \def\kvmypackage@val{#1}%
    }

Package code:

\ProvidesPackage{mypackage}

\RequirePackage{xkeyval}


\define@key{mypackage.sty}{val}[100]{%
  \ifnum#1 < -1
  \GenericError{Error!}{The value is not in the correct range}{See blabla}{or other blabla}
  \fi
  \ifnum#1 > 10000
  \GenericError{Error!}{The value is not in the correct range}{See blabla}{or other blabla}
  \fi
  \def\kvmypackage@val{#1}%
}

\ExecuteOptionsX{val} % Provide the default value (100 here)
\ProcessOptionsX%

\newcommand{\ValueOfVal}{%
  \kvmypackage@val%
}

\endinput

The \ifnum... instructions check the value for the range of [0...9999], while \kvmypackage@val{#1} is being defined to have the key value later on -- the name of this accessing macro is basically arbitrary, but it should be easy to remember and at best have the name of the key in its name, e.g. \kvmypackage@val.

The macro \ValueOfVal is given for convenience to get the value without needing \makeatletter...\makeatother Calling document:

\documentclass{article}


\usepackage[val=256]{mypackage}


\begin{document}
Value of val is \ValueOfVal
\end{document}
3
  • This work for the check that there is a value and if it is in the right range. But how do I set a default value, if the option is not used?
    – mallereik
    May 3, 2016 at 20:57
  • @mallereik: See the [100] in the definition of the of key -- that's the default value (chosen by me). The \ExecuteOptionsX command guarantees that the option is set then, if it is not used when loading the package.
    – user31729
    May 3, 2016 at 20:58
  • Works like a charm after the edit, thank you very much.
    – mallereik
    May 3, 2016 at 21:02
3

Solution using kvoptions

The default value is configured by macro `\myPackage@val@default'. If the value is out of range, then an error message is given and the value is set to the default value.

The leading zeros can be omitted in the package options, but the macro \theMyPackageVal is normalized to show four digits.

Package file myPackage.sty:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{myPackage}[2016/04/03]

\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=myPackage,
  prefix=myPackage@,
}
\newcommand*{\myPackage@val@default}{1234}
\DeclareStringOption[\myPackage@val@default]{val}
\AddToKeyvalOption*{val}{%
  % Error checks
  \ifnum\myPackage@val<0 %
    \PackageError{myPackage}{%
      Negative value (\myPackage@val) for option `val'%
    }{%
      The range for option `val' is 0000 to 9999.\MessageBreak
      \@ehc
    }%
    \let\myPackage@val\myPackage@val@default
  \else\ifnum\myPackage@val>9999 %
    \PackageError{myPackage}{%
      Too large value (\myPackage@val) for option `val'%
    }{%
      The range for option `val' is 0000 to 9999.\MessageBreak
    }%
    \let\myPackage@val\myPackage@val@default
  \fi\fi
}

\ProcessKeyvalOptions*

% Normalization
\edef\theMyPackageVal{%
  \ifnum\myPackage@val<1000 0\fi
  \ifnum\myPackage@val<100 0\fi
  \ifnum\myPackage@val<10 0\fi
  \number\myPackage@val
}%

\endinput

Test file:

\documentclass{article}
\usepackage[val=567]{myPackage}
\begin{document}
val=\theMyPackageVal
\end{document}

Result: val=0567

You must log in to answer this question.

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