# Inline environment shortcut (like $blah$)

I want to be able to format code with a monospaced font. Right now I'm using \texttt{} but it's kind of a pain to keep typing that everywhere. Is there any way to use a shortcut instead? For example, for math, you can wrap it in $. So, for my situation, I'd like to be able to write something like: const WIDTH = 5 and have it turn into: const WIDTH = 5 I've tried looking for shortcuts (which gave me a bunch of editor stuff), new commands (which told me how to define a new backslash command) and macros (which I didn't really understand but aren't what I want). • Almost all editors have this feature. You can create a shortcut for \texttt{} (like ctrl+i for \textit{}). What editor do you use? – Sigur Feb 16 at 18:06 • Is an editor the only way to do this? I was really hoping for something lower level, so to speak. – Jharod Feb 16 at 18:08 • Lets wait experts. I think you have to change the catcode of the . – Sigur Feb 16 at 18:10 • Do you want just \texttt or do you need a real verbatim environment? (e.g. will the content contain characters that are normally escaped in TeX? If so, perhaps you want to use fancyvrb \DefineShortVerb{\|} which will allow you to write |foo| to get what you want. – Alan Munn Feb 16 at 18:16 ## 3 Answers You can turn | into inline verbatim like doc.sty does: \documentclass[]{article} \makeatletter %Shamelessly copied from doc.sty \def\MakeShortVerb{%>>> \@ifstar {\def\@shortvrbdef{\verb*}\@MakeShortVerb}% {\def\@shortvrbdef{\verb}\@MakeShortVerb}}%<<< \def\@MakeShortVerb#1{%>>> \expandafter\ifx\csname cc\string#1\endcsname\relax \@shortvrbinfo{Made }{#1}\@shortvrbdef \add@special{#1}% \expandafter \xdef\csname cc\string#1\endcsname{\the\catcode#1}% \begingroup \catcode\~\active \lccode\~#1% \lowercase{% \global\expandafter\let \csname ac\string#1\endcsname~% \expandafter\gdef\expandafter~\expandafter{\@shortvrbdef~}}% \endgroup \global\catcode#1\active \else \@shortvrbinfo\@empty{#1 already}{\@empty\verb(*)}% \fi}%<<< \def\DeleteShortVerb#1{%>>> \expandafter\ifx\csname cc\string#1\endcsname\relax \@shortvrbinfo\@empty{#1 not}{\@empty\verb(*)}% \else \@shortvrbinfo{Deleted }{#1 as}{\@empty\verb(*)}% \rem@special{#1}% \global\catcode#1\csname cc\string#1\endcsname \global \expandafter\let \csname cc\string#1\endcsname \relax \ifnum\catcode#1=\active \begingroup \catcode\~\active \lccode\~#1% \lowercase{% \global\expandafter\let\expandafter~% \csname ac\string#1\endcsname}% \endgroup \fi \fi}%<<< \def\@shortvrbinfo#1#2#3{%>>> \PackageInfo{doc}{% #1\expandafter\@gobble\string#2 a short reference for \expandafter\string#3}}%<<< \def\add@special#1{%>>> \rem@special{#1}% \expandafter\gdef\expandafter\dospecials\expandafter {\dospecials \do #1}% \expandafter\gdef\expandafter\@sanitize\expandafter {\@sanitize \@makeother #1}}%<<< \def\rem@special#1{%>>> \def\do##1{% \ifnum#1=##1 \else \noexpand\do\noexpand##1\fi}% \xdef\dospecials{\dospecials}% \begingroup \def\@makeother##1{% \ifnum#1=##1 \else \noexpand\@makeother\noexpand##1\fi}% \xdef\@sanitize{\@sanitize}% \endgroup}%<<< \AtBeginDocument{\MakeShortVerb \|} \AtEndDocument{\DeleteShortVerb \|} \makeatother \begin{document} |Test| but also |Test$_{}|
\end{document}

• Rather than copying all this code, it's probably easier just to load the fancyvrb package which provides a \DefineShortVerb macro to do the same thing. – Alan Munn Feb 16 at 18:18
• @AlanMunn yes, that would have been better... Do you want to answer or should I edit mine? – Skillmon Feb 16 at 18:19
• The idea is the same; I would just edit your answer to do it that way. – Alan Munn Feb 16 at 18:20
• I tried fancyvrb but it didn't work. The copy/paste blob works perfectly and I don't mind it since I have a separate preamble.tex file to hide it in. I do have one further question -- the blob is really hard to understand and I'm wondering how to add a highlight to the contents? – Jharod Feb 16 at 18:21
• @Jharod The fancyvrb solution should definitely work, and is a much better solution that a ton of copied code you don't understand. If you edit your question to show a non-working fancyvrb example, we could help you with that. – Alan Munn Feb 16 at 18:22

It looks like you want an inline verbatim environment. The fancyvrb package provides a simple way to do that. Note that, just like the Skillmon's solution, you cannot define the backtick to be the verbatim delimiter with this approach. Alternatively, if you just don't like having to type \texttt all the time you could make a shorter replacement macro for it. Here I've used \vrb. Notice how the fancyvrb version allows the use of reserved TeX characters without being escaped; the \texttt solution does not.

\documentclass{article}
\usepackage{fancyvrb}
\DefineShortVerb{\|}
\newcommand{\vrb}{\texttt}
\begin{document}

Using the |fancyvrb| |foo|

Using a macro |\vrb{foo}| \vrb{foo}

\end{document}


Not really complicated and, differently from other proposed answers, the object can go in the argument to another command.

Thus something like \mbox{const WIDTH = 5} will work.

\documentclass{article}

\makeatletter
% define the active backquote
\begingroup\lccode~=\lowercase{\endgroup
\def~#1~{\texttt{#1}}%
}
% delay activating at begin document
\AtBeginDocument{\catcode\=\active}
\makeatother

\begin{document}

This is \texttt{const WIDTH = 5}

This is const WIDTH = 5

\end{document}


Beware it is quite fragile and finding a missing backquote could be difficult. It's simpler if you define some shortcut with your editor.

Even more fragile with double back quotes for denoting verbatim mode (however, the double back quotes cannot be used in the argument to another command, just like \verb).

\documentclass{article}

\makeatletter
% define the active backquote
\begingroup\lccode~=\lowercase{\endgroup
\def\jharodbackquote{\@ifnextchar~\jharodverb\jharodtt}
\def\jharodtt#1~{\texttt{#1}}
\let~\jharodbackquote
\def\jharodverb{\bgroup\catcode\=2 \verb}
}
% delay activating at begin document
\AtBeginDocument{\catcode\=\active}
\makeatother

\begin{document}

This is \mbox{const WIDTH = 5}

This is const WIDTH = 5

This is \verb{atim}

This is const WIDTH = 5

\end{document}
`