3

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} 
2
  • 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) Commented Feb 15, 2021 at 12:31
  • Thanks very much Commented Aug 17, 2021 at 15:28

1 Answer 1

3

Changing catcode values changes the tokenization of characters yet to be read from the 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? Commented Feb 15, 2021 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. Commented Feb 15, 2021 at 13:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.