0

I want to write a little documentclass which can be used with options, e. g.

\documentclass[title=a]{xyz}

In order to do this, I wrote in the xyz.cls

\RequirePackage{kvoptions}
\SetupKeyvalOptions{
  family=abc,
  prefix=abc@
}
\DeclareStringOption{title}
\ProcessKeyvalOptions*

However, it is not possible to write something like

\documentclass[title={Riemann surfaces, projective curves and function fields}]{xyz}

In this situation, the macro \abc@title has the value Riemannsurfaces (i. e. without spaces and just the part before the first comma). I've already tried the patch-option for the kvoptions-package.

Is there a way to implement this properly (it is possible in the hyperref-package, e. g. \usepackage[pdftitle={Riemann surfaces, ...}]{hyperref})?

  • Braces are always problematic when fed as global options (that is, to \documentclass). Can you make a small MWE, please? – egreg Apr 19 '15 at 12:17
  • Not to mention spaces, which are zapped when the global options are processed for being stored in the variable for later handing to the loaded packages. – egreg Apr 19 '15 at 13:24
  • I solved the problem by using xkeyval and xkvltxp instead of kvoptions. – FKranhold Apr 21 '15 at 19:17
  • 3
    You should write an answer. – egreg Dec 5 '15 at 21:22
1

Using spaces in class options is basically not supported by the latex2e engine (see github issue). There are some workarounds for that problem but these work not for every class (and therefore it usually won't work for your own class as you usually would write a class which based on another class). General speaking you should avoid spaces in class options (at least for latex2e, maybe there are better solutions for Latex3). However if you insist on it, in the following are given three solutions which at least work with some classes.

Solution 1: Using pgfopts

.tex file:

\documentclass[title={with space and, comma}]{xyz}
\begin{document}
    the Title is \title
\end{document}

xyz.cls file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{xyz}[2020/07/14]
\RequirePackage{pgfopts}

\LoadClass{article}

\pgfkeys{
    /xyz/.cd,
    title/.store in=\xyz@title
}

\ProcessPgfOptions{/xyz}

\renewcommand{\title}{\xyz@title}

This doesn't work with KOMAscript (in the linked thread is shown a workaround for it but I didn't try it).

Solution 2: Using xkeyval

See this thread for a minimum working example. As shown in the linked thread you need in opposite to the hyperref package two instead of one curly brackets before and after the value. This will also not work with KOMAScript (for the same reason as for Solution 1).

Solution 3: Loading kvoptions-patch before \documentclass in the .tex file

I don't know how to get the kvoptions-patch working in the .cls file but for some reason it is working with out problem when it is loaded before the \documentclass command inside the .tex file.

.tex file:

\RequirePackage{kvoptions-patch}
\documentclass[title=with space]{xyz}
\begin{document}
    the Title is: \printTitle
\end{document}

xyz.cls file:

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{xyz}[2020/07/14]
\RequirePackage{kvoptions}
\LoadClass{article}

\DeclareStringOption[]{title}
\ProcessKeyvalOptions* 

\newcommand{\printTitle}{\xyz@title}

In addition to the obvious disadvantage that it is confusing for class users that they need to load the kvoptions package before the \documentclass another drawback of this method is that you can't use commas inside values (e.g in the title in the above example). However this solution also works with KOMAScript.

| improve this answer | |

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.