2

This question is related to (Changing escape character in \tl_set_rescan:Nnn) and (https://tex.stackexchange.com/a/496258/7832) but is not the same.

I've tried to change this:

    \exp_args:NNx \_scontents_append_contents:nn \l_scontents_name_seq_tl 
      {
       \exp_not:N \scantokens \exp_after:wN { \l_tmpa_tl } %
      }

for something else in the expl3 way using \tl_set_rescan:Nnn, like this:

    \exp_args:NNx \_scontents_append_contents:nn \l_scontents_name_seq_tl 
      {
       < use \tl_set_rescan:Nnn over \l_tmpa_tl to obtain the same effect >
      }

But I can't get the same effect, I don't know what to put in {<setup>} to get the desired result. The MWE file:

\documentclass{article}
\usepackage{filecontentsdef}[2019/04/20]
\usepackage{xparse}
\usepackage{fvextra} 
\setlength{\parindent}{0pt} % just for the example
\ExplSyntaxOn

\keys_define:nn { scontents }
  {
    save-env  .tl_set:N   = \l_scontents_name_seq_tl,
    save-env  .initial:n  = contents,
    show-env  .bool_set:N = \l_scontents_show_env_bool,
    show-env  .initial:n  = false  
  }

% Adapted from https://tex.stackexchange.com/a/215571/7832
\cs_new_protected:Npn \_scontents_append_contents:nn #1 #2
  {
    \seq_if_exist:cF { g_scontents_seq_name_#1_seq }
      {
        \seq_new:c { g_scontents_seq_name_#1_seq }
      }
    \seq_gput_right:cn { g_scontents_seq_name_#1_seq } { #2 }
  }

\cs_new_protected:Npn \_scontents_getfrom_seq:nn #1 #2
  {
    \seq_item:cn { g_scontents_seq_name_#2_seq } { #1 }
  }

\ProvideExpandableDocumentCommand{\getstored}{ O{1} m }
  {
    \_scontents_getfrom_seq:nn { #1 } { #2 }
  }

% Define scontents (wrap \filecontentsdefmacro) whit [key=val] Delaying
% Adapted from https://tex.stackexchange.com/a/487746/7832
\ProvideDocumentEnvironment{ scontents }{}
  {
    \char_set_catcode_active:N \^^M
    \scontents_start_environment:w
  }
  {
    \scontents_stop_environment:
    \scontents_atend_environment:
  }

\cs_new_protected:Npn \scontents_environment_keys:w [#1]
  {
    \keys_set:nn { scontents } { #1 }
  }

% Star environment
\group_begin:
\char_set_catcode_active:N \^^M
\cs_new_protected:Npn \scontents_start_environment:w #1 ^^M
  {
    \tl_if_blank:nF { #1 } { \scontents_environment_keys:w #1 }
    \group_begin: % open group for env
    \use:c { filecontentsdefmacro } { \l_tmpa_tl } ^^M 
  }
\group_end:

% Stop environment
\cs_new_protected:Nn \scontents_stop_environment:
  {
    \endfilecontentsdefmacro 
    \group_end:  % close group for env
  }

% A variant to replace \^^M for \^^J (need by Verb{..} from fvextra)
% https://tex.stackexchange.com/a/8971/7832
\cs_generate_variant:Nn \tl_replace_all:Nnn { Nxx } 

% Expand \l_tmpa_tl and pass to seq
\cs_gset_protected:Nn \_scontents_macro_to_seq:
  { 
    \regex_replace_all:nnN { \^^M } { \^^J } \l_tmpa_tl
    \cs_log:N \l_tmpa_tl
    \exp_args:NNx \_scontents_append_contents:nn \l_scontents_name_seq_tl 
      {
       \exp_not:N \scantokens \exp_after:wN { \l_tmpa_tl } %
      }
  }

% Code after scontent environment 
\cs_new_protected:Nn \scontents_atend_environment:
  {
    \_scontents_macro_to_seq:
    \bool_if:NT \l_scontents_show_env_bool
      {
        \_scontents_getfrom_seq:nn { -1 }{ \l_scontents_name_seq_tl }
      }
    \cs_undefine:N \l_tmpa_tl
  }
\ExplSyntaxOff
\begin{document}
\section*{Test environment}
Test \verb+\begin{scontents}+ no \verb+[key=val]+\par

\begin{scontents}
Using \verb+scontents+ env no \verb+[key=val]+, save in \verb+contents+ 
with index $1$\footnote{AND footnotes !!}.%

Prove new \Verb*{ new fvextra whit braces } and environment \verb+Verbatim+

\begin{Verbatim}
(A) verbatim environment                                               %
many space here and percent at end of line                             %
                                many space                   
\end{Verbatim}
No espace after, see \verb+https://tex.stackexchange.com/a/460967/7832+.\par
Use \verb|\relax|.\relax
\end{scontents}

Test \verb+\begin{scontents}[save-env=other]+\par

\begin{scontents}[save-env=other]
    Now Using \verb+scontents+ env with \verb+[save-env=other]+, save in  
\verb+other+ with index $1$.

\begin{verbatim*}
      (B) verbatim environment

      kkkkkkkkkkkkkkkkkkkkkkkk
\end{verbatim*}
Show a space. 
\end{scontents}

\section*{Show stored contents}
XX\getstored[1]{contents}NO space here :)\par

YY\getstored[1]{other}A space here :)

\end{document}

Greetings.

  • You are ending up with content that is just \scantokens{<a load of tokens>}, so you are not setting anything at point-of-use. You want \tl_rescan:nn. – Joseph Wright Jun 25 at 7:10
  • Also you don't actually seem to have any set-up – Joseph Wright Jun 25 at 7:16
  • I think I've misunderstood the operation of the rescan and rescan. Replacing \scantokens with \tl_rescan:nn {} {\l_tmpa_tl } does not get the same result. – Pablo González L Jun 25 at 14:42
  • @JosephWright: Can you explain to me a little bit how \tl_rescan works. If I can't use it in my case, your explanation would help me learn and close the question, greetings. – Pablo González L Jun 26 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.