27

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?

yo'
  • 51,322
Sean Allred
  • 27,421

2 Answers2

25

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.)

Joseph Wright
  • 259,911
  • 34
  • 706
  • 1,036
  • 1
    Is there any update about "(Note that at present there is no expl3 code-level 'non-breaking space' function.)"? – Denis Bitouzé Jan 03 '19 at 20:42
  • 2
    @DenisBitouzé That's still true, though you could now \char_generate:nn { `\~ } { 13 } – Joseph Wright Jan 03 '19 at 20:53
  • 1
    @JosephWright Sorry to ask again: any change in the answer to Denis's question in the 2 1/2 years or so since? – Noldorin Sep 24 '21 at 01:07
  • 1
    @Noldorin Why would you need anyway? An active ~ (~ 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
  • @user202729 For a parsing macro. – Noldorin Feb 04 '22 at 15:48
  • @Noldorin Ah, I only understand what that mean re-reading that comment now ("parsing" means "parse a user-provided token list"). In that case even if expl3 provides some interface to typeset a non-breaking space (e.g. \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
  • @Noldorin If Unicode fonts are being used, slot 160 is NBSP (no-break space), so the user could do \tl_new:N \c_nbspace_tl \tl_set:Nn \c_nbspace_tl { \tex_Uchar:D 160 } and then a \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
  • @user202729 Yes, that's what I meant; I should have been more explicit. What do you mean by "useless for that purpose", however? – Noldorin Sep 04 '22 at 00:32
  • @Cicada That's good to know, thanks. – Noldorin Sep 04 '22 at 00:37
2

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}

user202729
  • 7,143
  • Remark, I think you need a sufficiently new expl3 version, otherwise the nested tl-analysis map bug will make the result incorrect. (or include my tlanalysispatch.sty for a temporary patch to the kernel until you update it.) – user202729 Aug 17 '22 at 01:39