21

As title, what is the difference between a and ~?

I see no differences after the compilation.

MWE:

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}

\title{Your Paper}

\author{You}

\date{\today}

\begin{document}
\maketitle

I am cool.

I~am~cool.

\end{document}
6
  • 2
    You will see the difference at end of line. ~ is unbreakable space where as space isn't unbreakable.
    – user11232
    Mar 28, 2014 at 1:54
  • 2
    "I am cool"... That's a rather bold statement :p
    – jub0bs
    Mar 28, 2014 at 2:23
  • It controls space too, it's very often used after a \the.... counter output command.
    – user31729
    Jan 13, 2015 at 21:27
  • 2
    In text mode ~=\nobreakspace Jan 13, 2015 at 21:29
  • @yo' Should I delete this questions, then?
    – hkviktor
    Jan 13, 2015 at 21:31

4 Answers 4

26

The difference can be seen at the end of line. A space is breakable and a ~ is an un-breakable space.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[colorinlistoftodos]{todonotes}

\title{Your Paper}

\author{You}

\date{\today}

\begin{document}
\maketitle

\hspace*{0.9\textwidth}I am cool.

\hspace*{0.9\textwidth}I~am~cool.

\end{document}

enter image description here

The three words I am and cool are glued together in the second line.

0
22

The ~ is an active character, which means it's the same as a macro like \mbox and so on. Its function is described by its definition, which is

\nobreakspace{}

so typing ~ is equivalent to typing \nobreakspace{}.

What does \nobreakspace do? Here it is:

\leavevmode\nobreak\ 

(a space follows the last backslash). So a paragraph is started or nothing is done if we're already in a paragraph (\leavevmode); then \nobreak is issued, which disallows any line break at the point (\nobreak) and then a normal interword space is inserted.

Thus when typing

no~break

there will be a space between the two words, but the line will not be broken after no.


Why the {} after \nobreakspace? If you have no~break in a caption, in the .aux file the expanded version will appear

no\nobreakspace  {}break

The braces have been introduced to cope with the rare case when a space after ~ is wanted; without them no~ break would write

no\nobreakspace   break

and, upon reading the .aux file, the space would be ignored. With the braces

no\nobreakspace  {} break

will be written and the additional space would not be ignored.


What happens if one types no~ break in the output? Two spaces are added but no line break at them is possible, because a space is a feasible line break point only if it is not preceded by a discardable item; since \nobreakspace becomes

\leavevmode\penalty 10000 \ 

and penalties are discardable like spaces, neither nor the following space can be used for a line break.

In the reverse case no ~break two spaces are output, but now a break point is possible at the first space (and both will disappear together with the penalty in case it is taken).

5
  • Nice explanation. Can I say +1? Jan 13, 2015 at 21:40
  • You speak about the no~ break but not about no ~break. In particuler this case may appear when using a char limitation per line with a line return intead of the space. What happen in this case? Oct 22, 2015 at 12:40
  • @RomainPicot Almost the same, you get two spaces if no break happens, but a break is possible (which is not with no~ break)
    – egreg
    Oct 22, 2015 at 12:48
  • @egreg thank you for the precision ;-) Oct 22, 2015 at 12:50
  • 1
    @RomainPicot I added a final note, thanks for the prompt.
    – egreg
    Oct 22, 2015 at 12:55
5

This is the definition of ~ in latex.ltx

\catcode `\~=13
\def~{\nobreakspace{}}

while \nobreakspace is defined as

\DeclareRobustCommand{\nobreakspace}{%
   \leavevmode\nobreak\ }

So, the active character ~ is equivalent to a space that cannot be broken into lines.

0

The other answers cover what ~ is and does. The most common reason why I use it is when I want to say something like "Figure 1" or "Theorem 2", where having a space before the number (along with unfortunate line breaking) could cause the number to appear as the first thing on the next line. This would look a bit as if I wanted to start an enumeration, which is not my intent. Therefore, I would write Figure~1 or Theorem~2 in my code to prevent that from happening.

In general, you should let TeX decide on the line breaking for itself, and not litter your code with too many ~. The downside of using it is that you end up with an unbreakable box, which makes line breaking difficult. In the "I~am~cool" example of the accepted answer, for example, the box probably extends into the right margin, which is unsightly and causes an overfull hbox warning.

You must log in to answer this question.

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