Here is more simple solution:
\newcount\tmpnum
\def\catcodeletters{\tmpnum=64
\loop \advance\tmpnum by1
\ifnum\tmpnum<128
\ifnum\catcode\tmpnum=11 \catcode\tmpnum=12 \fi
\repeat
}
\def\verb{\bgroup\catcode`\%=12\catcode`\^=13\catcode`\ =12\catcode`\#=12
\catcodeletters\verbA}
\def\verbA#1{\def\tmp{#1}\tt\expandafter\mm\meaning\tmp\endmm\tmp\egroup}
\def\mm#1->#2\endmm{\def\tmp{#2}}
\verb{a{b\}c\{d}e a #sp_&ace}
\end
I've added the \catcodeletters to my macro in order to spaces after \foo don't arise. Thanks to Heiko.
Edit: My 12-lines macro was not chosen as an accepted answer but 50-line macro was. May be that the spacemarks replaced by real spaces was the core of the interest, but it was not specified in the question. This is a part of printing, no scanning verbatim. So it can be implemented after \tmp is scanned. You can change \verbA definition and you can add the \printverb macro, which does the space replacement by another way than in the previous answer.
\def\verbA#1{\def\tmp{#1}\expandafter\mm\meaning\tmp\endmm
\tt\expandafter\printverb\tmp\end\egroup}
\def\printverb{\futurelet\next\printverbA}
\def\printverbA{%
\ifx\next\end \def\next{\let\next}%
\else \ifx\next\spacetoken \char32
\def\next{\afterassignment\printverb\let\next= }%
\else \next \def\next{\afterassignment\printverb\let\next}%
\fi \fi
\next
}
\edef\tmp{\let\noexpand\spacetoken= \space}\tmp
Now the \tt\char32 spacemarks are printed instead spaces.
\tt\makeatletter\meaning\@sverb(just typeset that). It's not that practically helpful, but it shows the complexity. You have to change catcodes, so taking in the argument (as you would naturally do) doesn't work since everything is already read by that time. You have to take in a delimiting argument, then change catcodes accordingly. Then you can do what you need. – Sean Allred Jun 03 '14 at 05:39xparsepackage for grabbing optional arguments, and tracking nested brackets is tricky). As already suggested, a much easier approach is to use some think\verb"a{b\}c\{d}e" where the very first character after\verb` is used as both the start and end marker. Would a solution working in this way be acceptable? – Joseph Wright Jun 03 '14 at 06:14\verbjust like it is in LaTeX. I'm just curious about more. – Z.H. Jun 03 '14 at 06:30\detokenize, but deleted it after Joseph pointed out your focus was plain TeX. If you want the answer resurrected, just let me know. – Steven B. Segletes Jun 03 '14 at 10:25code, Let's assume braces are always balanced and don't consider unbalanced case. – Z.H. Jun 03 '14 at 11:02\begin ... \endor a "toggle" approach than a command-with-argument definition. you might find the definitions of\verbatimand|...|in the filetugboat.sty(the plain tugboat implementation) interesting. these extended the texbook definitions to pay attention to spaces at the beginning of a line, and an "escape" to allow metacode within a verbatim block, among other features. this file (and related material) is in tex live in the.../plain/tugboat-plain/area. – barbara beeton Jun 03 '14 at 15:09