1

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}
  • 1
    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
  • 1
    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
3

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}

enter image description here

  • 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
    (+1), however the " but you shouldn't" you should put in boldface! :-) – Zarko Feb 15 '18 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.