# Verbatim text in token registers

How can I add verbatim text in a token register (I want to accumulate pieces of code in a toks register and print them in an appendix). In the MWE below if you substitute verbatim text it fails.

\documentclass{article}
\begin{document}
\newtoks\test
\test={one }
\test=\expandafter{\the\test test}
\the\test
\end{document}

-
What do you mean by "substitute verbatim text" ? Perhaps you can put the verbatim text in a box \mybox and then \test=\expandafter{\the\test \box\mybox} –  Alain Matthes Mar 10 '11 at 7:53
What do you mean exactly with "substitute verbatim text"? You mean you like to add pieces of code containing \verb macros or verbatim environments? Or should the text completely be read verbatim? You should add these cases to you MWE. It should always show what doesn't works. –  Martin Scharrer Mar 10 '11 at 11:22
@Martin Scharrer @Altermundus Sorry if I was not clear, I would like to enclose chunks of verbatim environment texts. –  Yiannis Lazarides Mar 10 '11 at 13:31
I would write the code to an external file instead and reread it again when you need it. It avoids this issues and is much faster if you add a lot of material multiple times. Something based on filecontents, but which allows to have multiple environments which all write to the same file. –  Martin Scharrer Mar 10 '11 at 14:43

EDIT: as we added a verbatim functionality to the xparse package, manipulating verbatim text becomes easier. For this to work, you will need a version of xparse later than 15/08/2011. I used token lists instead of toks, but that's mostly irrelevant.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
{ \tl_put_right:Nn #1 { #2 } }
\NewDocumentCommand{\useverb}{m}
{
\exp_args:Nx \scantokens
{ \token_to_str:N \verb + \tl_to_str:N #1 + }
}
\tl_new:N \test
\ExplSyntaxOff
\begin{document}
\useverb\test
\end{document}

-

The problem is that \verb operates by making the first character active and then using that to end the group that restores all of the font changes and category code changes it made.

\documentclass{article}
\begin{document}
\newtoks\test
\test={\verb~one~ }
\test=\expandafter{\the\test test}
\the\test
\end{document}


This works since ~ is already active. However, this isn't very useful because the main use of \verb is to avoid having to escape things like % or &. That isn't going to work here since these get tokenized when you put them in a token register. Instead, you should just use \texttt.

-

I would write the code to an external file instead and reread it again when you need it. It avoids this issues and is much faster if you add a lot of material multiple times. Something based on filecontents or VerbatimOut of fancyvrb, but which allows to have multiple environments which all write to the same file:

### Code:

% codewrite.sty
\RequirePackage{fancyvrb}

\newcommand*\CodeOut{\FV@Environment{}{CodeOut}}

\newwrite\@codewrite
% Or to save an output handle:
%\let\@codewrite\FV@OutFile

\newcommand*\CodeOpenFile[1]{%
\immediate\openout\@codewrite #1\relax
\def\CodeOpen@File{#1}%
}

\def\FVB@CodeOut{%
\@bsphack
\begingroup
\FV@UseKeyValues
\FV@DefineWhiteSpace
\def\FV@Space{\space}%
\def\FV@ProcessLine{\immediate\write\@codewrite}%
\let\FV@FontScanPrep\relax
%% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
\let\@noligs\relax
%% DG/SR modification end
\FV@Scan
}
\def\FVE@CodeOut{\endgroup\@esphack}

\DefineVerbatimEnvironment{CodeOut}{CodeOut}{}


### Usage Example:

\documentclass{article}

\usepackage{codewrite}

\CodeOpenFile{\jobname.cod}

\begin{CodeOut}
Test
\end{CodeOut}

\begin{document}

\begin{CodeOut}
\begin{verbatim}
test%^&\$%\relax
\end{verbatim}
\end{CodeOut}


There might be a package which does this already. Note that you can catch the content of a file to a macro using the catchfile package.
You probably right that writing to a file maybe the best possible solution, but I am sure one can get it into toks as well. Is \ReadCode supposed to read the file? It does not produce any output in the example. –  Yiannis Lazarides Mar 10 '11 at 16:01