1

Assuming that I have already parsed some text verbatim, using an xparse +v argument, what is the best way to typeset the text in a way that respects the original formatting?

Note that I cannot directly typeset the text using a verbatim environment, as I need to gently preprocess the text beforehand.

I have looked at how the verbatim environment set up typesetting, though it’s not clear what parts are doing what things, and I was wondering if there were a “modern” approach using currently-available LaTeX3 tools.

Addendum: After the text is parsed with with the +v argument, it is processed by some regexes, then it is split on the endline character and stored in a sequence variable as a set of lines, so that it can be written to an external file, before typesetting.

Example code:

\NewDocumentCommand \myverb { +v }
  { \kelly_verb:n { #1 } }

\tl_new:N \l__kelly_verb_body_tl
\seq_new:N \l__kelly_verb_lines_seq

\cs_new_protected:Nn \kelly_verb:n
  {
    \tl_set:Nn \l__kelly_verb_body_tl { #1 }
    \regex_replace_all:NnN \c__kelly_verb_regex { <replacement> }
      \l__kelly_verb_body_tl
    \kelly_set_split_lines:NV \l__kelly_verb_lines_seq
      \l__kelly_verb_body_tl
    \seq_map_inline:Nn \l__kelly_verb_lines_seq
       { \iow_now:Nn \g__kelly_output_iow { ##1 } }
    % Typeset here...
  }
3
  • 3
    Could you make a small compilable document to illustrate what you want to achieve? Currently it is not fully clear what your input is and what you want the output to be.
    – Marijn
    Commented Jul 28, 2020 at 20:14
  • You'll need to turn the newline characters (\char_generate:nn {13} {12}) into something that really breaks lines. Also you'll have to use \verbatim@font and have to suppress ligatures (you can't use \@noligs as that needs to turn characters active, so maybe via \tl_replace_all:Nnn for the characters you know to form ligatures, take a look at the \verbatim@nolig@list).
    – Skillmon
    Commented Jul 28, 2020 at 20:50
  • If you're free to use LuaLaTeX, do take a look at this answer to the posting How to handle verbatim material in LuaLaTeX? A related posting: How to print a verbatim block at its end automatically in LuaLaTeX?
    – Mico
    Commented Jul 28, 2020 at 21:22

1 Answer 1

3

An example of printing stuff in a verbatim like fashion.

\documentclass[]{article}

\usepackage{xparse}

\ExplSyntaxOn
\cs_generate_variant:Nn \seq_set_split:Nnn { Nnv }
\tl_new:N \l_kelly_verbatim_text_tl
\seq_new:N \l_kelly_nolig_seq
\seq_set_split:Nnv \l_tmpa_seq { \do } { verbatim@nolig@list }
\seq_remove_all:Nn \l_tmpa_seq {}
\seq_map_inline:Nn \l_tmpa_seq
  {
    \seq_push:Nx \l_kelly_nolig_seq { \char_generate:nn { `#1 } { 12 } }
  }
\cs_new_protected:Npx \__kelly_replace_newline:n #1
  {
    \tl_replace_all:Nnn \exp_not:N \l_kelly_verbatim_text_tl
      { \char_generate:nn { 13 } { 12 } }
      { #1 }
  }
\cs_new_protected:Npn \__kelly_no_ligs:
  {
    \seq_map_inline:Nn \l_kelly_nolig_seq
      {
        \tl_replace_all:Nnn \l_kelly_verbatim_text_tl { ##1 }
          { \mode_leave_vertical: \kern \c_zero_dim ##1 }
      }
  }
\cs_new_protected:Npn \__kelly_verbatim_output:n #1
  {
    \group_begin:
    \dim_set_eq:NN \parindent \c_zero_dim
    \tl_set:Nn \l_kelly_verbatim_text_tl { #1 }
    \tl_replace_all:Nnn \l_kelly_verbatim_text_tl { ~ } { \hbox:n { ~ } }
    \__kelly_replace_newline:n
      {
        \mode_leave_vertical: \kern \c_zero_dim
        \par
        \mode_leave_vertical: \kern \c_zero_dim
      }
    \__kelly_no_ligs:
    \use:c { verbatim@font }
    \l_kelly_verbatim_text_tl
    \group_end:
  }
\NewDocumentCommand \MyVerbatim { +v }
  {
    % do your stuff
    \__kelly_verbatim_output:n { #1 }
  }
\ExplSyntaxOff

\begin{document}
\MyVerbatim
{
  \test\ this \verbatim\  text.

  new  line
aa
 ne

 line
}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .