[Tex/LaTex] Customise \textvisiblespace

math-modespacingsymbolstypewriter

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?

Best Answer

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.