Is there an easy way to enter texttt mode, like $ for math mode?

You can use § as a delimiter without affecting other characters using the same first byte.

\documentclass{article}
\usepackage{newunicodechar}

\newunicodechar{§}{\makeabbreviationtt}
\def\makeabbreviationtt#1§{\texttt{#1}}

\begin{document}

This is §monospaced§. This doesn't affect ©, ¶
and other similar UTF-8 characters.

You can also do §¶§

\end{document}

enter image description here


Not for \texttt but for \verb the base distribution includes shortvrb package that allows you to define a shorthand

\documentclass{article}
\usepackage{shortvrb}
\MakeShortVerb|


\begin{document}
  aaaa |z|  aaa
\end{document}

\documentclass{article}
\def§#1§{\texttt{#1}}
\begin{document}
roman §tt text§ back to roman
\end{document}

enter image description here

However, as David points out in the comments, because § is not a single byte character, but a UTF-8 extended (multi-byte) character, this approach will wipe out any of the UTF-8 characters that begin with the same prefix, resulting in an error, if those characters are used (such as ©).

So, if you wanted this type of solution it would be better to choose the delimiter as a single-byte ASCII character and making it active:

\documentclass{article}
\catcode`|=\active
\def|#1|{\texttt{#1}}
\begin{document}
roman |tt text| back to roman
\end{document}

The downside here is you lose the use of | as a normal input character. Therefore, you could get fancy and build an escape into the definition, so that || together would echo a single | to be typeset:

\documentclass{article}
\usepackage[T1]{fontenc}
\let\svvert|
\catcode`|=\active
\def|#1|{\ifx\relax#1\relax\expandafter\svvert\else\texttt{#1}\fi}
\begin{document}
roman |tt text| back to roman || or $y =||x||$ and |then back to texttt|.
\end{document}

enter image description here

Tags:

Typewriter