When I want to refer to a math variable, say some index i
, I simply surround it with dollars (i.e., $i$
) as opposed to writing \begin{math}i\end{math}
.
Is there some similar option for texttt
mode, e.g. §i§
, to replace \texttt{i}
?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityWhen I want to refer to a math variable, say some index i
, I simply surround it with dollars (i.e., $i$
) as opposed to writing \begin{math}i\end{math}
.
Is there some similar option for texttt
mode, e.g. §i§
, to replace \texttt{i}
?
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}
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}
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}
\texttt[]
is made easy? – Denis Aug 19 '20 at 11:26\texttt
in a part of the document I usually just copy\texttt{
to the clipboard and use CTRL-V to enter it quickly (and manually add}
afterwards). – Marijn Aug 20 '20 at 8:41