Guest User

\tex_scantokens D:

a guest
Jun 30th, 2019
89
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \documentclass{article}
  2. \usepackage{filecontentsdef}[2019/04/20]
  3. \usepackage{xparse}
  4. \usepackage{fvextra}
  5. \usepackage{newvbtm}
  6. \setlength{\parindent}{0pt} % just for the example
  7. \ExplSyntaxOn
  8. \tl_new:N \l__scontents_macro_tmp_tl
  9. \keys_define:nn { scontents }
  10.  {
  11.    save-env  .tl_set:N   = \l__scontents_name_seq_tl,
  12.    save-env  .initial:n  = contents,
  13.    show-env  .bool_set:N = \l__scontents_show_env_bool,
  14.    show-env  .initial:n  = false
  15.  }
  16. % Adapted from https://tex.stackexchange.com/a/215571/7832
  17. \cs_new_protected:Npn \__scontents_append_contents:nn #1#2
  18.   {
  19.    \seq_if_exist:cF { g__scontents_seq_name_#1_seq }
  20.      { \seq_new:c { g__scontents_seq_name_#1_seq } }
  21.    \seq_gput_right:cn { g__scontents_seq_name_#1_seq } {#2}
  22.  }
  23. \cs_new_protected:Npn \__scontents_getfrom_seq:nn #1#2
  24.  { \seq_item:cn { g__scontents_seq_name_#2_seq } {#1} }
  25. \NewExpandableDocumentCommand { \getstored } { O{1} m }
  26.  { \__scontents_getfrom_seq:nn {#1} {#2} }
  27. % Define scontents (wrap \filecontentsdefmacro) whit [key=val] Delaying
  28. % Adapted from https://tex.stackexchange.com/a/487746/7832
  29. \NewDocumentEnvironment { scontents } { }
  30.  {
  31.    \char_set_catcode_active:N \^^M
  32.    \scontents_start_environment:w
  33.  }
  34.  {
  35.    \scontents_stop_environment:
  36.    \scontents_atend_environment:
  37.  }
  38. \cs_new_protected:Npn \scontents_environment_keys:w [ #1 ]
  39.  { \keys_set:nn { scontents } {#1} }
  40. \group_begin:
  41.  \char_set_catcode_active:N \^^M
  42.  \cs_new_protected:Npn \scontents_start_environment:w #1 ^^M
  43.    {
  44.      \tl_if_blank:nF {#1} { \scontents_environment_keys:w #1 }
  45.       \group_begin: % open group for env
  46.         \filecontentsdefmacro { \l__scontents_macro_tmp_tl } ^^M
  47.    }
  48.   \cs_new_protected:Nn \scontents_stop_environment:
  49.     {
  50.         \endfilecontentsdefmacro
  51.       \group_end: % close group for env
  52.     }
  53. \group_end:
  54. \cs_gset_protected:Nn \__scontents_macro_to_seq:
  55.   {
  56.    \regex_replace_all:nnN { \^^M } { \^^J } \l__scontents_macro_tmp_tl
  57.    \cs_log:N \l__scontents_macro_tmp_tl
  58.    \exp_args:NNx \__scontents_append_contents:nn \l__scontents_name_seq_tl
  59.      { \exp_not:N \tex_scantokens:D { \tl_use:N \l__scontents_macro_tmp_tl } }
  60.  }
  61. % Code after scontent environment \seq_item:cn { g__scontents_seq_name_#2_seq } { #1 }
  62. \cs_new_protected:Nn \scontents_atend_environment:
  63.   {
  64.    \__scontents_macro_to_seq:
  65.    \bool_if:NT \l__scontents_show_env_bool
  66.      { \__scontents_getfrom_seq:nn { -1 } { \l__scontents_name_seq_tl } }
  67.    \tl_clear:N \l__scontents_macro_tmp_tl
  68.  }
  69. % \newverbatim{typescverb}{}{}{}{}
  70. \DefineVerbatimEnvironment{typescverb}{Verbatim}{}
  71. \cs_new_eq:Nc \__scontents_fcdef_get:NN { filecontentsdef@get }
  72. \cs_new:Npn \__scontents_strip_scantokens:N #1
  73.  { \exp_args:NV \__scontents_strip_scantokens:n #1 }
  74. \cs_new:Npn \__scontents_strip_scantokens:n #1
  75.  { \exp_not:o { \use_ii:nn #1 } }
  76. \group_begin:
  77.  \char_set_catcode_active:N \^^M
  78.  \cs_new:Npn \__scontents_fcdef_print:N #1
  79.    {
  80.      \cs_set_eq:NN \__scontents_fcdef_saved_EOL: ^^M
  81.      \cs_set_eq:NN ^^M \scan_stop:
  82.      \use:x
  83.        {
  84.          \exp_not:N \tex_scantokens:D
  85.            {
  86.              \exp_not:N \begin{typescverb} ^^M
  87.              \__scontents_strip_scantokens:N #1
  88.              \c_backslash_str end \c_left_brace_str typescverb \c_right_brace_str ^^M
  89.            }
  90.        }
  91.      \cs_set_eq:NN ^^M \__scontents_fcdef_saved_EOL:
  92.    }
  93. \group_end:
  94. \NewDocumentCommand { \typestored } { O{1} m }
  95.  {
  96.    \group_begin:
  97.    \tl_put_right:Nx \l_tmpb_tl
  98.      { \seq_item:cn { g__scontents_seq_name_#2_seq } {#1} }
  99.    \__scontents_fcdef_get:NN \__scontents_fcdef_print:N \l_tmpb_tl
  100.    \group_end:
  101.  }
  102. \ExplSyntaxOff
  103. \begin{document}
  104.  
  105. \section*{Test environment}
  106. Test \verb+\begin{scontents}+ no \verb+[key=val]+\par
  107.  
  108. \begin{scontents}
  109. Using \verb+scontents+ env no \verb+[key=val]+, save in \verb+contents+
  110. with index $1$\footnote{AND footnotes !!}.%
  111.  
  112. Prove new \Verb*{ new fvextra whit braces } and environment \verb+Verbatim+
  113.  
  114. \begin{Verbatim}
  115. (A) verbatim environment                                               %
  116. many space here and percent at end of line                             %
  117.                                 many space                  
  118. \end{Verbatim}
  119. No espace after, see \verb+https://tex.stackexchange.com/a/460967/7832+.\par
  120. Use \verb|\relax|.\relax
  121. \end{scontents}
  122.  
  123. \section*{Show stored contents}
  124. XX\getstored[1]{contents}NO space here :)\par
  125.  
  126. \section*{typestored}
  127. \typestored[1]{contents} % not work
  128. \end{document}
RAW Paste Data