6

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.

2 Answers 2

5

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.

4
  • This is exactly what I'm after - thanks a lot! I had a look at the source for the shortvrb package but it's a bit too complicated for me at my current level of TeX mastery. Thanks for the explanation too. Mar 28, 2012 at 4:37
  • you defined in '+'. When we use + sign in math mode "+" sign clashing. Check it. Mar 28, 2012 at 5:51
  • I know. I just chose '+' as an example for the delimiter - I can make it anything I like. Mar 28, 2012 at 6:01
  • @S.Murugan: That happens with any symbol you use for this purpose.
    – Caramdir
    Mar 28, 2012 at 15:26
0

Check this will work fine:

\documentclass[a4paper]{article}
\usepackage{fancyvrb}

\begin{document}
\verb+asdf+  typesets a verbatim 'asdf' $a+b+c$
\end{document}
1
  • That's not what I was after. I know about fancyvrb and \verb+asdf+, but what I want is to achieve the affects of \DefineShortVerb{\+} with +asdf+ (or any delimiter of my choosing really), but instead using \texttt intead of verbatim. Mar 28, 2012 at 6:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .