The accepted answer in this question shows a neat redefinition of \textvisiblespace, which I like to use because it doesn't produce a warning in math mode.
However, I use it with monospace in an equation, and it doesn't look perfect: the symbol is too close to its right neighbour. Here's an MWE:
\documentclass{article}
\newcommand\vartextvisiblespace[1][.3em]{%
\mbox{\kern.1em\vrule height.3ex}%
\vbox{\hrule width#1}%
\hbox{\vrule height.3ex}
}
\begin{document}
A formula with visible space in it:
\begin{equation}
s = \mathtt{a\vartextvisiblespace string\vartextvisiblespace sequence}
\end{equation}
\end{document}
What is a clean way to make it look better?


alignenvironment, and use\xfor the name of the space (irrelevant except that i want to include the code here), add these two lines to the display:s &= \mathtt{another\x such\x string}\\ s &= \mathtt{\x\x\x\x\x\x\x\x one\x more}and take a look. the letters in successive lines that should be lined up are just slightly offset from one another, so the effect is not truly that of a monospace font. (i've made this mistake so many times myself that i tend to anticipate it. i did test.) – barbara beeton Jun 20 '13 at 17:340.5emas opposed to0.3em). – Werner Jun 20 '13 at 17:47\mboxmust be precisely the width of a letter in the monospace font. egreg has used\hrulefillwhich adjusts the width of the rule rather than possibly modifying the width of the box. – barbara beeton Jun 20 '13 at 17:530.5emas in egreg's answer is too short... – Werner Jun 20 '13 at 18:02ttfont sets the character width to.5em(well, half the nominal point size), so i'm not surprised that egreg used that. but measuring the actual width is never a bad idea (although it would need adjusting if the type size changes mid-document). – barbara beeton Jun 20 '13 at 18:30