3

I want to be organized in my code. In order to pass options to packages, i developped my own macros :

\makeatletter

\newcommand*\Option{%
  \@ifstar{\@Option}{}}

\newcommand*\@Option[3]{%
  \PassOptionsToPackage{#2}{#1}}

\makeatother

One example of use, in preamble:

\Option*{babel}{frenchb}{french babel language}

#1 is the package name
#2 is the option
#3 is a short description
The star indicates that we effectively want to use the option.
The package name is the first argument so that i can sort them alphabetically.

The starred version works. But the unstarred version, code:

\Option{babel}{frenchb}{french babel language}

produces the error:

! LaTeX Error: Missing \begin{document}

To go further with the frenchb package, frenchb provides a command to globally change the aspect of the document : \frenchbsetup{}

I want to manage that the same way:

\makeatletter

\newcommand*\Frenchb{%
  \@ifstar{\@Frenchb}{}}

\newcommand*\@Frenchb[2]{%
  \AtBeginDocument{%
    \@ifpackagewith{babel}{frenchb}
     {\frenchbsetup{#1}}
     {}}}

\makeatother

The starred version doesn't work, and the unstarred version produces the same error as \Option.


Here is the full document:

\documentclass{report}

\makeatletter

\newcommand*\Option{%
  \@ifstar{\@Option}{}}

\newcommand*\@Option[3]{%
  \PassOptionsToPackage{#2}{#1}}

\newcommand*\Frenchb{%
  \@ifstar{\@Frenchb}{}}

\newcommand*\@Frenchb[2]{%
  \AtBeginDocument{%
    \@ifpackagewith{babel}{frenchb}
     {\frenchbsetup{#1}}
     {}}}

\makeatother

\Option{babel}{frenchb}        {french babel language}
\Frenchb{CompactItemize=false} {don't make compact itemize}

\usepackage{babel}
%\frenchbsetup{CompactItemize=false}

\begin{document}

  \part{Introduction} % checking babel/frenchb

    \begin{itemize} % checking CompactItemize=false
      \item item 1
      \item item 2
    \end{itemize}

\end{document}
0

1 Answer 1

5

In the no * case you do nothing which leaves the following arguments as text in the input stream which is not allowed before \begin{document} (and you do not want to typeset the text anyway) so you have to gobble three and two arguments respectively:

\makeatletter
\def\eat@Option#1#2#3{}
\newcommand*\Option{%
  \@ifstar{\@Option}{\eat@Option}}

\newcommand*\@Option[3]{%
  \PassOptionsToPackage{#2}{#1}}

\newcommand*\Frenchb{%
  \@ifstar{\@Frenchb}{\@gobbletwo}}

\newcommand*\@Frenchb[2]{%
  \AtBeginDocument{%
    \@ifpackagewith{babel}{frenchb}
     {\frenchbsetup{#1}}
     {}}}

You must log in to answer this question.

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