Let's say I construct a macro like \keywords{this, that, other} that accepts an argument, but I want to reject any argument that contains macros or anything besides space, newline, and characters (meaning I raise a \ClassError). Presumably this means I would reject anything that contained catcodes other than 5,10,11,12. It seems like it should be possible to implement this, but I have not the slightest idea how.
1 Answer
The concept of “string” is quite vague. Besides, you probably don't want to reject something such as “étale cohomology”, which would be a perfectly fine keyword in a mathematical paper.
You probably want to ensure that the author types in étale rather than \'etale, in order to ease extraction for other usages.
expl3 has the feature you want, namely, \text_purify:n. This removes commands but is friendly to UTF-8 characters also in pdflatex. You can then compare the result of applying \text_purify:n to the argument with the argument itself.
\documentclass{article} % this would go in the class file \ExplSyntaxOn \NewDocumentCommand{\keywords}{m} { \mccurleyclass_keywords:n { #1 } } \prg_generate_conditional_variant:Nnn \tl_if_eq:nn { e } { T,F,TF } \cs_new_protected:Nn \mccurleyclass_keywords:n { % check whether the argument only contains standard text \tl_if_eq:enTF { \text_purify:n { #1 } } { #1 } {% good! \__mccurleyclass_keywords_print:n { #1 } } {% \msg_error:nn { mccurleyclass } { invalid-keywords } } } \prop_gput:Nnn \g_msg_module_type_prop { mccurleyclass } { Class } \msg_new:nnnn { mccurleyclass } { invalid-keywords } { Illegal~keywords } { Please~use~only~standard~text~in~keywords } \cs_new_protected:Nn \__mccurleyclass_keywords_print:n { \par\addvspace{\topsep} \noindent\textbf{Keywords}\quad #1 \par\addvspace{\topsep} } \ExplSyntaxOff \begin{document} \keywords{this, that, other} % good \keywords{étale cohomology, something else} % good \keywords{\'etale cohomology, something else} % bad \keywords{\textit{étale} cohomology, something else} % bad \end{document} The first two pass the test, the last two don't and issue the message
! Class mccurleyclass Error: Illegal keywords For immediate help type H <return>. ... l.52 ...textit{étale} cohomology, something else} % bad ? h Please use only standard text in keywords - The concept of "string" is only vague in LaTeX. Everywhere else it means UTF-8 string.mccurley– mccurley2022-11-05 16:42:45 +00:00Commented Nov 5, 2022 at 16:42
- That's an excellent answer. I tested it with quite a few inputs and it seems to do exactly what I want.mccurley– mccurley2022-11-05 17:31:41 +00:00Commented Nov 5, 2022 at 17:31
\tl_analysis_map_inlineand build up from there. (it's a bit clumsy but should be straightforward how to.) Alternative includes \str_if_in \c_backslash_str.