# Escaping “math” characters in boxed verb

When I want to print out some common "special meaning" characters, I use the \verb command, i.e.

\verb?x^2 + 1?

(No, I don't wan't to use the math mode, I want to print out that ^ character.)

Works fine, except then when I want to "box" the text. This: \fbox{\verb?x + 1?} works fine, but this: \fbox{\verb?x^2 + 1?} fails: seems that the fbox removes the verbatim-ness, and processes the ^ as math-mode (even though I didn't put any \$...)

Question: is there a "simple" way to (re-)enable the verbatim mode inside a \fbox ?

Note: I also checked this similar question, however I am unable to make the accepted answer work.

you could use

\texttt{x\char\^2 + 1}

• Seems to be the simplest (no additional package). And, for the record, \fbox{\texttt{x\char\^2 + 1}} works fine. – kebs Nov 18 '14 at 16:45

The verbatimbox package can help. It also works on true verbatim, unlike a \texttt alternative.

\documentclass{article}
\usepackage{verbatimbox}
\begin{document}
\verb?x^2 + 1 + \alpha? \par
\begin{verbbox}x^2 + 1 + \alpha\end{verbbox}
\fbox{\theverbbox}
\end{document}