1

Suppose i have a package like this:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{test}[2014/08/21 Example package]

\newcommand{\bla}{}
\DeclareOption{optiona}{
    \renewcommand{\bla}{Choice A}
}

\DeclareOption{optionb}{
    \renewcommand{\bla}{Choice B}
}
\ProcessOptions\relax

And i want to write a documentation, where i want to show both options like this:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[optionb]{test}
\begin{document}
This is the output with optiona:
\bla %prints Choice A
This is the output with optionb:
\bla%prints Choice B
\end{document}

How do i achieve that? i found something about keyval, but honestly, i don't get how that works. Any helpful tips?

1

The short answer is that you can't retrospectively change options for already loaded packages (see here: Applying options to already loaded package), but if you design your class file carefully then you can probably make hooks for you to latch on to in each case. Using your example:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{test}[2014/08/21 Example package]

\newcommand{\bla}{}
\newcommand{\abla}{Choice A}
\newcommand{\bbla}{Choice B}
\DeclareOption{optiona}{
    \renewcommand{\bla}{\abla}
}

\DeclareOption{optionb}{
    \renewcommand{\bla}{\bbla}
}
\ProcessOptions\relax

and then in your documentation, something like

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[optionb]{test}
\begin{document}
This is the output with optiona:
\abla %prints Choice A
This is the output with optionb:
\bbla%prints Choice B
\end{document}

The above is ok, but you can be more clever if you use \if commands, something like \if@optiona which is set to true if optiona is specified, and then you can build that into the definition of \bla. Ultimately, however, it always comes down to the same thing: making a way of specifying the two different definitions from the tex file.

|improve this answer|||||
  • Ok, hm, that's not what I hoped to hear. I thought that it was possible, because the geometry package allows to set a new geometry mid document... – Astrina Feb 11 at 17:07
  • That's essentially because the geometry package is written in such a way to change settings: in fact, all the keyval options you typically pass into the geometry package can be set by direct commands, too. – rbrignall Feb 11 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.