# verbatim code inside \newcommand definition

All the questions I could find on this subject are about people who want to use \verb in the argument passed to a command, or about people who want within a \newcommand to wrap one of the arguments within a \verb(atim).

Instead, I just want to use a \verbatim environment within something like a \newcommand. More specifically, I've tried the following:

\newcommand \MyFoo {
blabla
\begin{verbatim}
hihi
\end{verbatim}
}


and as you probably all know, this doesn't work: the definition goes through, but when I try to use \MyFoo I get an error about File ended while scanning use of \@xverbatim.).

Strictly speaking, I don't need to use \newcommand. What I have is a bunch of chunks (which contain plain normal text, as well as verbatim elements) stored in a file, and then I want to construct various documents from this by selecting various of those chunks: in one document I might want to use chunk 4, 7, and 9, while in another I might want 4, 6, 7, and 8.

The only solution I could find so far is to store each chunk in a separate file (so I just \input the relevant chunk instead of calling a command containing the chunk), but editing hundreds of small files is much too inconvenient, so I'd much rather keep all the chunks in a single file.

I'm thinking of writing a script which extracts the relevant chunks, but I'm still hoping there's a more direct solution. Now that I think about it, I could also live with someway to set the list of enabled chunks somewhere, then \input the chunks-file and within that file have each chunk be conditionalized on its inclusion in the list of enabled chunks.

• Verbatim invocations are not allowed as part of a macro definition. However, depending on your need, perhaps \detokenize would suffice, or even \texttt in some cases. Nov 20, 2017 at 21:29
• I assume you're using pdfLaTeX at present to compile your LaTeX documents. Is this assumption correct? Are you free to use LuaLaTeX? If so, the postings How to print a verbatim block at its end automatically in LuaLaTeX and How to handle verbatim material in LuaLaTeX may be of interest to you.
– Mico
Nov 20, 2017 at 21:47
• @StevenB.Segletes: I do use \texttt at many places, but for multiline code samples, this is impractical. @Mico: yes, I'm using pdflatex. I'll take a look at your lualatex links, tank you. Nov 20, 2017 at 21:59
• I think you need to provide a small coded example of what you are trying to do, as it still is a bit vague by your description. Nov 20, 2017 at 22:01
• Of course, for something this simple one could use \texttt{hihi} Nov 20, 2017 at 23:13

\documentclass{article}

\begin{document}

\newsavebox\MyFooA
\begin{lrbox}{\MyFooA}
\begin{minipage}{\textwidth}
blabla
\begin{verbatim}
hihi
\some \command
& % in verbatim
\end{verbatim}
\end{minipage}
\end{lrbox}

\newsavebox\MyFooB
\begin{lrbox}{\MyFooB}
\begin{minipage}{\textwidth}
a second
\begin{verbatim}
in classic \TeX you
would run out of boxes
but have ~32 thousand these days
\end{verbatim}
\end{minipage}
\end{lrbox}

\begin{center}
\usebox{\MyFooB}
\end{center}

and

\begin{center}
\usebox{\MyFooA}
\end{center}

and B again

\begin{center}
\usebox{\MyFooB}
\end{center}

\end{document}

• The \subsection commands work a bit weirdly in such lrboxes, but I think I can live with it. Thanks a lot! Nov 22, 2017 at 3:42