# How do I make a character active only in text mode?

I want to make the character > active only in text mode (to create markdown-style block quotes) but not in math mode. I know the \mathcode>="8000 way to make a character active only in math mode, but what about the reverse? I tried \ifmmode but that doesn't seem to work.

Here's what I have now, having to escape it as \> in math mode.

\usepackage[many]{tcolorbox}

\makeatletter

\newtcolorbox{QuoteBlock}{
blanker,
before skip=\topsep,
after skip=\topsep,
borderline west={2pt}{0pt}{black!50!white},
breakable,
left=\parindent,
}

\begingroup
\catcode\>=\active
\catcode\^^M=\active
\gdef>#1^^M{%
\begin{QuoteBlock}%
\noindent\@ifnextchar\par\@gobble\relax#1
\end{QuoteBlock}
}
\endgroup
\def\>{\string>}

\AtBeginDocument{\catcode\>=\active \catcode\^^M=\active}

\makeatother

\begin{document}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
$x\>0$
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
\end{document}

• make it active always, and have an ifmmode test so it does the normal thing in math mode (if change the catcode inside the definition, that has no effect on the current token at all) Feb 15, 2021 at 12:31
• Thanks very much Aug 17, 2021 at 15:28

Changing catcode values changes the tokenization of characters yet to be read from teh file but has no effect on any tokens already created.

You want it to be active always but do what it did before in math mode, so:

\documentclass{article}

\usepackage[many]{tcolorbox}

\makeatletter

\newtcolorbox{QuoteBlock}{
blanker,
before skip=\topsep,
after skip=\topsep,
borderline west={2pt}{0pt}{black!50!white},
breakable,
left=\parindent,
}

\begingroup
\catcode\>=\active
\catcode\^^M=\active
\gdef>{\relax\ifmmode\string>\else\expandafter\quotegt\fi}%
\gdef\quotegt#1^^M{%
\begin{QuoteBlock}%
\noindent\@ifnextchar\par\@gobble\relax#1
\end{QuoteBlock}
}
\endgroup

\AtBeginDocument{\catcode\>=\active \catcode\^^M=\active}

\makeatother

\begin{document}
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
> Lorem ipsum dolor sit amet, consectetuer adipiscing elit
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
$x>0$
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
\end{document}

• Thanks! May I ask what the initial \relax in the definition of > is for? Feb 15, 2021 at 13:05
• @Planted try it without as the first entry in an align or array cell :-) (I'm sure we have questions specifically about that if you search fro ifmmode) basically tex looks ahead for \multicolumn` before deciding math is needed in this cell so ifmmode would take the wrong branch. Feb 15, 2021 at 13:08