79

I'm wondering about the best way to do an explicit space character (I don't even know what it's properly called), like the ones you get in the \begin{verbatim*} environment. The closest I've found is

\sqcup

in math mode, but it seems like a bit of a hack to me. Is there any text symbol for what I'm looking for?

4 Answers 4

90

Nothing special is needed here; standard LaTeX provides \textvisiblespace:

\documentclass{article}

\begin{document}

An explicit space: a\textvisiblespace b

\end{document}

enter image description here

A simple variation of the original definition:

\DeclareTextCommandDefault{\textvisiblespace}{%
  \mbox{\kern.06em\vrule \@height.3ex}%
  \vbox{\hrule \@width.3em}%
  \hbox{\vrule \@height.3ex}}

allows to control the width using an optional argument:

\documentclass{article}

\newcommand\Vtextvisiblespace[1][.3em]{%
  \mbox{\kern.06em\vrule height.3ex}%
  \vbox{\hrule width#1}%
  \hbox{\vrule height.3ex}}

\begin{document}

An explicit space: a\textvisiblespace b

An explicit space: a\Vtextvisiblespace b

An explicit 1em space: a\Vtextvisiblespace[1em]b

An explicit 1cm space: a\Vtextvisiblespace[1cm]b

\end{document}

enter image description here

2
  • 1
    that's pretty cool. Is there an easy way to control the amount of space to its left and right? I use this in monospace, and it gets too close to its right neighbour.
    – lenz
    Jun 20, 2013 at 13:03
  • @lenz please feel free to open a new question. Jun 20, 2013 at 13:49
10

The simplest way to get it is by printing character 32 in typewriter font:

 \texttt{\char32}
1
  • 3
    This relies imho to much on the font encoding. E.g. it won't work if you use xetex/fontspec or if you type in greek (LGR-encoding). Apr 5, 2012 at 9:01
7

You can store that character (a squat-u) inside a box and use it as necessary:

enter image description here

\documentclass{article}
\newsavebox{\spacebox}
\begin{lrbox}{\spacebox}
\verb*! !
\end{lrbox}
\newcommand{\aspace}{\usebox{\spacebox}}%
\begin{document}
Hi\aspace there!
\end{document}

You cannot directly store it in a macro, since verbatim content cannot be passed as an argument. However, boxing it via an lrbox environment works. Use the squat-u via \aspace.

Another option using fancyvrb:

\documentclass{article}
\usepackage{fancyvrb}
\fvset{showspaces=true}
\SaveVerb{verbspace}! !
\newcommand{\aspace}{\UseVerb{verbspace}}%
\begin{document}
Hi\aspace there!
\end{document}
3
  • The output is very nice but when I use it inside a hidden item in beamer, it remains visible.
    – yannis
    May 25, 2019 at 10:47
  • @yannis: Can you show an example?
    – Werner
    May 25, 2019 at 16:06
  • In beamer: \begin{frame}[fragile]\frametitle{Title}\begin{itemize}\item<1-2>Blabla, \item<2>Bla\aspace bla\end{itemize}\end{frame}, then when the second item is supposed to be invisible, the explicit blank space will remain visible.
    – yannis
    Jun 1, 2019 at 17:50
4

Unicode character uni2423 is present in a number of TeX fonts (Stix, Latin Modern Roman, a few others). If you are using LuaLaTeX or XeTeX, you can call the character directly, or paste it from a character map.

This is not necessarily better than the methods discussed in other replies. It is just another way to do it. Let's see if it works here. If you see the character between A and B below (pasted from character map), it means your browser's font has it (mine does):

A␣B

This method also works in LuaLaTeX and XeTeX as \verb|A␣B| as long as the font has the symbol.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .