Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am trying the following:

\begin{frame}[fragile]{OCP en Exceptions}
``If you throw a checked exception from a method in your code and the
\verb!catch! is three levels above, \textit{you must declare that
  exception in the signature of each method between you and
  the \verb!catch!}''
\end{frame}

Somehow I can't use the verbatim text inside the italic block. How do I solve this the right way?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

You can't use verbatim material inside another macros argument. The already mentioned cprotect package works around that, but I would call it overkill using it here. Simply use a font declaration inside a group instead of a font macro, i.e. {\itshape text \verb+$%^+ text} instead of \textit{text \verb+$%^+ text}.

Applied to your code example:

\begin{frame}[fragile]{OCP en Exceptions}
``If you throw a checked exception from a method in your code and the
\verb!catch! is three levels above, {\itshape you must declare that
  exception in the signature of each method between you and
  the \verb!catch!}''
\end{frame}

Also using verbatim only to get the font effect is a misuse. Simply use tt font directly! For often used real verbatim material I would recommend to define them as macros using \verbdef of the verbdef package. (There is also \Verbdef of my newverbs package if you need it in an expandable form, but normally this is not the case.)

share|improve this answer

Use cprotect package. More details, see the manual.

\documentclass{article}
\usepackage{cprotect}
\begin{document}
\cprotect\textit{\verb|\LaTeX| is one of my primary weapons}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.