7

I'm trying to put verbatim text inside a node (in a beamer frame):

\begin{frame}[fragile]{P}
\tikzstyle{cell} = [rectangle, draw, thin , fill=black!10 , minimum size=5mm]
\begin{tikzpicture}[node distance=3cm, auto,>=latex', thick, overlay]
    \path[->,draw]<1-> node[cell] (p) {Text at \verb!p!};                
\end{tikzpicture}
\end{frame}

But it doesn't compile and produces an error. If one replaces \verb!p! with anything else, like "p", it compiles. Note that I also tried to put "fragile" in the frame options. Thank you in advance!

1 Answers1

10

You can use \Verb from the fancyvrb package:

\documentclass{beamer}
\usepackage{tikz}
\usepackage{fancyvrb}
\usetikzlibrary{arrows}

\begin{document}

\begin{frame}[fragile]{P}
\tikzstyle{cell} = [rectangle, draw, thin , fill=black!10 , minimum size=5mm]
\begin{tikzpicture}[node distance=3cm, auto,>=latex', thick, overlay]
    \path[->,draw] node[cell] (p) {Text at \Verb!p!};                
\end{tikzpicture}
\end{frame}

\end{document}

As Caramdir has correctly suspected in a comment, if another document class (article, for example) is used there's no need to use \Verb and the standard \verb command will work.

enter image description here

You should also consider using \texttt{p} instead of \verb!p! (as Torbjørn T suggested in his comment) if your intention is just to use a mono-spaced font for the "p" character.

Gonzalo Medina
  • 505,128
  • You forgot the <1-> from the OP. (although it's probably no-op here, it's responsible for the broken behavior of verb. Without <1->, it works both with and without Verb. // Side note, read the documentation of fancyvrb for remark on how to use Verb, it's different (e.g. you need to escape spaces etc.) Could just use \texttt{ for simplicity. – user202729 Jul 27 '22 at 06:46