208

Sometimes people here use \verb!A SHORT COMMAND! and sometimes \textt{A SHORT COMMAND}. I'm unsure when to use which.

Are there any guidelines or arguments for \verbatim vs. \texttt?

7 Answers 7

174

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.

6
  • 15
    I'd recommend using \texttt (or, better, a macro with semantic name that uses \texttt) unless you know it won't work for your particular string, such as with \verb|x=1%2|. And even then, using \texttt{1\%2} is probably just as good. Sep 6, 2010 at 9:04
  • 6
    Another huge downside of \verb I think is it sometimes doesn't work with other environments. I think for instance you can't use \verb in a \section{} environment. Mar 12, 2012 at 17:40
  • Would using \protect allow \verb to work in moving arguments? I'm only asking, because I don't know how to test for myself. Oct 21, 2015 at 21:02
  • @TylerCrompton: No.
    – GuM
    Oct 9, 2016 at 22:10
  • A disadvantage of \texttt{} is that you don't get vertical single quotes, even with \usepackage{upquote}. I want those if I'm using tt to represent source code/computer input (which is about the only thing I use tt for).
    – dedded
    Sep 21, 2018 at 15:33
41

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|".

1
  • 17
    Notice that \verb{__start} will give tons of errors. I've fixed the delimiters: they should be identical characters that are not among those to typeset verbatim.
    – egreg
    Mar 12, 2012 at 17:21
19

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.

10

\texttt allows line breaks, whereas \verb does not. They serve slightly different purposes.

1
  • 1
    …also in the source code.
    – GuM
    Oct 9, 2016 at 22:09
2

Just found that wanting to highlight text in a document makes a difference....

highlighting with

\usepackage{color,soul}

and

\hl{text to highlight}

This works fine if the text to highlight includes \texttt but fails with \verb

so for example

\hl{\texttt{printf} format specifiers} 

works fine, but

\hl{\verb$printf$ format specifiers} 

fails...

2
  • 3
    The problem isn't with \hl, but with \verb. \verb (and any other verbatim-like commands and environments) don't work in arguments to other commands. It's the same principle as here: tex.stackexchange.com/a/447081/134574 Sep 21, 2018 at 11:19
  • @PhelypeOleinik, thanks for the helpful comment and for checking out this very very very late answer
    – tom
    Sep 21, 2018 at 16:02
1

One more thing: \texttt{ Hello } will print Hello, while \verb| Hello | will print Hello . In other words, \texttt ignores leading and trailing spaces.

0

Just to add on to what @TH mentioned, \texttt also works well inside equation/eqnarray environments and TikZ code. \verb (in its primitive form) doesn't work very well in such cases.

1
  • \verb (in its primitive form) works without problem in equation (or eqnarray)...
    – Werner
    Jul 22, 2015 at 17:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .