12

When you press a space then you add whitespace in a document. Is there another way to insert the same space with other commands such \hspace etc.?

I don't ask generally about space commands... I ask specifically about a command that produces a space with such a width as the one produced using the Space key in the keyboard.

  • Not sure if this is what you're after, but you can use an escaped space \ which will add a space. You can then chain these so that \ \ \ will give three spaces, whereas three hits of the space bar would give only 1. ~ creates a non-breaking space of the same length, so you can also use that – Au101 Jun 15 '17 at 1:35
  • \kern<dimen> is similar to \hspace, but does not allow breaking, and cannot go negative past the left margin, unlike \hspace. In math mode \mkern can be used, with dimensional units of mu. The \tabto macro from the package of the same name allows versatile horizontal movement to a specified point relative to the left margin (forward or backward). It also remembers where you last tabbed to. – Steven B. Segletes Jun 15 '17 at 1:52
  • And there's \hfill and in math mode there are many commands like \; \! \. plus with amsmath you further get \quad and \qquad. – JPi Jun 15 '17 at 1:54
  • ...and \hfil. And \space. Also, \@ makes sure the space that follows is treated like a normal space, as opposed to an end-of-sentence space, as in Dr.\@ Frankenstein. – Steven B. Segletes Jun 15 '17 at 1:56
  • 1
    Don't forget the ubiquitous \phantom{} for providing space equal in width to the given argument. – Steven B. Segletes Jun 15 '17 at 2:07
14

You've gotten a lot of answers in the comments but let me see if I can come up with a list. (I'm going to ignore math mode and the various leaders.)

There are a bunch of ways to get space that's the same as a normal space. First, of course, you can just type a space or a tab. In most cases, a newline on a nonblank line that doesn't have a comment is the same as a space. You can also enter a space using ^^20 but there's really no advantage to doing so since ^^20 is exactly the same as if a space character were in the input at that point.

Okay, so beyond an ASCII space, tab, newline, and the ^^ method, the options that I see are \space, \ , ~, \hskip, and \kern (and, of course, any LaTeX macros on top of those such as \hspace and \hspace*).

  • \space is a macro defined as \def\space{ }. That is, it expands to a single space token.
  • \ (backslash space). This is a control space. It behaves exactly like a space token does when \spacefactor is 1000, more on this below.
  • Control newline (really carriage return, but this part of TeX is pretty esoteric and not important here) is a macro that expands to a control space.
  • ~ is a tie. It is a nonbreaking version of the control space. It is essentially \nobreak\.
  • \hskip plus some font parameters can construct a normal interword space.
  • \kern plus a font parameter can construct a nonbreaking space that doesn't stretch or shrink.

The font parameters we need for \hskip and \kern are parameters 2, 3, and 4 which we can use as so.

\hskip\fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font
\kern\fontdimen2\font

Of these choices, all of them except for the \kern will expand or shrink like normal spaces as the glue set for the line changes (there's an example below).

Additionally, only the space tokens (which you get from literal spaces, tabs, and usually a newline) are affected by the \spacefactor which controls how much the next space stretches or shrinks. The space factor in the example below is 3000 (which is the value set after a period) whereas 1000 means no extra stretching or shrinking for the space.

enter image description here

\documentclass{article}
\usepackage{parskip}
\begin{document}

\vrule{} \vrule\  Normal space.\\
\vrule{}\space\vrule\ \verb!\space! macro.\\
\vrule\ \vrule\ Control space.\\
\vrule~\vrule\ Tie (\verb!~!).\\
\vrule\hskip\fontdimen2\font plus\fontdimen3\font
minus\fontdimen4\font\vrule\ \verb!\hskip!.\\
\vrule\kern\fontdimen2\font\vrule\ \verb!\kern!.

Using \verb!\break! to stretch the glue on each line.\\
\vrule{} \vrule\ Here is some text which will change spacing.\break
\vrule{}\space\vrule\ Here is some text which will change spacing.\break
\vrule\ \vrule\ Here is some text which will change spacing.\break
\vrule~\vrule\ Here is some text which will change spacing.\break
\vrule\hskip\fontdimen2\font plus\fontdimen3\font minus\fontdimen4\font\vrule\
Here is some text which will change spacing.\break
\vrule\kern\fontdimen2\font\vrule\ Here is some text which will change spacing.\break

Setting the \verb!\spacefactor! to a period's space factor code.\\
\vrule\spacefactor=\sfcode`.{} \vrule\  Here is some text which will change spacing.\break
\vrule\spacefactor=\sfcode`.{}\space\vrule\ Here is some text which will change spacing.\break
\vrule\spacefactor=\sfcode`.\ \vrule\ Here is some text which will change spacing.\break
\vrule\spacefactor=\sfcode`.~\vrule\ Here is some text which will change spacing.\break
\vrule\spacefactor=\sfcode`.\hskip\fontdimen2\font plus\fontdimen3\font
minus\fontdimen4\font\vrule\ Here is some text which will change spacing.\break
\vrule\spacefactor=\sfcode`.\kern\fontdimen2\font\vrule\ Here is some text which will change spacing.\break
\end{document}
  • Excellent answer. Very thorough! Thank you! – Adam Jun 15 '17 at 3:36
  • You can use \hskip\fontdimen2\font..., without \the (which also does an undesired expansion). Similarly, \spacefactor=\sfcode`. is perfectly good. – egreg Jun 15 '17 at 8:30
  • @egreg, thanks! That's a very good point. I've updated my answer. – TH. Jun 15 '17 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.