9

I have seen package options such as in geometry that allow the user to specify an option with a certain value. For example, \usepackage[margin=1in]{geometry} will use 1in for the left and right margins. Is this possible to do in my own custom class? Say I want to specify the margins in the class, such as \documentclass[margin=1in]{myclass}. How would I do this? I know how to pass in regular options, such as option, but not ones that have a value, such as option=value.

4

3 Answers 3

12

enter image description here

For old releases packages providing this are available, but in the current release it is built in, see ltnews35.

myc.cls


\NeedsTeXFormat{LaTeX2e}[2022-06-01]
\ProvidesClass{myc}[2022-06-17]

\RequirePackage{xcolor}


\DeclareKeys{
color.code = \colorlet{foocol}{#1},
val.store = \fooval
}


\SetKeys{
color =black,
val=0
}

\ProcessKeyOptions

\setlength\textwidth{6cm}
\setlength\textheight{8cm}

\renewcommand\normalsize{\fontsize{10}{12}\selectfont}

file.tex


\documentclass[color=red,val=42]{myc}

\begin{document}

\textcolor{foocol}{one}  $x=\fooval$
\end{document}

4
  • 2
    is there a more detail documentation for the new built-in commands, including uses with tests for example? The ltnews35 blurb is quite lacking for people who, like me, have not followed closely l3 development.
    – ArTourter
    Jun 17, 2022 at 13:13
  • 1
    texdoc souce2e has information (ltkeys chapter) and we have an intention (not done yet) to update clsguide The l3keys2e package doc also covers the main ideas, but with some changes in command names.@ArTourter Jun 17, 2022 at 15:21
  • Thanks David! I did look at the l3keys2e doc but left me none the wiser:-) The source2e is more helpful indeed. I will keep an eye on the clsguide though.
    – ArTourter
    Jun 17, 2022 at 16:25
  • One should note that keys defined via expl3's \keys_define:nn also work with \ProcessKeyOptions, and that currently more different key-types are available with the expl3 names.
    – Skillmon
    Jun 23, 2022 at 7:10
5

mycls.cls

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{mycls}[2022-06-17]
\LoadClass{article}
\RequirePackage{l3keys2e}
\ExplSyntaxOn
\keys_define:nn{test}
{
  name.tl_set:N = \l_name_tl,
  age.int_set:N = \l_age_int,
}
\ProcessKeysOptions{test}
\def\myinfo{my~name~is~\tl_use:N \l_name_tl,and~i~am~\int_use:N \l_age_int ~years~old.}
\ExplSyntaxOff

main.tex

\documentclass[name = {ljguo},age = 12]{mycls}
\begin{document}
  \myinfo
\end{document}

enter image description here

1
  • 1
    +1 yes l3keys2e is a good choice if needed to work on previous releases as it gives a very simple update path to switch later to the new built-in option handler, as it is essentially the same code, but with an older interface: note s in \ProcessKeysOptions not \ProcessKeyOptions and use of _ and : names for the key types, compared to my answer using the new format version. Jun 17, 2022 at 10:45
0

The following uses expkv-opt (and defines keys using the expkv-def frontend).

myc.cls


\ProvidesClass{myc}[2022-06-18]

\RequirePackage{xcolor}

\RequirePackage{expkv-opt,expkv-def}
\ekvdefinekeys{myc}
  {%
     code    color = \colorlet{foocol}{#1}
    ,initial color = black
    ,store   val   = \fooval
    ,initial val   = 0
    ,store   size  = \foosize
    ,initial size  = 10
  }

\ekvoProcessGlobalOptions{myc}
\ekvoProcessLocalOptions{myc}

\renewcommand\normalsize
  {%
    \fontsize{\foosize}{\strip@pt\dimexpr1.2\dimexpr\foosize pt\relax\relax}%
    \selectfont
  }

\newcommand*\myctest
  {\rule{.4pt}{7pt}Options were: \textcolor{foocol}{\fooval}\rule{.4pt}{7pt}}

file.tex


\documentclass[color=red, val=42, size=14]{myc}

\begin{document}
\myctest
\end{document}

Result:


enter image description here


You must log in to answer this question.

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