[Tex/LaTex] Define the own short-verb like macro

macrostypewriterverbatim

I'm trying to define shorthand macros for \texttt that are a bit like the \verb ones. The end result is to do +asdf+ and this have the same effect as \texttt{asdf}. This is like the DefineShortVerb command in the fancyvrb package, but instead of verbatim I want texttt as the underlying command.

First, I can obtain a macro like \ttt+asdf+ via the xparse package:

\documentclass[a4paper]{article}
\usepackage{xparse}
% want \ttt[delimiter]#1[delimiter] for \texttt{#1}:
\NewDocumentCommand\ttt{v}{%
    \texttt{#1}%
}
\begin{document}
\ttt+asdf+
\end{document}

This works.

Is there some way to take the next step and use [delimiter]asdf[delimiter] to mean \texttt{asdf}?

For example, if I were using fancyvrb I'd use DefineShortVerb:

\documentclass[a4paper]{article}
\usepackage{fancyvrb}
\DefineShortVerb{\+}
\begin{document}
+asdf+ % typesets a verbatim 'asdf'
\end{document}

My question is: Is there any way I can emulate this behaviour but using \texttt as the base command as opposed to \verb?.

It doesn't have to be as sophisticated as providing a DefineShortVerb macro; I'd just like the [delmiter]text[delimiter] functionality.

(Another way to go about it – is there some way I can override a fancyvrb command to use \texttt instead of \verb as the base command?)

cheers.

Best Answer

You can make + (or whatever symbol you choose) into a command character and then use \def to have it do whatever you want:

documentclass{article}

\catcode`+=\active
\def+#1+{\texttt{#1}}

\begin{document}
+abc def+
\end{document}

Here \catcode`+=\active makes + an “active” character and then \def+#1+{\texttt{#1}} defines + as a macro that takes one parameter that is delineated by another + and passed to \texttt.