# New verbatim environments defined with newverbs package generate errors when used in math mode

The newverbs package allows the user to define variants of the verb command which can be used for things like applying colors to verbatim text. Just like the old verb, the user-defined variants work fine in math mode. However, using the user-defined variants in math mode gives the following error on compile:

LaTeX Error: Command \ttfamily invalid in math mode.


What can I do if I want to define variants of verb that compile without errors?

MWE:

\documentclass{article}
\usepackage{newverbs}

\newverbcommand{\myverb}{}{}

\begin{document}
Old \verb|verb|, works in math: $+\verb|foo|+$.

New \myverb|verb|, gives error in math: $+\myverb|foo|+$.
\end{document}


When we ignore the compilie error this gives the desired output, namely:

• Welcome to tex.sx. Sid you try this as $+\text{\myverb|foo|}+$ ? Aug 31 '20 at 17:15

The difference is that \verb does \ifmmode\hbox; more precisely

% latex.ltx, line 5775:
\def\verb{\relax\ifmmode\hbox\else\leavevmode\null\fi
\bgroup
\verb@eol@error \let\do\@makeother \dospecials
\verbatim@font\@noligs
\language\l@nohyphenation
\@ifstar\@sverb\@verb}


The commands defined with \newverbcommand don't do this check. You can fix it by patching the relevant command and adding the enveloping \hbox in math mode.

\documentclass{article}
\usepackage{newverbs}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\new@@verbcommand}
{\bgroup}
{\ifmmode\hbox\fi\bgroup}
{}{}
\makeatother

\newverbcommand{\myverb}{}{}

\begin{document}

Old \verb|verb|, works in math: $+\verb|foo|+$.

New \myverb|verb|, gives error in math: $+\myverb|foo\\|+$.

\end{document}

• Perfect, thanks! And it still works when I go back to my original definition of myverb which stored the verbatim text in a box and then put that inside a colorbox, so my problem is solved.
– Cian
Aug 31 '20 at 21:56
• @Cian I filed a bug report to the package author. Aug 31 '20 at 22:05
• @Cian: I uploaded a fix in newverbs v1.5 2020/09/02 which got updated yesterday to CTAN. It should be available for update in the LaTeX distributions in the next days. Sep 3 '20 at 14:29

You can put the verbatim text into a box:

\documentclass{article}
\usepackage{newverbs}

\newverbcommand{\myverb}{\hbox\bgroup}{\egroup}

\begin{document}
Old \verb|verb|, works in math: $+\verb|foo|+$.

New \myverb|verb|, gives error in math: $+\myverb|foo|+$.
\end{document}


The result is the same as yours, but without the error.

Correction: As egreg pointed out, this approach doesn't work for newer newverbs versions. For 1.3a (2012/05/08) it works, for 1.4 (2019/09/09) it doesn't.

• Did you try it? The error is still there, because the \hbox\bgroup code is used after the \verbatim@font declaration that does \ttfamily. Aug 31 '20 at 17:38
• Yes, I did try it. It works fine (TeXLive 2019 on Debian). I'll now try to find the package version. Aug 31 '20 at 17:40
• newverbs.sty 2012/05/08 v1.3a Aug 31 '20 at 17:42
• A bit of trace: \myverb ->\begingroup \newverbcommand@settings \def \verb@egroup {\verb@orig@eg roup \egroup \endgroup }\def \newverbs@txend {\egroup \endgroup }\hbox \bgroup \verb Aug 31 '20 at 17:43
• And here the \verb expansion which switches the font: \verb ->\relax \ifmmode \hbox \else \leavevmode \null \fi \bgroup \verb@eol@err or \let \do \@makeother \dospecials \verbatim@font \@noligs \language \l@nohyph enation \@ifstar \@sverb \@verb` Aug 31 '20 at 17:45