2

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}
1
  • 1
    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) – David Carlisle Feb 15 at 12:31
2

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}
2
  • Thanks! May I ask what the initial \relax in the definition of > is for? – Planted Feb 15 at 13:05
  • 1
    @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. – David Carlisle Feb 15 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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