22

How do I make an item in a description list verbatim? \verb in \item[] seems to be not allowed:

\begin{description}
    \item[\verb+list comprehension [f a | a <- M a]+] bla bla bla
\end{description}

3 Answers 3

18

The package examplep provides several commands and environments for typesetting verbatim text. For instance, the robust commands \PVerb[options]{text}and \Q{text} can be used in macro arguments and in section titles as well.

Code example for your description list:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[Q=yes]{examplep}
\begin{document}
\begin{description}
    \item[\Q{list comprehension [f a | a <- M a]}] bla bla bla
    \item[\PVerb{testing: \verb||{} #_$ }] item text
\end{description}
\end{document}

Output:

verbatim example

See Verbatim phrases and listings in LaTeX by Péter Szabó (or texdoc examplep) for further information. Escaping special characters (for instance with \Q) may be necessary as described in section 3.1 of the documentation.

Note, T1 font encoding has to be use to get a proper < symbol because it's unavailable in standard OT1 encoding.

3
  • Great answer, \Q works well for me, although I am not sure what the difference between that and \PVerb ? Thanks Oct 8, 2018 at 2:46
  • Hi, the code in this answer works in TexLive 2019, if I remember correctly it works in TexLive 2020, but I see it just failed in TexLive 2021, with some error like ! Package examplep Error: \PVerb inner delimiter must be brace, (examplep) not backslash or control sequence or active char.
    – ollydbg23
    Dec 25, 2021 at 7:57
  • I post a new question here about this issue: examplep package used in description list item error under TexLive 2021
    – ollydbg23
    Dec 25, 2021 at 8:06
8

The \verb command is not allowed inside arguments of other commands. One workaround is to use \SaveVerb from fancyvrb.

The following definition of \vitem is taken from the Companion. It has an optional argument taking styling parameters from the fancyvrb package.

\usepackage{fancyvrb}

\newcommand\vitem[1][]{\SaveVerb[%
    aftersave={\item[\textnormal{\UseVerb[#1]{vsave}}]}]{vsave}}

\begin{description}
    \vitem+list comprehension [f a | a <- M a]+ bla bla bla
\end{description}
0

For completion, a solution using \cprotect, and a solution that avoids \verb entirely.

Also (most of the time) answers the question: How can I put verbatim inside an optional argument?

\usepackage{cprotect}

...

\begin{description}
    \cprotect[o]\item[\verb+list comprehension [f a | a <- M a]+] bla bla bla
    \item[\textnormal{\texttt{list comprehension [f a | a <- M a]}}] bla bla bla
\end{description}

Without the \textnormal, the text produced by \texttt will be bold, which may or may not be what you want.

2
  • cprotect package is newer than when the question was posted.
    – user202729
    Dec 25, 2021 at 9:12
  • And I can't find a duplicate.
    – user202729
    Dec 25, 2021 at 9:13

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.