When should one use \verb and when \texttt
The short answer is you use \verb
where you need to write a small piece of inline verbatim material that contains characters TeX treats (or rather, is currently treating) as special. \texttt
is for when you just want typewriter font.
\verb
has some downsides, such as not working in moving arguments. In those cases, you're probably better off using \texttt
(or related semantic markup) and performing the appropriate escaping.
Sometimes using \verb|...|
is better. For example if you copy paste a piece of code like __start:
in a \texttt{}
environment you might get an error as symbol "_
" is not inside a math environment. And then you have to rewrite the code like this: "\texttt{\_\_start}
". But why would you do this when you can just use:"\verb|__start|
".
One advantage of \verb
is that underscores are typeset better. For example in
\documentclass{article}
\usepackage{verbatim}
\begin{document}
\texttt{foo\_bar}
\verb|foo_bar|
\end{document}
the first foo_bar
has a thinner underscore. See Underscores in words (text) for more details.