kvoptions 和 babel 不相容

kvoptions 和 babel 不相容

我在我的文檔類中使用它kvoptions,到目前為止它工作得很好,但是在一次重大重寫之後我​​開始遇到奇怪的錯誤,我能夠追溯到包babel

微量元素:

%% 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}

日誌摘錄(為簡潔起見省略路徑):

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}]

如果沒有kvoptions補丁,錯誤會變得更嚴重:

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'. []

分析:問題是我在全域選項 ( title) 中使用了空格(和大括號),並且該選項顯然被傳遞給babel.如果沒有空格,就不會有錯誤。

問題:我可以防止文檔類選項被包用作全域選項嗎?或者是否有一些類似於\hypersetup{}我可以使用而不是選項的解決方法?我可以忍受這一點。

答案1

我透過汲取靈感解決了這個問題另一個問題的回答。應用於上面的 MWE,它看起來像這樣:

%% 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}

相關內容