[Tex/LaTex] How to declare several options for a package

package-optionspackage-writing

I'm writing a package and already added two options with \DeclareOption. Then with \ExecuteOptions I set the default option. This works really well, but now I have to add a second option type and this does not seem to work.

Let me explain what I mean by option type. The first couple of options (remember, those that worked well) defined a language (I made custom translations using the translations package and this option passes the language to the translations package and to babel), so only one of them was passed, e.g. \usepackage[en]{myPackage}. What I try to do now is passing a second option to my package, where there are also two possibilities of choosing (whether the bibliography is sorted by name or by appearance), so the usepackage command should look like this: \usepackage[en, bib-by-appearance]{myPackage}.

What I've done is the following:

\DeclareOption{de}{%
    % Some Code
}

\DeclareOption{en}{%
    % Some Code
}

\DeclareOption{bib-by-name}{%
    \bibliographystyle{abbrvnat}%
}

\DeclareOption{bib-by-appearance}{%
    \bibliographystyle{template-files/custom-abbrvnat}%
}

\ExecuteOptions{en, bib-by-appearance}

\ProcessOptions\relax

This does not seem to work. How am I supposed to define the options to get my result?


Update

I was asked to deliver an MWE and I normally also do post one, but I am unable to reproduce this behaving… I normally do my MWE's with ShareLaTeX because I find it more simple to set up a small document, well I now ended up uploading my whole project and compiling it. With ShareLaTeX it works well, while it doesn't on my machine. My computer says

Package natbib: Citation 'FooBar' on page 1 undefined.

while it should be defined (and it also does like the ShareLaTeX example shows). I also tested to simply use \bibliographystyle{template-files/custom-abbrvnat} without selecting it via an option. This also works well. So it seems that my computer has trouble to do his job if the bibstyle is set by an option.

The fail happens if I change \ExecuteOptions{en} to \ExecuteOptions{en, bib-by-appearance}. So I am able to \DefineOption the two possibilities, I am also able to use both bibstyles if called directly (without the option) but I fail when setting the default value of my package to be en, bib-by-appearance.

Update 2

I now also tested what would happen if I directly set the language, without using the options, so the \ExecuteOptions would only contain the bib-by-appearance and this works. So I think I've located the issue. Now my question is:

Why isn't it working with the two options? How can I make it work?


Feel free to shout out any guess that comes in your mind, I'm happy to try them out. If I can deliver anything else than an MWE, let me know.

Best Answer

The space in \ExecuteOptions is an easily solved problem; but there's another one. With the input

\documentclass{article}
\usepackage[en,bib-by-name]{sam}

\begin{document}
x
\end{document}

the .aux file will contain

\relax 
\bibstyle{unsrtnat}
\bibstyle{abbrvnat}

and running BibTeX will abort loading the second bib style, which is the one the user wants!

This is BibTeX, Version 0.99d (TeX Live 2017)
The top-level auxiliary file: samtest.aux
The style file: unsrtnat.bst
Illegal, another \bibstyle command---line 3 of file samtest.aux
 : \bibstyle
 :          {abbrvnat}
I'm skipping whatever remains of this command

Use a conditional:

\RequirePackage{natbib}
\DeclareOption{de}{%
    % Some Code
}

\DeclareOption{en}{%
    % Some Code
}

\newif\ifbibbyname
\DeclareOption{bib-by-name}{%
  \bibbynametrue
}

\DeclareOption{bib-by-appearance}{%
  \bibbynamefalse
}

\ExecuteOptions{en,bib-by-appearance}% <--- no spaces here!

\ProcessOptions\relax

\ifbibbyname
  \bibliographystyle{abbrvnat}
\else
  \bibliographystyle{unsrtnat}
\fi

(I used a standard bib style instead of the custom one you have, just replace it.)