# Adding Non-breaking space after single character in text mode

Is it possible in LaTeX (2e) to create macro or other processing command that will add non-breaking space after single character in text mode?

Example:

A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$


should produce:

A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$


I know that I can use regular expresion like: s/$$\<[a-z]\>$$[ ]/\1\~/g, but this one will also change text in math mode and listing/verbatim mode.

• This is a well known problem for Polish, Czech and Slovak, with their one letter prepositions. Feb 26, 2014 at 0:20

It would be a bit fragile to do that in TeX: I'd do the replace with a regex in the editor as you suggest. (If you just wanted to make a ~ in text and a normal space in math that would be easier, but having it work in listings and verbatim would be harder)

For example given

\documentclass{article}

\usepackage{amsmath}

\begin{document}

A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}

\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}

A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}

\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}

\end{document}


The command M-x addtilde in emacs produces

\documentclass{article}

\usepackage{amsmath}

\begin{document}

A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}

\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}

A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}

\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
$a b c = 123$
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}

\end{document}


where addtilde is defined by

(defun hidespaceenv (e)
(goto-char (point-min))
(while (re-search-forward
(if (string-equal e "[") "\\\\\$" (if (string-equal e "") "\\" (concat "\\\\begin{" e "}"))) nil 1) (while (re-search-forward "\$$[a-zA-Z]\$$\$$\\s-+\$$\\|\$$\\\\begin\$$" (save-excursion(re-search-forward (if (string-equal e "[") "\\\\\$"
(if (string-equal e "$") "\\$"
(concat "\\\\end{" e "}")))
nil 1) (point)) 1)
(replace-match "\\1SPACE@@\\2\\3@@" t))))

(interactive)
(mapcar hidespaceenv (list "verbatim" "align" "equation" "[" "\$"))
(goto-char (point-min))
(while (re-search-forward "\$$\\(^\\|\\s-+\$$[a-zA-Z]\\)\\s-+" nil 1)
(replace-match "\\1~"))
(goto-char (point-min))
(while (re-search-forward "SPACE@@\$$\\s-+\\|\\\\begin\$$@@" nil 1)
(replace-match "\\1")))
`
• Thank you. I think that it's very close to final answer, but unfortunately, you can use lstlisting to create new listing environments, so I need to provide all environments that are type of verbatim/listing mode. Feb 26, 2014 at 21:58
• @faramir yes and other math environments such as align* or displaymath or ... (it also gets confused if you have comments like %\begin{align} but it is perhaps useful in real cases, even though it is easy to construct bad tests) Feb 26, 2014 at 22:02
• I wonder is it possible to use answer from tex.stackexchange.com/questions/10033/… to insert '~' after single letter tokens on text mode? Feb 27, 2014 at 2:00
• This. Function. Is. Amazing. This is honestly the best idea I've ever seen as an editor macro. Feb 28, 2014 at 21:00
• @SeanAllred still waiting for the vim crowd to supply the vim version:-) Feb 28, 2014 at 21:46