Customise \textvisiblespace

The glyph you're building is wider than letters in a monospaced font; use 0.5em as width and simplify the definition:

\documentclass{article}

\newcommand\vartextvisiblespace[1][.5em]{%
  \makebox[#1]{%
    \kern.07em
    \vrule height.3ex
    \hrulefill
    \vrule height.3ex
    \kern.07em
  }% <-- don't forget this one!
}

\begin{document}

A formula with visible space in it:

$s = \mathtt{a\vartextvisiblespace string\vartextvisiblespace sequence}$

\ttfamily
a\vartextvisiblespace\vartextvisiblespace\vartextvisiblespace a

abcde

\end{document}

enter image description here

You can fine tune the width with \vartextvisiblespace[.3em] or similar.


Just add a small \kern on the right-hand side (equivalent to that the left-hand side):

enter image description here

\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath

\newsavebox{\textvisiblespacebox}
\savebox{\textvisiblespacebox}{\texttt{a}}
\newcommand\vartextvisiblespace[1][\wd\textvisiblespacebox]{%
  \makebox[#1]{\kern.1em\rule{.4pt}{.3ex}%
  \hrulefill%
  \rule{.4pt}{.3ex}\kern.1em}%
}

\newcommand{\x}{\vartextvisiblespace}% For ease-of-use
\begin{document}

\begin{align*}
  a &= \mathtt{a\x string\x sequence} \\
  b &= \mathtt{a\x\x\x\x\x\x\x\x sequence} \\
  c &= \mathtt{a\x\x\x\x\x\x\x\x\x\x\x\x\x\x\x e}
\end{align*}

\end{document}

I've also updated the construction of \vartextvisiblespace to be more LaTeX2e-y. It measures the width of a regular typewriter font character, and uses it to place establish the box width of the visible space.