# Passing content as argument (or macro to be expanded) to verbatim environment

Is it possible to custom what it's inside a verbatim environment? Suppose I want to print as verbatim something to be calculated on the fly, inserted in a kind of verbatim template. Think for example in printing an algorithm where the number of steps (or any other parameter) is to be calculated outside the verbatim environment, and then passed to it. I don't know if as a macro to be expanded or as an argument.

This is a kind of twist of this other question, but adapted to verbatim environment. I have the feeling that if the answer works for verbatim, so it will with sagetex.

Any ideas?

The MWE (please note the comment just below \begin{verbatim}:

\documentclass{article}%
\PassOptionsToPackage{T1}{fontenc}
\usepackage{fontenc}
\usepackage[utf8]{inputenc}

\pagestyle{empty}

\begin{document}
\def\np{15}

\begin{verbatim}
np=\np #THIS IS WHAT I WANT TO GET, WHEN PRINTED, IT SHOULD BE np=15
p1=0
pcoeffs=range(np+1)
x = var('x')
for i in range(np+1):
pcoeffs[i]= Integer(randint(-8,8))
p1=p1+pcoeffs[i]*x^i
\end{verbatim}
\end{document}

• This looks like typesetting source code, being possible,for example,with listings and its escape to LaTeX method – user31729 Nov 11 '14 at 8:55
• Or Verbatim from fancyvrb – egreg Nov 11 '14 at 8:57
• Thanks @ChristianHupfer, but the thing is that I'd want to know about a general method to do this, so it can also applies to sagetex (see link to my prior question) – Pablo B. Nov 11 '14 at 8:57

If your verbatim doesn't use backslashes and braces, you can do it with fancyvrb:

\documentclass{article}%
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{fancyvrb}

\pagestyle{empty}

\begin{document}
\def\np{15}

\begin{Verbatim}[commandchars=\\\{\}]
np=\np #THIS IS WHAT I WANT TO GET, WHEN PRINTED, IT SHOULD BE np=15
p1=0
pcoeffs=range(np+1)
x = var('x')
for i in range(np+1):
pcoeffs[i]= Integer(randint(-8,8))
p1=p1+pcoeffs[i]*x^i
\end{Verbatim}
\end{document}


Alternatively, you can use a variant of the code in my answer to Partial expansion: macro arguments and nothing more

\documentclass{article}
\usepackage{xparse,l3regex,verbatim}

\ExplSyntaxOn
\NewDocumentCommand{\tofile}{m}
{
\manual_tofile:n { #1 }
}

\iow_new:N \g_manual_tofile_write_stream
\tl_const:Nn \c_manual_specials_tl { \  \\ \{ \} \\$ \& \# \^ \_ \% \~ }
\tl_new:N \l_manual_argument_tl
\tl_new:N \l_manual_line_tl
\seq_new:N \l_manual_arguments_seq
\int_new:N \l_manual_arguments_int

\cs_new_protected:Npn \manual_dospecials:
{
\tl_map_inline:Nn \c_manual_specials_tl
{
\char_set_catcode_other:N ##1
}
}

\cs_new_protected:Npn \manual_tofile:n #1
{
\group_begin:
\tex_endlinechar:D \^^J
\iow_open:Nn \g_manual_tofile_write_stream { #1 }
\manual_tofile_aux:xw
}

\cs_new_protected:Npn \manual_tofile_aux:nw #1 #2 ~%
{% #1 is the list of arguments, #2 is the terminator
\tl_set:Nn \l_manual_eof_tl { #2 }
\tl_trim_spaces:N \l_manual_eof_tl
\seq_set_split:Nnn \l_manual_arguments_seq { } { #1 }
\int_set:Nn \l_manual_arguments_int { \seq_count:N \l_manual_arguments_seq }
\manual_dospecials:
}

\cs_generate_variant:Nn \manual_tofile_aux:nw { x }

{
\str_if_eq:VnTF \l_manual_eof_tl { #1 }
{
\iow_close:N \g_manual_tofile_write_stream
\group_end:
}
{
\manual_replace_write:n { #1 }
}
}

\cs_new_protected:Npn \manual_replace_write:n #1
{
\tl_set:Nn \l_manual_line_tl { #1 }
\int_step_inline:nnnn { 1 } { 1 } { \l_manual_arguments_int }
{
\tl_set:Nx \l_manual_argument_tl { \seq_item:Nn \l_manual_arguments_seq { ##1 } }
\regex_replace_all:nnN { \# ##1 } { \u{l_manual_argument_tl} } \l_manual_line_tl
}
\iow_now:NV \g_manual_tofile_write_stream \l_manual_line_tl
}

\cs_generate_variant:Nn \iow_now:Nn { NV }
\cs_generate_variant:Nn \str_if_eq:nnTF { V }

\ExplSyntaxOff

\newcommand{\np}{15}

\begin{document}
\tofile{\jobname.tmp}{{\np}}<<EOF
np=#1
p1=0
pcoeffs=range(np+1)
x = var('x')
for i in range(np+1):
pcoeffs[i]= Integer(randint(-8,8))
p1=p1+pcoeffs[i]*x^i
<<EOF
\verbatiminput{\jobname.tmp}

\end{document}


You can use #1` and so on for denoting tokens to be substituted; the respective substitutions should be a list of braced arguments that are subject to full expansion.