# Locally redefining ~ in code that uses expl3 facilities

I am trying to rewrite a package to use expl3 facilities.

Here I need to define an \initializeplaintitle command that stores a "purified" version of the document title, retrieved from \@title into a macro. The current (LaTeX2e) version is

\newcommand\initializeplaintitle[1][]{%
{%
\def\\{ }%
\def~{ }%
\def\emph##1{##1}%
#1%
\protected@xdef\plaintitle{\@title}%
}%
}


As you can see, it makes some local assignments so that any \\ and ~ present in the title become spaces and it strips out all \emph commands.

I wonder how I can obtain the same result in a package that is written using the expl3 syntax and facilities. In principle, the \def should become \tl_set:Nn. But how can I act on the ~ given that this is redefined in the expl3 syntax?

You can use \char_set_active:nN

\char_set_active:nN { \~ } \c_space_tl


This gives a meaning to the character specified in the first argument in a context where it is active (which ~ is in normal situations).

You also have to condense spaces, so probably something like

\documentclass{article}
\usepackage{xparse}

\title{A \emph{title} \\ about \emph{A.~Uthor}} % to initialize \@title

\ExplSyntaxOn

\tl_new:N \g_callegar_title_pure_tl
\tl_new:N \l__callegar_title_pure_tl

\cs_new_protected:Nn \callegar_purify_title:
{
\tl_set_eq:Nc \l__callegar_title_pure_tl { @title }
% \emph{...} -> ...
\regex_replace_all:nnN { \c{emph}\cB. (.*?) \cE. } { \1 } \l__callegar_title_pure_tl
% \\ and ~ -> space
\regex_replace_all:nnN { (\c{\\}|\cA\~) } { \  } \l__callegar_title_pure_tl
% multiple spaces to a single space
\regex_replace_all:nnN { \s+ } { \  } \l__callegar_title_pure_tl
% globally set the purified title
\tl_gset_eq:NN \g_callegar_title_pure_tl \l__callegar_title_pure_tl
}

\callegar_purify_title:
\tl_show:N \g_callegar_title_pure_tl

\stop


The output will be

> \g_callegar_title_pure_tl=A title about A. Uthor.

• The second solution using regexps is clear to me, even if not completely compatible with the previous version of my code (sorry, I failed to give you enough context --- the #1 in the latex2e code that I gave you was meant to pass to the code other substitutions in the form of \defs as in \initializeplaintitle{\def\extrainfo#1{}}). I'll try to make some experiments with the \char_set_active:nN { \~ } \c_space_tl to see if I can work out another solution (even if the one with the regexps is much nicer and I plan to move to something like this later on). – Callegar Dec 23 '18 at 9:18