I want to create a LaTeX
class with optional parameters using LaTeX3
. But I'm having problems converting the token list holding the parameter value to a string. I would like to know how to do this correctly even if my entire strategy for processing class options is wrong. I am finding learning expl3
very very difficult.
No matter how I try to convert the token list to a string it just seems to return the LaTeX source code. I feel I have tried every permutation of \tl_to_str:n
and :V
with every permutation of \str_set:Nn
and :NV
but I probably haven't otherwise I wouldn't need to ask this question.
For the class I am creating, one optional parameter will allow the end user to choose the underlying document class. The default will be article
if no class
parameter is supplied:
\documentclass{apple}
would use the default classarticle
whereas\documentclass[class=book]{apple}
would use the specified classbook
I thought it good strategy to
- define a string constant for the default
\str_const:Nn \c__apple_base_class_str { article }
- use the string constant to seed the parameter value
- use a string to hold the final value once the class options have been processed
Edit: The reason for using a string is that, if I understand correctly, token lists are for stuff that will be typeset and strings (such as LaTeX has them) are for stuff that won't be typeset. Source Token list vs string in LaTeX3
MWE of the broken bit
\str_new:N \l__apple_base_class_str
\str_set:Nn \l__apple_base_class_str { \tl_to_str:n \l__apple_class_tl }
Full MWE
This code is seeded from the accepted answer of how to properly write initialization part with expl3. I assume this is the correct way to specify and process class options.
\NeedsTeXFormat{LaTeX2e}
\RequirePackage{expl3,xparse,l3keys2e}
\ProvidesExplClass{apple}
{2022/06/27} {0.0} {My first class}
\str_const:Nn \c__apple_base_class_str { article }
\keys_define:nn { apple }
{
, class .tl_set:N = \l__apple_class_tl
, class .initial:n = { \c__apple_base_class_str }
, class .value_required:n = true
}
\str_new:N \l__apple_base_class_str
\str_set:Nn \l__apple_base_class_str { \tl_to_str:n \l__apple_class_tl }
\iow_term:x { tl~of~class:~\tl_use:N \l__apple_class_tl }
\iow_term:x { str~of~class:~\str_use:N \l__apple_base_class_str }
\ProcessKeysOptions { apple }
\LoadClass { \str_use:N \l__apple_base_class_str }
Output
Document Class: apple 2022/06/27 v0.0 My first class
tl of class: article
str of class: \tl_to_str:n \l__apple_class_tl
! LaTeX Error: File `\tl_to_str:n \l__apple_class_tl .cls' not found.
Compiling with LuaHBTeX, Version 1.15.0 (MiKTeX 22.3)
if that matters.