# How to add space around all inline equations, adjusting for punctuation

The answer to this question explains how to add space around inline equations, but it doesn't account for punctuation. You can see what I mean in the picture below. Is there a way to put the space after the comma, rather than before it? The obvious solution of putting the comma inside the equation environment, e.g., $A = \pi r^2,$ won't work because the fonts are different (I'm using XeLaTeX). That is, unless you manually specify each time that the comma should be a different font, but I'm hoping for a lazier solution (which accounts for all of the normal punctuation: periods, commas, semicolons, etc.).

If you use a non zero value for \mathsurround, you should add punctuation inside the formula and then restore the spacefactor afterwards.

\documentclass{article}

\newcommand{\fp}{%
\mbox{#1\xdef\fpsf{\the\spacefactor}}%
\aftergroup\setfpsf
}
\newcommand\setfpsf{\spacefactor\fpsf\relax}

\begin{document}

The area of a circle is given by $A=\pi r^2$, where $r$ is its radius.
The length of the circumference is $C=2\pi r$. Easy.

\mathsurround=6pt \xspaceskip=10pt

The area of a circle is given by $A=\pi r^2$, where $r$ is its radius.
The length of the circumference is $C=2\pi r$. Easy.

The area of a circle is given by $A=\pi r^2,$ where $r$ is its radius.
The length of the circumference is $C=2\pi r.$ Easy.

The area of a circle is given by $A=\pi r^2\fp{,}$ where $r$ is its radius.
The length of the circumference is $C=2\pi r\fp{.}$ Easy.

\end{document}


Here the settings are exaggerated by the high \xspaceskip, used just for testing that the additional space is inserted when appropriate (fourth paragraph). Here's the output of

The area of a circle is given by $A=\pi r^2\fp{,}$ where $r$ is its radius.
The length of the circumference is $C=2\pi r\fp{.}$ Easy.


when \mathsurround=3pt is used: Although I couldn't reproduce your example without a spurious space inside the \text command (see comment), you can use \kern (outside mathmode) or \mkern (inside mathmode) to fine tune spacing. Note that \mkern requires a special unit mu rather than your usual pt.

\documentclass{article}

\usepackage{amsmath}

\begin{document}

$\text{The area }A\text{ of a circle is given by } A=\pi r^2\text{ , where r is its radius.}% notice the space between the opening brace and the comma.$

The area $A$ of a circle is given by $A=\pi r^2$, where $r$ is its radius.

$\text{The area }A\text{ of a circle is given by } A=\pi r^2\text{\kern-4pt , where r is its radius.}% kerning inside mathmode$

The area $A$ of a circle is given by $A=\pi r^2$\kern-4pt, where $r$ is its radius.% kern outside of math mode

$V=\sum\int \mkern25mu \frac{4\pi r^3\!\mathop{}\mathrm dx}{3}% example of math kerning (command \mkern). 18 mu = 1 em$

\end{document}


EDIT: This answer seems not to be true if you use XeLaTeX.

I dont see your problem. This

\documentclass{article}
\begin{document}
The Area $A$ of a circle is given by $A=\pi r^2$, where $r$ is the radius.
\end{document}


results in: which is absolutly fine.

• I'm using XeLaTeX and the body font is different from the math font. May 3 '15 at 9:11
• Okay, that information wasn't given in the question, therefore this answer :) I edit my answer accorrding to this. May 3 '15 at 9:14