# Use shortvrb without needing to end the command manually

Currently, using shortvrb, it's possible to define a single character to start, by itself, an inline verbatim command:

\documentclass{article}
\usepackage{shortvrb}

\begin{document}
\MakeShortVerb{\|}
foo |this is verbatim|
\end{document}


or similarly, with fancyvrb,

\documentclass{article}
\usepackage{fancyvrb}

\begin{document}
\DefineShortVerb{\|}
foo |this is verbatim|
\end{document}


In both cases -- as with \verb!some verbatim text! -- the symbol is required again to end the verbatim portion. If it's left out, an error similar to "\verb ended by end of line" is produced.

Is there a way to (re)define \MakeShortVerb or similar, such that the newline will merely end the command, rather than producing an error? Ideally, one could then write something like:

\DefineAsymmetricVerb{\|}
This is some regular text.
Now some verbatim code that only needs a symbol at the start of the line, like:
| if 1 == 1
|     print 'All is well'


This would allow blocks (or single lines) of verbatim text without the clutter of

| if 1 == 1                |
|     print 'All is well'  |


or

\begin{verbatim}
if 1 == 1
print 'All is well'
\end{verbatim}

• Isn't this \begin{verbatim}...\end{verbatim}? – egreg Feb 15 '18 at 15:32
• @egreg Yes, that would work, but in the context in which I'm wanting my verbatim text that would add undesirable clutter. I've added a clarification about that. I'll happily grant that this is a problem that I could find other ways around, but I've gotten myself curious as to whether it's possible to just end \verb with a new line and what the mechanics are behind that being possible (or not). – owjburnham Feb 15 '18 at 15:40
• how should short verb know, where to end verbatim text? for example in regular languages we use dots, comas to show where a sentence is finished ... – Zarko Feb 15 '18 at 15:40
• Verbatim mode has the tendency to go on forever. – egreg Feb 15 '18 at 15:41
• @egreg It would definitely go on forever if -- as I had accidentally done above -- I wrote \begin instead of \end – owjburnham Feb 15 '18 at 15:42

## 1 Answer

You can, but you shouldn't.

\documentclass{article}

\makeatletter
\catcode|=\active
\def|{%
\par
\begingroup
\parindent\z@\mbox{}%
\owj@eol\catcode\^^M=\active
\let\do\@makeother\dospecials
\verbatim@font\@noligs
\@vobeyspaces\frenchspacing
}
\def\owj@par{\par\endgroup}
\begingroup\lccode~=\^^M\lowercase{\endgroup
\def\owj@eol{\let~\owj@par}
}
\makeatother

\begin{document}

This is some regular text.
Now some verbatim code that only needs a symbol at the start of the line, like:
| if 1 == 1
|     print 'All is well'
|
| else
|     error
| fi
\noindent This is some regular text

\end{document}


• The master has spoken. – Steven B. Segletes Feb 15 '18 at 16:03
• Fabulous, thanks so much; I'm going to have great fun getting my head around that! And as per another comment that I just posted ( tex.stackexchange.com/questions/415474/… ), I'm totally with you on "You can, but you shouldn't." – owjburnham Feb 15 '18 at 16:06
• (+1), however the " but you shouldn't" you should put in boldface! :-) – Zarko Feb 15 '18 at 16:07
• Interesting. I assume "you shouldn't" because somehow this is dangerous for ... some reasons. Maybe for the compiler, or linters, or error messages, or something. Would it be possible to define an environment in which you permit this syntax, that way dangerous issues are contained? – Addem Jun 6 at 20:04
• @Addem The worst part is catching the end-of-line. This is always very fragile. Besides, adding a wrapper environment is useless, as there's already verbatim. – egreg Jun 6 at 20:19