I need to define a token list of the form
\tl_new:N \g_silly_tl
\tl_set:Nn \g_silly_tl { Text ~ and ~ a ~ tie:?? }
How do I do this?
More generally, is there a reference where one can see how to input characters 'normally' under expl3 syntax?
I need to define a token list of the form
\tl_new:N \g_silly_tl
\tl_set:Nn \g_silly_tl { Text ~ and ~ a ~ tie:?? }
How do I do this?
More generally, is there a reference where one can see how to input characters 'normally' under expl3 syntax?
This question has two distinct parts, one of which is easier than the other!
The specific issue here is (presumably) not inserting a tilde but rather inserting a non-breaking space in some 'fixed' text stores as within a LaTeX3 tl variable. While a tilde is a convenient shortcut for this at the document level, here I think I would simply use the macro \nobreakspace.
\tl_set:Nn \l_silly_tl { Text ~ and ~ a \nobreakspace tie:?? }
(Note that at present there is no expl3 code-level 'non-breaking space' function.)
On the wider issue of 'inserting non-standard catcode characters', the expandable function \char_generate:nn is available to create tokens of (almost) all reasonable category codes
\tl_set:Nx \l_silly_tl
{
\char_generate:nn { `\a } { 4 }
}
There are a few cases that are not covered by this approach. For example, with older expl3 releases we couldn't do active characters and you'd need to use the regex engine:
\RequirePackage{expl3}
\ExplSyntaxOn
\tl_clear:N \l_tmpa_tl
\regex_replace_all:nnN { } { \cA\~ } \l_tmpa_tl
More recent versions of expl3 can do this using \char_generate:nn, so it's more of a historical note.
The second option is stick to the 'traditional' \lowercase trick, using expl3 naming of course
\group_begin:
\char_set_catcode_active:n { `\@ }
\char_set_lccode:nn { `\@ } { `\~ }
\tex_lowercase:D
{
\group_end:
\tl_set:Nn \l_silly_tl { Text ~ and ~ a @ tie:?? }
}
(Notice that there is no public interface to \lowercase beyond the saved-primitive.)
I recently made a package to do this. (now on CTAN.)
(remark: clearly everything the package can do l3regex and lowercase can also do, but this package provides a more convenient syntax...)
With the package, you can write
\documentclass{article}
\usepackage{precattl} % ← this package!
\begin{document}
\ExplSyntaxOn
\precattl_exec:n {
\tl_new:N \g_silly_tl
\tl_set:Nn \g_silly_tl { Text ~ and ~ a \cA~ tie:?? }
% ↑ l3regex-replacement-like syntax, but not exactly identical.
% Note that you can embed it directly into the code itself
}
\ExplSyntaxOff
\end{document}
expl3code-level 'non-breaking space' function.)"? – Denis Bitouzé Jan 03 '19 at 20:42\char_generate:nn { `\~ } { 13 }– Joseph Wright Jan 03 '19 at 20:53~(~in normal catcode regime) is by default exactly equivalent to\nobreakspace{}which can be used without any issue in expl3 code. (actually, it expands to that) – user202729 Feb 04 '22 at 15:21\nobreakspace:) it would be useless for that purpose, so stick with regex or \lowercase. (my answer below might also be interesting) – user202729 Jul 09 '22 at 14:20\tl_new:N \c_nbspace_tl \tl_set:Nn \c_nbspace_tl { \tex_Uchar:D 160 }and thena \c_nbspace_tl tie; or enter the character directly or with a macro, or\Uchar160, etc, or indirectly via a\use:c{}etc, so the parsing might need to use x-expansion. – Cicada Sep 03 '22 at 06:03