TransWikia.com

Reading Lines Verbatim into a Sequence Variable

TeX - LaTeX Asked on September 29, 2021

I would like to create an “environment” (i.e. delimited parser) that reads lines verbatim and stores them in a sequence variable.

I have tried using an xparse +v type argument, then splitting the parsed token list on the endline character, like so:

seq_set_split:Nxn l__ks_verb_lines_seq { char_generate:nn { 13 } { 12 } } { #1 }

where #1 is the verbatim tokens. Unfortunately, this approach strips the indentation from the lines, which makes them unsuitable for my purposes.

I do not require that xparse be used.

One Answer

The description of seq_set_split:Nnn mentions that it strips spaces. We can define a simplified version that does not. Here, I've not fully separated out from seq internals, which formally I should do (one could create a 'psuedo-seq' then set a real one from it):

documentclass{article}
usepackage{xparse}
ExplSyntaxOn
cs_new_protected:Npx ks_set_split:Nn #1#2
  {
    tl_set:Nn #1
      {
        exp_not:N __ks_set_split:w exp_not:N prg_do_nothing:
        #2
        exp_not:N __ks_set_split_end:
      }
    tl_replace_all:Nnn #1 { char_generate:nn { `^^M } { 12 } }
      {
        exp_not:N __ks_set_split_end:
        exp_not:N __ks_set_split:w exp_not:N prg_do_nothing:
      }
    tl_set:Nx #1 { s__seq #1 }
  }
cs_new:Npn __ks_set_split:w #1 __ks_set_split_end:
  { exp_args:No __seq_wrap_item:n {#1} }
NewDocumentCommand foo { +v }
  {
    ks_set_split:Nn l__ks_verb_lines_seq { #1 }
    seq_show:N l__ks_verb_lines_seq
  }
ExplSyntaxOff

begin{document}
foo{some

   stuff
   
   which
is
 indented}

end{document}

A feature request for a 'non-space stripping' version of seq_set_split:Nnn is likely the best longer-term fix here: I suspect you are not the only user who wishes to do something like this.

Correct answer by Joseph Wright on September 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP