2

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

  1. 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.
  2. campa in their answer at https://tex.stackexchange.com/a/515203/256551 successfully demonstrates how to use soem macros \a and \b within another macro's name, but \a and \b are defined as simple strings, \def\a{A} and \def\b{B}, while the \temp macro in my MWE is itself calling a macro.
  3. 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?

4
  • 1
    you can not use \mkfirstuc for this, it is not expandable. You can use \text_titlecase:n from expl3. But you shouldn't define unconditionally commands like this, if you overwrite an important existing command you can break a lot. Commented Jun 13, 2022 at 9:53
  • @UlrikeFischer, thank you very much! How should the syntax \text_titlecase:n be used in the context of the MWE? Could you, please, write this solution as an answer to the question? I appreciate it very much! Commented Jun 13, 2022 at 10:01
  • 1
    If you really want to learn the internal working/understand what is going on, consider reading the TeXbook/TeX by topic.
    – user202729
    Commented Jun 13, 2022 at 15:49
  • @user202729, thank you very much for the great suggestion! I will definitely consult these resources! Commented Jun 14, 2022 at 3:11

2 Answers 2

1
\documentclass{article}

\begin{document}
    \def\CurrentOption{word}
    \ExplSyntaxOn
    \cs_generate_variant:Nn\text_titlecase:n{V}
    \cs_new:cpn {\text_titlecase:V\CurrentOption}{1}
    \ExplSyntaxOff
    \Word
\end{document}

1
  • Thank you very much for your help! Commented Jun 13, 2022 at 10:21
2

You can solve your task without any \ExplSyntaxOn, only by TeX primitives:

\def\defoptionmacro#1#2\relax#3{%
   \uppercase{\expandafter\xdef\csname #1}#2\endcsname{#3}%
}

% usage:
\def\CurrentOption{worD}
\lowercase\expandafter{\expandafter\defoptionmacro\CurrentOption}\relax{1}

% test:
\meaning\Word  % it is macro:->1

First, \lowercase is processed and \defoptionmacro word\relax{1} is invoked. Second, the \uppercase is applied to the first token and then the \csname...\endcsname is completed.

But IMHO your requirement to do uppercase of the first character is due to you want to avoid to re-define existing macro or another control sequence. This can be done by different way: use a character typically not present in the control sequences. For example:

\def\defoptionmacro#1\relax#2{\expandafter\xdef\csname option?#1\endcsname{#2}}

Then the macro \option?word is defined. Of course, you must to use option-defined macros by the same prefix option? using \csname...\endcsname.

1
  • +1 for the great, working method and for the recommendation to use a special character. I will definitely consider that! Commented Jun 14, 2022 at 3:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .