# Make §...§ marker for textmode INSIDE math mode

I want to definde §...§ as markers for textmode inside math mode. This would be useful for example inside "cases" environments, to add text defining the cases; or for defining sets.

\{ x \mid x \text{ is prime.}\}


I want to be able to use

\{ x \mid x § is prime.§\}


Currently I achieve this in a very primitive way:

\def§#1§{\text{#1}}%


But this does not nest (text inside math inside text inside math...) unlike when using the \text command.

I tried reading about catcodes, mathcodes, etc.pp. but unfortunately this is nowhere documented well enough for me to understand. Also it seems to me that the fact that I want to use § as opening AND closing symbol makes things a lot more complicated.

Can you help me?

It really isn't recommended that you do this. The switching behaviour of $ is one of the least desirable parts of the TeX syntax, which is why LaTeX introduces $$, and perhaps more importantly it won't work if you use utf8 encoding in classic TeX via inputenc. However... \documentclass{article} \usepackage[latin1]{inputenc} \def§{\relax\ifmmode\hbox\bgroup\else\egroup\fi} \begin{document} $\{ x \mid x § is a prime. {}>2 §(sigh)§ § \ast\}$ \end{document}  Added note which was too long for a comment. \text{...} or \(...$$ have distinct start and end markers, $ has a toggle feature which isn't really that good, so I don't recommend emulating it. But technically giving definitions to non-ascii characters is highly non portable between different TeX engines. In order to make utf8 work in (pdf)tex most of the non-ascii characters need to be given active definitions that decode the utf8 encoding so if you go in by hand as here and define that character (which is really two tokens in utf8 as seen by pdftex) you will disable a large part of the utf8 decoder. You could use the form in @egreg's comment

\newunicodechar{§}{\relax\ifmmode\hbox\bgroup\else\egroup\fi}


but then again that only works for utf8 input in classic tex.

LaTeX goes to a lot of trouble to give a uniform syntax to the underlying features, and the uniform syntax here is prefix function with mandatory argument, so \text{ ... }

• With UTF-8 you can use the newunicodechar package and say \newunicodechar{§}{\relax\ifmmode\hbox\bgroup\else\egroup\fi}. But I surely wouldn't recommend it. Apr 22, 2013 at 8:35
• @egreg Yes or with xetex you could define it with a different version again, but it's not recommended there either:-) Apr 22, 2013 at 8:39
• I don't quite understand what aspect is "not recommended"? Do you say that even nesting with \text is not recommended? Or only once you use §...§? If the latter I don't understand the difference, since all I want to do is use §...§ as shorthand for \text. I always use utf8 with pdftex. Also I tried to replace \hbox in your code by \text (as this would behave better with making text small in subscripts etc.), but then it stops working. Can you fix it? Apr 22, 2013 at 8:44
• @rmh comment got too long so I added a note to the answer Apr 22, 2013 at 9:00
• I see. Thanks for the explanations. I find the current situation a bit unsatisfactory, considering how often ones needs to place text inside formulas. I would not mind a "non-toggle" solution, similar to $$and$$, but I am not sure what brackets or similar are still free. Any suggestions? Apr 22, 2013 at 13:04

This isn't what you asked but it's a nice way to mix math mode and text mode without having to type \text (which is sort of what you want): build it into the macro.

\newcommand\setst[2]{\left\{#1\mid\text{#2}\right\}


Then you can type \setst{x}{$x$ is prime}.