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.
- 757,742
- 680
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}
- 757,742
-
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. – ArTourter Jun 17 '22 at 13:13
-
1
texdoc souce2ehas information (ltkeys chapter) and we have an intention (not done yet) to updateclsguideThel3keys2epackage doc also covers the main ideas, but with some changes in command names.@ArTourter – David Carlisle Jun 17 '22 at 15:21 -
Thanks David! I did look at the
l3keys2edoc but left me none the wiser:-) Thesource2eis more helpful indeed. I will keep an eye on theclsguidethough. – ArTourter Jun 17 '22 at 16:25 -
One should note that keys defined via
expl3's\keys_define:nnalso work with\ProcessKeyOptions, and that currently more different key-types are available with theexpl3names. – Skillmon Jun 23 '22 at 07: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,213
- 1
- 4
- 12
-
1+1 yes
l3keys2eis 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: notesin\ProcessKeysOptionsnot\ProcessKeyOptionsand use of_and:names for the key types, compared to my answer using the new format version. – David Carlisle Jun 17 '22 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:
- 60,462



l3keysone? – Joseph Wright Jun 17 '22 at 07:13