Skip to content

Commit

Permalink
Handle 'misplaced' \protect correctly in \text_expand:n
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwright committed Apr 8, 2022
1 parent 1ca596e commit 128f11e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions l3kernel/CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ this project uses date-based 'snapshot' version identifiers.
except for char code 0

### Fixed
- Handling of 'misplaced' `\protect` by `\text_expand:n`
- Nesting of `\tl_analysis_map_inline:nn`
- Naming of an error message

Expand Down
24 changes: 20 additions & 4 deletions l3kernel/l3text.dtx
Expand Up @@ -947,14 +947,15 @@
% \end{macrocode}
% \LaTeXe{}'s \cs{protect} makes life interesting. Where possible, we
% simply remove it and replace with the \enquote{parent} command; of course,
% the \cs{protect} might be explicit, in which case we need to leave it alone
% if it's required. There is also the case of a straight \tn{@protected@testopt}
% to cover.
% the \cs{protect} might be explicit, in which case we need to leave it alone.
% That includes the case where it's not even followed by an \texttt{N}-type
% token. There is also the case of a straight \tn{@protected@testopt} to
% cover.
% \begin{macrocode}
\cs_new:Npx \@@_expand_cs:N #1
{
\exp_not:N \str_if_eq:nnTF {#1} { \exp_not:N \protect }
{ \exp_not:N \@@_expand_protect:N }
{ \exp_not:N \@@_expand_protect:w }
{
\bool_lazy_and:nnTF
{ \cs_if_exist_p:N \fmtname }
Expand All @@ -963,8 +964,23 @@
{ \exp_not:N \@@_expand_replace:N #1 }
}
}
\cs_new:Npn \@@_expand_protect:w #1 \q_@@_recursion_stop
{
\tl_if_head_is_N_type:nTF {#1}
{ \@@_expand_protect:N }
{
\@@_expand_store:n { \protect }
\@@_expand_loop:w
}
#1 \q_@@_recursion_stop
}
\cs_new:Npn \@@_expand_protect:N #1
{
\@@_if_recursion_tail_stop_do:Nn #1
{
\@@_expand_store:n { \protect }
\@@_expand_end:w
}
\exp_args:Ne \@@_expand_protect:nN
{ \cs_to_str:N #1 } #1
}
Expand Down
8 changes: 8 additions & 0 deletions l3kernel/testfiles/m3text001.lvt
Expand Up @@ -90,4 +90,12 @@
\tl_show:x { \text_expand:n { \exp_not:n \scan_stop: \use:n { ~ \use_none:n } { X } { \foo } } }
}

\TEST { Dealing~with~\protect }
{
\tl_show:x { \text_expand:n { \protect \foo } }
\tl_show:x { \text_expand:n { \protect a } }
\tl_show:x { \text_expand:n { \protect } }
\tl_show:x { \text_expand:n { \protect { ab } } }
}

\END
16 changes: 16 additions & 0 deletions l3kernel/testfiles/m3text001.tlg
Expand Up @@ -64,3 +64,19 @@ l. ... }
<recently read> }
l. ... }
============================================================
============================================================
TEST 9: Dealing with \protect
============================================================
> \protect \foo .
<recently read> }
l. ... }
> \protect a.
<recently read> }
l. ... }
> \protect .
<recently read> }
l. ... }
> \protect {ab}.
<recently read> }
l. ... }
============================================================

0 comments on commit 128f11e

Please sign in to comment.