27

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?

2
  • 6
    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). Jul 31, 2011 at 18:14
  • There are some alternatives fancyvrb, verbdef, fvextra,
    – user202729
    Jun 28, 2022 at 10:47

4 Answers 4

25

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

16

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

enter image description here

\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}
1
  • Here does lstset defined for all \lstinlines?
    – alper
    Jan 17 at 12:02
15

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

1

I made a package to handle this case. Use it like this

%! TEX program = lualatex
\documentclass{article}
\usepackage{cprotectinside}
\begin{document}

\cprotectinside{!}{\textbf{using !\verb|-i|! as} ...}

\end{document}

Its main advantage is that it can handle the protected content arbitrarily deeply nested. e.g. in those cases it can be a little difficult to figure out what to cprotect.

It's on CTAN now with documentation, but if there's any bug source code can be found in https://github.com/user202729/TeXlib

Basically the syntax is \cprotectinside{ <delimiting character> }{ <text> }, where the region between 2 consecutive delimiting characters occurring in the text will be c-protected.

2

You must log in to answer this question.

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