I find myself spending effort in placing the tilde character before every inlined math I type, e.g.,

   On 1899, G. Pick proved that 
the area of a simple polygon~$P$ whose vertices are 
located on the integer grid is~$i + b/2 -1$, 
where~$i$ is the number of grid points in the interior of%
~$P$ and~$b$ is the number of grid points on the
the boundary of~$P$.

Can anyone think of a way of redefining the opening $ character so that it would do that automatically. Many will find this useful, methinks. Ideally, I would write instead just

  On 1899, G. Pick proved that
the area of a simple polygon $P$ whose vertices are 
located on the integer grid is $i + b/2 -1$, 
where $i$ is the number of grid points in the interior of
$P$ and $b$ is the number of grid points on the
the boundary of $P$.

I think, but not sure, that this is doable with the following method:

  1. Use character other than $ for inline math (or refer internally to \( and \))
  2. Define $ as an active character which \unskips previous spaces, replacing them with non-breakable space.
  3. After un-skipping, let $ redefine itself to do the closing part.
  4. The closing $ will invoke \) and then will redefine $ as in 2.

An obvious bug of this method would be that displayed math wrapped with $$ would misbehave. I have an unpleasant feeling that there might be more.

  • Yes, 2. and then 1. popped into my mind too. One problem with global catcode changes is that they can mess with other packages which authors really didn't thought that $ would ever by anything then catcode 3. My tikz-timing package for example would stop supporting $ characters in timing strings. You might just do search and replace: s/ \$/~$/g instead over your documents. – Martin Scharrer Feb 21 '11 at 9:00
  • 1
    The catcode changes will also not work inside macro arguments and environments which collect their body (e.g. tabularx, environ envionments, ...). – Martin Scharrer Feb 21 '11 at 9:03
  • @Martin — Hmmm, I wonder if environ should use \scantokens internally to help overcome this problem (doesn't fix it entirely, of course) – Will Robertson Feb 21 '11 at 9:21
  • 2
    @Yossi: I'd definitely not put a ~ in front of $i + b/2 -1$. So I'd make the question harder and ask for a ~ only in front of $<single token>$! – Hendrik Vogt Feb 21 '11 at 10:03
  • 2
    I think I'd go for a new command, \m that expanded to \unskip~\(#1\) (or whatever the correct syntax is) since, as others have pointed out, you don't actually want to do this all the time. – Andrew Stacey Feb 21 '11 at 10:59

Here's an example using the active characters approach. I'm not against this sort of idea for your own documents, but do note that it's probably not the most robust approach (like Martin comments, this could affect the inner workings of other packages—doing it \AtBeginDocument is probably a good idea).

  \ifdim\lastskip>\z@ \unskip\textvisiblespace \fi
  \mathshift #1\mathshift
\DeclareRobustCommand\({% from fixltx2e
  \ifdim\lastskip>\z@ \unskip\textvisiblespace \fi
  \ifmmode \@badmath \else \mathshift\fi
\section{Introduction} Hello $a+b$
\section{Not sure $x=y$. $x=z$ though!}
And \( f(x) = x^2 \) too.

We'd better ensure no space gets added if there's nothing before the maths:
$a$ & $b$ \\
$c$ & $d$ \\

In order to make the example actually show the idea, I've used a visible space instead of a hard space. Remove \textvisiblespace above with ~ for your actual work :)

Also, I've assumed above that you're not using $$...$$ for display math, as strictly speaking it's not valid LaTeX syntax.


One way for not breaking other people's packages is to rely on the fact that package writers tend to be conservative, and adhere to ASCII. So, I would redefine the same set of macros that Will did, but have these parameterized by the math character, which can default, or not default, to $.

I am clueless as to how one can do this, but just imagine you could write

\usepackage[begin=〈, end=〉]{nbsm}

and then proceed, wrapping your inlined math with and . Or if you are the daring type,

\usepackage[begin=$, end=$]{nbsm}
  • 1
    Puh, I'm not sure, but is this really an answer? – Hendrik Vogt Feb 21 '11 at 11:43
  • @Hendrik, call it a partial answer if you like. Or, think of it as a full answer (rewrite Will's code with 〈 and 〉), which raises, on its way, another small question. – Yossi Gil Feb 21 '11 at 14:55

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.