3
\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\catcode`#=11
\str_set:Nn \l_tmpa_str {#}
\tl_analysis_show:N \l_tmpa_str
\str_set:Nn \l_tmpb_str {#}
\tl_analysis_show:N \l_tmpb_str
\ExplSyntaxOff

The above code gives

The token list \l_tmpa_str contains the tokens:
>  # (the character #)
>  # (the character #).
<recently read> }

l.7 \tl_analysis_show:N \l_tmpa_str

?
The token list \l_tmpb_str contains the tokens:
>  # (the character #).
<recently read> }

l.9 \tl_analysis_show:N \l_tmpb_str

?

Why there are two # in \l_tmpa_str but only one in \l_tmpb_str?

1

1 Answer 1

7

Don't forget that in \ExplSyntaxOn a line end doesn't make a space, so your input is essentially the same as:

\catcode`#=11\str_set:Nn\l_tmpa_str{#}

then TeX, looking for a termination to the integer 11, expands \str_set:Nn which grabs #6, freezing its catcode, and then it gets doubled by \tl_to_str:n (in \str_set:Nn), and then \tl_analysis_show:N sees #12#12. Using the proper expl3 function for changing catcodes solves the problem (or adding a ~ at the end of the line):

\char_set_catcode_letter:N \#
% \catcode`\#=11~
\str_set:Nn \l_tmpa_str {#}
\tl_analysis_show:N \l_tmpa_str
5
  • 1
    Great explanation! It's interesting to note that the ~ is not ignored at the end of a line, but would be removed as part of tokenizing a control word as in \cs_new:Npn \foo { a \c_empty_tl ~ b } (\cs_show:N \foo would print \foo=\long macro:->a\c_empty_tl b.).
    – frougon
    Commented Mar 3, 2020 at 14:42
  • @frougon That's right. Though it's not ignored at the end of that line because the space follows a character token. If the line ended with a control sequence token, the ~ would be ignored as well. Commented Mar 3, 2020 at 14:59
  • (a character token with catcode 12). Of course, but this would be due to tokenizing the control word, not to the end of the line. Unless I'm saying crap. :-)
    – frougon
    Commented Mar 3, 2020 at 15:01
  • @frougon No, you are correct! That was be being sloppy when stating rules (I probably learned this from The TeXbook, with Knuth always telling half-truths :-) Commented Mar 3, 2020 at 15:03
  • 1
    Yup, formulating such rules perfectly precisely is very difficult. Thanks!
    – frougon
    Commented Mar 3, 2020 at 15:04

You must log in to answer this question.

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