Here, I use a token cycle to emulate an inline-\verb
-like command with line breaking, called \bverb{...}
. I should point out where I think it has application: for short inline bursts of verbatim where, for example, a long verbatim variable name runs out the right margin. This approach allows either automatic line breaking at any token, or else targeted line breaking at designated discretionary breakpoints.
It is not truly verbatim in several ways:
Braces must be balanced, and all cat-1/cat-2 token pairs will be represented as braces {}
in the output.
Spaces are not treated as verbatim, but stretch and linebreak.
Here are some features:
By default, line breaking occurs at any character.
An non-empty optional character (or macro) can be specified to indicate non-hyphenating discretionary line-break points, if one desires to line-break at more logical places
While spaces are presented as spaces, an optional second argument may be used to respecify the rendering of spaces, for example, with \textvisiblespace
.
Cat-14 %
comment characters are treated in a verbatim fashion.
Best of all, that silly space that appears after detokenizing macro names goes away in \bverb
, except where necessary.
Here is an MWE that tries to demonstrate all these points.
\documentclass{article}
\usepackage{tokcycle}
\newcommand\bverb[1][]{\begingroup\ifx\relax#1\relax
\def\tmpA{\allowbreak}\let\tmpB=\empty
\setlength\spaceskip{1.5ex plus 1ex minus .5ex}% SETTING \xspaceskip CAN BE DONE
\else\def\tmpA{}\let\tmpB=#1
\setlength\spaceskip{1.5ex plus 1ex minus .5ex}\fi% SETTING \xspaceskip CAN BE DONE
\catcode`\%=12 \bverbaux}
\newcommand\bverbaux[2][ ]{%
\ttfamily\tokencycle
{\tctestifx{\tmpB##1}{\addcytoks{\allowbreak}}{%
\addcytoks[1]{\tmpA}\addcytoks[1]{\string##1}%
\addcytoks{\nobreak\hspace{0pt plus 5pt minus .5pt}}}}
{\addcytoks[1]{\string{}\processtoks{##1}\addcytoks[1]{\string}}}
{\tctestifx{\tmpB##1}{\addcytoks{\allowbreak}}
{\tctestifnum{\cstest{##1}=11}
{\tcpeek\zzz\tctestifcatnx\zzz A{\tcpush{\space}}{}}{}%
\tcpush{\string##1}}}
{\addcytoks{\allowbreak#1}}#2\endtokencycle\endgroup}
\def\cstest#1{\expandafter\cstestaux\string#1.\relax}
\def\cstestaux#1#2#3\relax{\the\catcode`#2 }
\textwidth=2.5in
\begin{document}
\bverb{%$#\-^_+\x vb\ x\?a\x 0 In the normal \bverb situation,
spaces are visible, and line-breaks may occur at any location.}
\bverb[\-]{A specific (non-printing) discre\-tionary break character may
be in\-serted by using the optional ar\-gument. In this case, only the
discretionary break (and spaces) al\-low breaks, allowing one to set
non-hyphenated breaks at the syl\-lable boundaries.}
\bverb[!]{A non-empty optional argument must be defined for the dis!cret!ionary
break to be defined.
The token required for \bverb line breaks can be defined as either a macro,
such as \- or alter!nately a character, as here, an ex!clama!tion point.}
\bverb[!]{Balanced {bracing} is a require!ment and spaces are not treated
in a \verbatim fashion.}
\bverb[\-][\textvisiblespace]{The second optional argument of \bverb is an alternate
treatment for the space character. Here, \textvisiblespace is selected.}
\bverb[!]{Notice how the nasty space that follows macro names when using
\de!tokenize disappears, whenever ap!pro!priate: \x0, \x a, \%x vs \% x}
\end{document}

\verb
arguments across lines. Good editors set up for LaTeX should do the same. In case yours doesn't do this check, get the habit of writing<space>%<newline>\verb
. The editor should at least respect lines that end with%
; if it doesn't, change it.