Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

Instead of having to write

\{ 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?

share|improve this question
    
Welcome to TeX.SX. Note that you don't have to sign with your name since it automatically appears in the lower right corner of your post. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say "thank you" to users who helped you. –  Claudio Fiandrino Apr 22 '13 at 8:35
add comment

2 Answers

up vote 11 down vote accepted

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...

enter image description here

\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{ ... }

share|improve this answer
    
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. –  egreg Apr 22 '13 at 8:35
    
@egreg Yes or with xetex you could define it with a different version again, but it's not recommended there either:-) –  David Carlisle Apr 22 '13 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? –  rmh Apr 22 '13 at 8:44
    
@rmh comment got too long so I added a note to the answer –  David Carlisle Apr 22 '13 at 9:00
    
@rmh: fundamentally, what’s “not recommended” is toggles (like $). you pointed out that your original def wouldn’t work for text-inside-maths-inside-text -- that’s precisely because you invented a toggle command. –  wasteofspace Apr 22 '13 at 12:20
show 5 more comments

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}.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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