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 have experienced a very frustrating feature of LaTeX.

I have used bnf.tex to be able to write a BNF grammar definition. As I found the notation <some text> very convenient, I decided to use it through the entire document. This might be a bad idea, but now I would rather not change the whole source.

Here are my macros:

{\catcode`\ =\active{\global\let =\ }}
\gdef<{\ensuremath{\langle}%
   \ifmmode\catcode`\ =\active\relax\fi%
   \begingroup\sf}
\gdef>{\/\endgroup\ensuremath{\rangle}%
   \ifmmode\catcode`\ =10\relax\fi%
   \relax}

It works very well, except when argument to complex math commands.

So

<cou cou>
$<cou cou>$
and $\sqrt{<cou cou>}$

will keep their inner space, but

$$\frac{<cou cou>}{E}$$
and $\Instr{<cou cou>}{E}$

will lose their's, looking like <coucou>.

In fact, any solution formatting the text between < and > as normal (\sf) text in both math and normal mode would be very useful here :-)

Here is a full file containing the examples:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}

\DeclareMathOperator{\instr}{Instr}
\newcommand{\Instr}[2]{\instr(#1,#2)}

\catcode`\>\active
\catcode`\<\active

{\catcode`\ =\active{\global\let =\ }}

\begingroup

    \gdef<{\ensuremath{\langle}%
        \ifmmode\catcode`\ =\active\relax\fi%
        \begingroup\sf}
    \gdef>{\/\endgroup\ensuremath{\rangle}%
        \ifmmode\catcode`\ =10\relax\fi%
        \relax}

\endgroup

\begin{document}

<cou cou>

$<cou cou>$

$$\frac{<cou cou>}{E}$$

$\Instr{<cou cou>}{E}$

\end{document}
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted

I would simplify the definition to this one:

\catcode`\<\active
\protected\def<#1>{\ensuremath{\langle\text{\normalfont\sffamily #1\/}\rangle}}

Example:

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}

\DeclareMathOperator{\instr}{Instr}

\catcode`\<\active
\protected\def<#1>{\ensuremath{\langle\text{\normalfont\sffamily#1\/}\rangle}}

\begin{document}

<cou cou>

$<cou cou>\frac{<cou cou>}{2}$

\[\frac{<cou cou>}{E}\]

$\instr(<cou cou>,E)$

\end{document}

I don't see any advantage in using an \Instr command that hides the semantics. Never use $$...$$ in LaTeX. Also consider that two-letter commands such as \sf are obsolete and should not be used: it's best to stick with the LaTeX2e commands \sffamily and similar ones that suggest more clearly what font attribute we are selecting; \normalfont is used to avoid that the surrounding font can influence the typesetting of the syntactic object.

However, instead of activating <, I suggest you

\protected\def\<#1>{\ensuremath{\langle\text{\normalfont\sffamily#1\/}\rangle}}

and then to input the thing as

\<cou cou>
share|improve this answer
    
this is very interresting. Why does it work ? I never saw such a definition embedding the ending character. This looks like pattern matching ;-) –  Layus Feb 26 '12 at 11:51
1  
@Layus This is an example of a delimited argument (for which it's necessary to use \def, as \newcommand won't accept it. More information in the TeXbook or in TeX by Topic (texdoc texbytopic). –  egreg Feb 26 '12 at 11:57
    
Thank you very much for the pointers. –  Layus Feb 26 '12 at 13:23
add comment

Generally it's a bad idea to change catcodes mid document, just use a box to switch to text mode (with an extra group in case you ever use color)

\documentclass{article}
\usepackage[utf8x]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}

\DeclareMathOperator{\instr}{Instr}
\newcommand{\Instr}[2]{\instr(#1,#2)}

\catcode`\>\active
\catcode`\<\active


\begingroup

    \gdef<{\ensuremath{\langle}\hbox\bgroup\bgroup\sffamily}
    \gdef>{\/\egroup\egroup\ensuremath{\rangle}}

\endgroup

\begin{document}

<cou cou>

$<cou cou>$

$$\frac{<cou cou>}{E}$$

$\Instr{<cou cou>}{E}$

\end{document}
share|improve this answer
    
I would have sweared that i tried this... Thank you anyway ! –  Layus Feb 26 '12 at 11:47
    
Can you please expand on your comment about colour? I always thought that the double group enclosure was to protect from \aftergroup used by NFSS and helps not to allow font changes to leak out. –  Yiannis Lazarides Feb 26 '12 at 11:51
    
@YiannisLazarides it is \aftergroup but the real problem cases are colour commands, specifically colour commands in boxes, where the colour restore is positioned by \aftergroup but needs to stay in the box. The kernel uses \color@endbox and friends which do nothing unless color is loaded, but that was just to increase compatibility with LaTeX2.09 packages a long while ago, may as well just use two groups directly in new stuff. –  David Carlisle Feb 26 '12 at 11:57
    
@DavidCarlisle Thanks that makes sense. –  Yiannis Lazarides Feb 26 '12 at 11:58
    
@Layus I was going to mention that \hbox doesn't get smaller in subscripts so if you need that you should use the ams packages and \text (which would also take care of colour support) but @egreg already replied with that so you might want to go with his. –  David Carlisle Feb 26 '12 at 12:00
show 3 more comments

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.