[latex3-commits] [git/LaTeX3-latex3-latex3] main: Handle 'misplaced' \protect correctly in \text_expand:n (128f11efa)

Joseph Wright joseph.wright at morningstar2.co.uk
Fri Apr 8 10:36:27 CEST 2022


Repository : https://github.com/latex3/latex3
On branch  : main
Link       : https://github.com/latex3/latex3/commit/128f11efa27b27cd1b4c8752059dbc268fe0bec3

>---------------------------------------------------------------

commit 128f11efa27b27cd1b4c8752059dbc268fe0bec3
Author: Joseph Wright <joseph.wright at morningstar2.co.uk>
Date:   Fri Apr 8 09:36:27 2022 +0100

    Handle 'misplaced' \protect correctly in \text_expand:n


>---------------------------------------------------------------

128f11efa27b27cd1b4c8752059dbc268fe0bec3
 l3kernel/CHANGELOG.md            |  1 +
 l3kernel/l3text.dtx              | 24 ++++++++++++++++++++----
 l3kernel/testfiles/m3text001.lvt |  8 ++++++++
 l3kernel/testfiles/m3text001.tlg | 16 ++++++++++++++++
 4 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/l3kernel/CHANGELOG.md b/l3kernel/CHANGELOG.md
index fc57e49a6..b52e7f920 100644
--- a/l3kernel/CHANGELOG.md
+++ b/l3kernel/CHANGELOG.md
@@ -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
 
diff --git a/l3kernel/l3text.dtx b/l3kernel/l3text.dtx
index 0d1365c8d..56c42562c 100644
--- a/l3kernel/l3text.dtx
+++ b/l3kernel/l3text.dtx
@@ -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 at 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 at 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 }
@@ -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
   }
diff --git a/l3kernel/testfiles/m3text001.lvt b/l3kernel/testfiles/m3text001.lvt
index cdd66f177..2e7c391a1 100644
--- a/l3kernel/testfiles/m3text001.lvt
+++ b/l3kernel/testfiles/m3text001.lvt
@@ -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
diff --git a/l3kernel/testfiles/m3text001.tlg b/l3kernel/testfiles/m3text001.tlg
index e7b0a95bf..f38bbe0bd 100644
--- a/l3kernel/testfiles/m3text001.tlg
+++ b/l3kernel/testfiles/m3text001.tlg
@@ -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. ...  }
+============================================================





More information about the latex3-commits mailing list.