How to put \verb command inside of \textbf{} block?

It looks like you can't use \verb|...| inside of \textbf{...}. I tried the following:

\textbf{Be very careful when using \verb|-i| as it's destructive and it's not reversible!}


And got this error:

! LaTeX Error: \verb illegal in command argument.


Any ideas?

-
More generally, you can't use \verb inside any other command's argument (apart from some commands which are specially made to support this, by not really using an argument here). –  Paŭlo Ebermann Jul 31 '11 at 18:14

It depends on what you want the verbatim to support. If you simply want to put some normal text in "verbatim" (typewriter) font, then use \texttt. If you want to allow special characters such as _, &, or ^ to be displayed as is, then \listinline (which xport mentions) may be enough, and allows to customize the font somewhat.

However, # or % will still give you trouble if you don't somehow tell the surrounding command that they are there: for instance, as soon as % is read from the file the first time, TeX ignores the rest of the line, and there is no way to retrieve it. Here you will have two options that I know of: either save the offending piece of verbatim text prior to using it, for instance with newverb, or tell the surrounding command to first read its argument verbatim, via the cprotect package.

\documentclass{article}
\usepackage{cprotect}
\begin{document}
Try \cprotect\textbf{this \verb|%ther #pt\on|}.
\end{document}


Actually, in the case of \textbf and other font commands, you can also use the corresponding switch, \bfseries, as in {\bfseries this \verb|%&#| example}.

-

Use listings package, it is very useful and can be used everywhere, either in section heading or body.

\documentclass{article}
\usepackage{xcolor,listings}
\lstset
{
language={[LaTeX]TeX},
basicstyle=\tt\color{red},
}
\begin{document}
\section{This is an inline listing in \lstinline|section|}
Remember \lstinline|I can find| a tool to convert PDF to EPS
\lstinline!in my! neither bathroom nor kitchen.
\end{document}

-

use \texttt instead and a monotype font which supports a bold typeface. \verb cannot be inside another macro.

-