ivan” post=82805Questo è il codice di una semplice classe che definisce due opzioni
con la sintassi di LaTeX3:
`\RequirePackage{color,l3keys2e,xparse}\ProvidesExplClass
{test} {2010/01/02} {2.0} {A simple class with options}\bool_new:N \l__test_pagelayout_standard_bool
\bool_new:N \l__test_pagelayout_periodical_bool\keys_define:nn { test } {
showframe .bool_set:N = \l_test_showframe_bool ,
pagelayout .choice:,
pagelayout / standard
.code:n= \bool_set_true:N\l__test_pagelayout_standard_bool,
pagelayout / periodical
.code:n= \bool_set_true:N\l__test_pagelayout_periodical_bool,
}\ProcessKeysOptions { test }
\LoadClass{book}
% “showframe'' boolean option
\bool_if:NT \l_test_showframe_bool
{
\RequirePackage{showframe}
}% “pagelayout'' option:
\RequirePackage{geometry}
\bool_if:NT \l__test_pagelayout_standard_bool
{
\geometry{%
a4paper,
includeheadfoot=true,
textwidth= 110mm,
textheight= 220mm,
% marginratio= 1:2,
marginparwidth= 30mm,
marginparsep= 12pt
}
}
\bool_if:NT \l__test_pagelayout_periodical_bool
{
\geometry{
includeheadfoot=false,
textheight= 165mm,
textwidth= 110mm,
paperwidth= 170mm,
paperheight= 240mm,
% marginratio= 2:3,
marginparwidth= 26mm,
marginparsep= 10pt}
}\endinput`
Questa semplice classe definisce due opzioni:
1) L’opzione showframe è booleana e può assumere i valori:
showframe=true (o semplicemente showframe)
showframe=false
2) L’opzione pagelayout è multichoice e può assumere i valori:
pagelayout=standard
pagelayout=periodicalA parte il fatto che la sintassi di LaTeX3 mi pare molto macchinosa, come mai se si decommenta la riga relativa a marginratio si ottiene un errore?
La sintassi che ho usato è corretta? Si può migliorare? Mi sono ispirato a un articolo di J Wright su TB e al codice del pacchetto acro.sty (che è uno dei pochi che definisce opzioni a più valori)
Si può migliorare, secondo me.
`\RequirePackage{l3keys2e,xparse}
\ProvidesExplClass{test} {2010/01/02} {2.0} {A simple class with options}
\group_begin:
\char_set_lccode:nn {`?}{`:}
\tl_to_lowercase:n
{
\group_end:
\cs_new:Npn \__test_standard_layout:
{
\geometry{%
a4paper,
includeheadfoot=true,
textwidth= 110mm,
textheight= 220mm,
marginratio= 1?2,
marginparwidth= 30mm,
marginparsep= 12pt
}
}
\cs_new:Npn \__test_periodical_layout:
{
\geometry{
includeheadfoot=false,
textheight= 165mm,
textwidth= 110mm,
paperwidth= 170mm,
paperheight= 240mm,
marginratio= 2?3,
marginparwidth= 26mm,
marginparsep= 10pt
}
}
} % end of \tl_to_lowercase:nn
\cs_new_eq:NN \__test_page_layout: \__test_standard_layout: % default
\keys_define:nn { test } {
showframe .bool_set:N = \l__test_showframe_bool,
pagelayout .choice:,
pagelayout / standard
.code:n = \cs_set_eq:NN \__test_page_layout: \__test_standard_layout:,
pagelayout / periodical
.code:n = \cs_set_eq:NN \__test_page_layout: \__test_periodical_layout:,
}
\ProcessKeysOptions { test }
\LoadClass{book}
\RequirePackage{geometry}
\bool_if:NT \l__test_showframe_bool { \geometry{showframe} }
\__test_page_layout:
\RequirePackage{color}
\endinput`
Purtroppo il passaggio per [tt]\tl_to_lowercase:n[/tt] è inevitabile, perché i due punti sono come lettere nell’ambiente di programmazione e quindi [tt]\geometry[/tt] non riconoscerebbe l’opzione [tt]2:3[/tt].
Non serve definire tanti condizionali, in questo caso; secondo me è meglio definirsi funzioni che eseguano il compito prescritto.
Ciao
Enrico