TeX - LaTeX Asked by Kelly Smith on May 12, 2021
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...
}
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}
Correct answer by Skillmon on May 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP