What happened
Given that the macro \CurrentOption whose value is an unknown string of English letters already exists in the document, I want to create another macro whose name is the capitalized value of \CurrentOption.
For example, if \CurrentOption contains the string word, I would like to define a macro called Word.
However, the error
! Missing \endcsname inserted.
<to be read again>
\def
l.8 \expandafter\def\csname\temp
YesOrNo\endcsname{1}
is produced when compiling the following MWE with pdfLaTeX:
\documentclass{article}
\usepackage{mfirstuc} % For \xmakefirstuc, to capitalize the first letter.
\begin{document}
\def\CurrentOption{word}
\edef\temp{\xmakefirstuc{\CurrentOption}}
\expandafter\xdef\csname\temp\endcsname{1} % <--- The error is here
\end{document}
How should I change the code above, without removing the statement \def\CurrentOption{word}, so that a macro named Word with the value 1 is defined?
General purpose of code
I am writing a package in which case-insensitive options should be processed. For example, if the package receives the option word, I would like the package to xdef the macro whose name is Word. Similaly, if the package receives the option wOrD, I would like the package to xdef the same macro Word.
My plan is to achieve this using the following code within the package:
\ProvidesPackage{courseMacros}[6/13/2022 v2.0, Miriam Briskman]
\RequirePackage{mfirstuc}
\DeclareOption*{
\edef\temp{\xmakefirstuc{\CurrentOption}}
\expandafter\xdef\csname\temp\endcsname{1}
}
\ProcessOptions\relax
% .... the rest of the package ...
Note that \CurrentOption above is a defined macro expanding to the current option's value.
Nonetheless, the same ! Missing \endcsname inserted. error is generated when using the package above in a TeX file.
Console Output
The full output of the console when compiling the 1st MWE is
This is pdfTeX, Version 3.141592653-2.6-1.40.24 (MiKTeX 22.3) (preloaded format=pdflatex.fmt)
restricted \write18 enabled.
entering extended mode
(example.tex
LaTeX2e <2021-11-15> patch level 1
L3 programming layer <2022-02-24>
(C:\Users\Miriam Briskman\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\arti
cle.cls
Document Class: article 2021/10/04 v1.4n Standard LaTeX document class
(C:\Users\Miriam Briskman\AppData\Local\Programs\MiKTeX 2.9\tex/latex/base\size
10.clo))
(C:\Users\Miriam Briskman\AppData\Local\Programs\MiKTeX 2.9\tex/latex/mfirstuc
mfirstuc.sty
(C:\Users\Miriam Briskman\AppData\Local\Programs\MiKTeX 2.9\tex/latex/etoolbox
etoolbox.sty))
(C:\Users\Miriam Briskman\AppData\Local\Programs\MiKTeX 2.9\tex/latex/l3backend
\l3backend-pdftex.def) (C:\temp\example.aux)
! Missing \endcsname inserted.
<to be read again>
\def
l.8 \expandafter\xdef\csname\temp
\endcsname{1}
?
Research so far
- Joseph Wright in their answer at https://tex.stackexchange.com/a/519/256551 provides an impressive explanation of how expansion works in
TeX. However, non of the examples uses a macro within\csname ... \endcsname. - campa in their answer at https://tex.stackexchange.com/a/515203/256551 successfully demonstrates how to use soem macros
\aand\bwithin another macro's name, but\aand\bare defined as simple strings,\def\a{A}and\def\b{B}, while the\tempmacro in my MWE is itself calling a macro. - When I change my MWE to either of
\documentclass{article}
\usepackage{mfirstuc}
\begin{document}
\def\CurrentOption{word}
\edef\temp{\CurrentOption} % <--- \xmakefirstuc{\CurrentOption} changed to \CurrentOption
\expandafter\xdef\csname\temp\endcsname{1}
\end{document}
or
\documentclass{article}
\usepackage{mfirstuc}
\begin{document}
\def\CurrentOption{word}
\expandafter\xdef\csname\CurrentOption\endcsname{1}
\end{document}
the code executes without errors. These changes, however, prevent the macro's name from being capitalized using the macro \xmakefirstuc.
Software Specifications
Executing pdflatex --version in my Windows 10's CMD outputs
MiKTeX-pdfTeX 4.10 (MiKTeX 22.3)
© 1982 D. E. Knuth, © 1996-2021 Hàn Thế Thành
TeX is a trademark of the American Mathematical Society.
using bzip2 version 1.0.8, 13-Jul-2019
compiled with curl version 7.72.0; using libcurl/7.72.0 Schannel
compiled with expat version 2.2.10; using expat_2.2.10
compiled with jpeg version 9.4
compiled with liblzma version 50020052; using 50020052
compiled with libpng version 1.6.37; using 1.6.37
compiled with libressl version LibreSSL 3.1.4; using LibreSSL 3.1.4
compiled with MiKTeX Application Framework version 4.4; using 4.4
compiled with MiKTeX Core version 4.12; using 4.12
compiled with MiKTeX Archive Extractor version 4.0; using 4.0
compiled with MiKTeX Package Manager version 4.7; using 4.7
compiled with uriparser version 0.9.4
compiled with xpdf version 4.02
compiled with zlib version 1.2.11; using 1.2.11
How should the code in the 1st MWE be corrected to eliminate the error, without removing the definition of \CurrentOption, and still being able to capitalize the newly created macro?
\text_titlecase:nfrom expl3. But you shouldn't define unconditionally commands like this, if you overwrite an important existing command you can break a lot. – Ulrike Fischer Jun 13 '22 at 09:53\text_titlecase:nbe used in the context of the MWE? Could you, please, write this solution as an answer to the question? I appreciate it very much! – Miriam Briskman Jun 13 '22 at 10:01