# How to make a macro that prints verbatim text?

I'm trying to do automatic linebreaking right now with verbatim, and I have this setup.

\usepackage{fancyvrb}
\usepackage{fvextra}

\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
text text text text text \macro{text}
\end{Verbatim}


The setup I have right now is a tad cumbersome, and what I'm trying to do right now is code a macro that would let me put in all of that automatically, something like this:

\newcommand{\bigvrb}[1]{
\begin{Verbatim}[breaklines=true,numbers=left,breakafter=\},breakbefore=\\]
#1
\end{Verbatim}}


so that I can do everything a tad easier and, if I want to change the format, I can edit it en masse. Problem is, it just typesets this as #1 because that's how verbatim works. I've tried using \expandafter and things like that but nothing's really working. Do y'all have any ideas? Thanks so much!!!

• You cannot have a verbatim environment as the argument to another command. Jul 19 at 19:39
• If you don't need the full verbatim capability (e.g., if you don't need % and if all your braces will be balanced), then \detokenize could work for you, with automatic line breaking. The % limitation can be overcome with a catcode change, but not the balanced brace requirement. Jul 20 at 14:54
• @StevenB.Segletes Be aware that \detokenize doubles hashes and inserts spaces behind control-word-tokens. Besides this you may also need a catcode-change of the space-character and the horizontal-tab-character and the ^ as otherwise consecutive spaces collapse into a single space and ^^-notation still works. Try, e.g.: \documentclass{article}\begin{document}\expandafter\texttt\expandafter{\detokenize{\TeX \TeX\TeX# A B^^JC}}\end{document} Jul 20 at 15:04
• @UlrichDiez You make excellent points. Perhaps then, my \altdetokenize at tex.stackexchange.com/questions/34580/escape-character-in-latex/… can overcome a number of those issues. Jul 20 at 15:18
• @StevenB.Segletes I am aware of your tokcycle-package and I like it. :-) In my answer to the question "How to \futurelet the token after a space" a \romannumeral-expansion-based macro \StringifyNAct is provided which applies \string to each token of its argument and hereby takes brace-nesting into account. Perhaps a similar routine can be applied to a macro-argument that got tokenized while space and horizontal tab and carriage return and ^ had catcode 12. Jul 20 at 15:38

The fancyvrb package allows you to define your own environments. If you use this you can just change the definition:

\documentclass[]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\DefineVerbatimEnvironment{MyVerbatim}{Verbatim}{breaklines=true,numbers=left,breakafter=\},breakbefore=\\}

\begin{document}

\begin{MyVerbatim}
text text text text text \macro{text}
\end{MyVerbatim}

\end{document}

• Thanks! This does exactly what I wanted :) Jul 19 at 19:00

I cannot recommend using a macro with undelimited-argument-syntax for producing multiline-verbatim-material:

The question of handling indentation of the source code will cause confusion because with verbatim-material indentation-spaces in the source-code are tokenizend and therefore show up as horizontal space/visible space in the output.

In order to exhibit some problems/weirdnesses the following example provides a macro \bigvrb—the option showspaces=true is applied so you can better see the treatment of spaces that are intended to be indentation for just making the source code more readable:

\RequirePackage{xparse}
\documentclass[a4paper]{article}

\usepackage{fancyvrb}
\usepackage{fvextra}

\NewDocumentCommand\bigvrb{}{%
\begingroup
\catcode\^^I=12 %
%\endlinechar=\^^M
\newlinechar=\endlinechar
\innerbigvrb
}%
\begingroup
\catcode\^^A=14\relax
\catcode\%=12\relax
\catcode\{=12\relax
\catcode\}=12\relax
\catcode$$=1\relax \catcode$$=2\relax
\catcode\/=0\relax
\catcode\\=12\relax
/catcode/^^M=12/relax^^A
/csname @firstofone/endcsname(^^A
/endgroup^^A
/NewDocumentCommand/innerbigvrb(+v)(^^A
/scantokens(^^A
\endgroup^^A
\begin{Verbatim}[showspaces=true, breaklines=true,numbers=left,breakafter=\},breakbefore=\\]^^M^^A
#1^^M^^A
\end{Verbatim}^^M%^^A
)^^A
)^^A
)%

\pagestyle{empty}
\parindent=0ex
\parskip=.25\baselineskip

\begin{document}
\enlargethispage{9cm}\vspace*{-4.5cm}%

\noindent\hrulefill

This looks okay:

\bigvrb{ABC
DEF}

\noindent\hrulefill

This has a visible comment-char and the spaces for indenting the code are visible also:

\bigvrb{%
ABC%
DEF%
}
\noindent\hrulefill

The spaces for indenting the code are visible. Besides this you have four
linebreaks \emph{inside} the verbatim-argument---the linebreak before the
first line (\verb*|  ABC|) and the linebreak after the last line
(\verb*|  DEF|) each yields an empty line:

\bigvrb{
ABC
DEF
}

\noindent\hrulefill

Even more spaces are used for indenting the code. They all are visible:

\begin{itemize}
\item Some text of an item of an \verb|itemize|-environment.
That item also contains a \verb|\bigvrb|-command whose
result is here:
\bigvrb{ABC
DEF}
\end{itemize}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way, keeping in mind that
curly braces must be balanced with \verb|\bigvrb|'s argument if not using delimiter-syntax:

\let\MyClosebrace=\}
\bigvrb{First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\MyClosebrace, breakbefore=\\]
Second environment}

\noindent\hrulefill

Now let's have fun creating two \verb|Verbatim|-environments in a weird way,
using delimiter-syntax---as verbatim-arg-delimiter \verb|>| is chosen:

\bigvrb>First environment
\end{Verbatim}

\begin{Verbatim}[showspaces=true, breaklines=true, numbers=left, breakafter=\}, breakbefore=\\]
Second environment>

\noindent\hrulefill

\textbf{Summa summarum:}

Undelimited-argument-syntax with multiline-verbatim-material is
confusing as
\begin{itemize}
\item you cannot have the usual code-indentation and linebreaking.
\item you cannot easily use the command \verb|\bigverb|
for verbatimized typesetting of the line \verb|\end{Verbatim}|.
\item you may need to take care of braces being balanced in
\verb|\bigvrb|'s argument if you don't use delimiter-syntax.
\end{itemize}

\end{document}


Of course \bigvrb of the example above is intended to obtain its argument by reading and tokenizing .tex-input after (temporarily) switching to verbatim-catcode-régime.

Therefore \bigvrb does not work in situations where it obtains its argument

• by having it passed as part of a macro-definition which got tokenized under non-verbatim-catcode-régime
• by having it passed from another macro during whose carrying-out the argument got tokenized under non-verbatim-catcode-régime
• by having it passed from a token-register via \the-expansion when the content of that register was tokenized under non-verbatim-catcode-régime
• etc.

Using it as component of moving arguments might yield unexpected effects.

As a rule of thumb you can use \bigvrb within the text of the document but you cannot use it within macro-definitions/within the argument of other macros. Thus it is of very limited use only.

This tokcycle based approach is set up to handle tokens (and groups) in the input stream in a successive manor. It can thus be programmed to emulate verbatim in most ways (the exception being that the grouping tokens {} must be balanced)

space after \macros. Like \a\b\c