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
.
3 Answers
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}
-
2is 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. Jun 17, 2022 at 13:13
-
1
texdoc souce2e
has information (ltkeys chapter) and we have an intention (not done yet) to updateclsguide
Thel3keys2e
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:-) Thesource2e
is more helpful indeed. I will keep an eye on theclsguide
though. 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 theexpl3
names.– SkillmonJun 23, 2022 at 7:10
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}
-
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: notes
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
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:
l3keys
one?