17

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}?

  • 2
    Welcome to TeX.SE. – Mico Aug 19 at 11:16
  • 4
    Many editors have shortcuts for these commands, so it might be an idea to explore those. – daleif Aug 19 at 11:26
  • @gail Instead of looking for a LaTeX solution, which may be somewhat risky as emphasized by David below, why not program your editor so that the typing of \texttt[] is made easy? – Denis Aug 19 at 11:26
  • When I need to type a lot of \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 at 8:41
  • 2
    @Denis because a main point of using something like LaTeX instead of WYSIWIG software is that it allows us to avoid such hacks? If you need something often, the best way is not to let the editor duplicate it lots of times (cluttering your source and making it difficult to change styling later on), but instead define a macro once and for all and let the compiler expand it. – leftaroundabout Aug 20 at 14:09
27

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

| improve this answer | |
22

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}
| improve this answer | |
15
\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

| improve this answer | |
  • 3
    are you sure that's a good idea (you just redefined an entire range of characters sharing the same utf-8 prefix) – David Carlisle Aug 19 at 11:23
  • @DavidCarlisle I wasn't sure it was a good idea, and now I'm even more sure it is not. However, it is what the OP asked for. – Steven B. Segletes Aug 19 at 11:24
  • No the OP didn't ask that a whole block of characters were made unusable, try adding © to your text:-) – David Carlisle Aug 19 at 11:29
  • @DavidCarlisle Point taken – Steven B. Segletes Aug 19 at 11:30
  • 1
    @DonHosek Yes, thank you, that is correct. The thought crossed my mind earlier, but I didn't pursue it. – Steven B. Segletes Aug 19 at 18:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.