2

I'm using kvoptions in my document class and it's worked nicely so far, but after a major rewrite I started getting strange errors that I was able to trace back to the babel package.

MWE:

%% myclass.cls

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{myclass}

\RequirePackage[patch]{kvoptions}

\DeclareStringOption{title}
\ProcessKeyvalOptions*

\LoadClass{article}
\title{\myclass@title}
\RequirePackage[english]{babel}

\endinput
\documentclass[title={Here be dragons}]{myclass}

\begin{document}
Lorem Ipsum
\end{document}

An excerpt from the log (paths omitted for brevity):

babel.sty:460: LaTeX Error: Missing \begin{document}. [    {}}]
babel.sty:460: Too many }'s. [    {}}]
babel.sty:475: LaTeX Error: Missing \begin{document}. [    \ifin@\edef\bbl@tempc{\bbl@tempb}\fi}]

Without the kvoptions patch, the errors are getting worse:

babel.sty:339: LaTeX Error: Missing \begin{document}. [\ProcessOptions*]
babel.sty:339: You can't use `macro parameter character #' in horizontal mode. [\ProcessOptions*]
TeX STOPPED: File ended while scanning use of \reserved@{##1,##2\reserved@b }\def \reserved@b ##1,\reserved@b ##2\reserved@b 
TeX reports the error was in file:3 
myclass.cls:13: LaTeX Error: Unknown option `english' for package `babel'. []

Analysis: The problem is that I have used spaces (and braces) in the global option (title) and this option is obviously passed on to babel. Without the spaces, the errors don't occur.

Question: Can I prevent document class options from being used as global options by packages? Or is there some work-around akin to \hypersetup{} for me to use instead of options? I could live with that.

  • 3
    Complicated values (with spaces) in the document options are difficult, see e.g. here for a discussion github.com/latex3/latex2e/issues/85. But you can always define a \myclasssetup command which can process key-val input with one of the various keyval packages. – Ulrike Fischer Mar 15 at 9:03
1

I solved this problem by taking inspiration from an answer to another question. Applied to the MWE above, it would look like this:

%% myclass.cls

\NeedsTeXFormat{LaTeX2e}[2018/04/01]
\ProvidesClass{myclass}

\RequirePackage{kvoptions}

\DeclareStringOption{title}
\ProcessKeyvalOptions*

\LoadClass{article}

\newcommand*{\docsetup}[1]{
  \kvsetkeys{myclass}{#1}
  \title{\myclass@title}
  \RequirePackage[english]{babel}
}

\endinput
\documentclass{myclass}
\docsetup{title={An awesome title}}

\begin{document}
Lorem Ipsum
\end{document}

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.