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.

Adam
  • 4,684
  • 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 01: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 01: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 01: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 01:56
  • In smaller math styles, the scalerel package provides \LMpt and \LMex inside of the argument to \ThisStyle{} to give pt and ex dimensions that are scaled to the local math font size, if you are in \scriptstyle or \scriptscriptstyle, so that the spacing in $\ThisStyle{x\hspace{2\LMex}y}$ will be different from$\scriptscriptstyle\ThisStyle{x\hspace{2\LMex}y}$. – Steven B. Segletes Jun 15 '17 at 02:03
  • 1
    Don't forget the ubiquitous \phantom{} for providing space equal in width to the given argument. – Steven B. Segletes Jun 15 '17 at 02:07
  • @JPi I don't ask generally about space commands... I ask specifically about a command that produces a space with width same with the one produced using the Space key in the keyboard. – Adam Jun 15 '17 at 02:07
  • @StevenB.Segletes I think that my question is misunderstood and that is my fault. See my previous comment. :P – Adam Jun 15 '17 at 02:08
  • A carriage return in your input file, if a line-ending % is absent, inserts a normal space. – Steven B. Segletes Jun 15 '17 at 02:08
  • Keep in mind that the natural width of a space can vary, in order to achieve full justification of text lines. – Steven B. Segletes Jun 15 '17 at 02:10
  • @StevenB.Segletes Yes I know that but I believe that there is a standard length for the space between two words. :) – Adam Jun 15 '17 at 02:10
  • In many fonts, a space is the same as 0.666ex. Is that what you want? Of course, in \ttfamily, it is 1ex. – Steven B. Segletes Jun 15 '17 at 02:11
  • It is font dependent. \ttfamily need not give 1ex. It will do so only (usually) with a monospaced font and \ttfamily is, strictly speaking, 'typewriter' and not monospaced. For example, Latin Modern has a variable width typewriter, as well as a monowidth one. – cfr Jun 15 '17 at 02:22
  • The thing is, that if you say \hspace{0.666ex}, this is not going to be equivalent to typing a space, even if the font's nominal inter-word space is 0.666ex and even if you place it in the same context i.e. between two words. Because inter-word space is gluey, whereas 0.666ex is not. – cfr Jun 15 '17 at 02:25
  • @cfr This is a good answer. You can post it if you like. – Adam Jun 15 '17 at 02:25
  • @Werner I don't care for commands for horizontal spacing as I already know them all...I want to see which one produces a normal space the one that exists between two words...I have said it in another comment before. :) – Adam Jun 15 '17 at 03:28
  • @Adam \space, if you don't want to type a space; I can't understand what you're after. – egreg Jun 15 '17 at 08:28

1 Answers1

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}
TH.
  • 62,639
  • Excellent answer. Very thorough! Thank you! – Adam Jun 15 '17 at 03: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 08:30
  • @egreg, thanks! That's a very good point. I've updated my answer. – TH. Jun 15 '17 at 14:55