The only non-breakable space command in ConTeXt that I know of is the tilde (~) command, but I'd like to have finer control over how long the non-breaking space will be. In LaTeX, I could accomplish this with \hspace*, but ConTeXt only offers the non-starred version of the command, and this produces a breakable space.

Does ConTeXt offer a non-breaking version of its \hspace command, or is there another preferred way of doing this in ConTeXt? I'd be content with temporarily modifying the width of the ~ space, but I'm also not sure how I might do that.

  • 2
    Are you familiar with \kern1cm, \kern5em, etc? – Mico Apr 15 at 3:23
  • I was not, so thanks for letting me know! – Joey McCollum Apr 15 at 12:08

You can use \kern as Mico suggested in a comment, however, that has the disadvantage, that it doesn't stretch or shrink.

To get a horizontal space that can't be broken around and that stretches and shrinks like interword space, you can use

\nobreak\hskip1em plus 1em minus .5em\nobreak 

However, that also doesn't fit well with the ConTeXt philosophy of first class Unicode support. Instead of specifying space manually, maybe have a look at all the predefined horizontal spaces in spac-chr.mkiv, which are natively supported by ConTeXt. The big advantage of those is that they properly export to XML and are copy-pastable from PDF. If they are breakable, it should be possible to just surround them with \nobreak like above.

Here is a shortened excerpt from spac-chr.mkiv:

\edef\nobreakspace            {\normalUchar"00A0} % space
\edef\softhyphen              {\normalUchar"00AD} % softhyohen
\edef\enquad                  {\normalUchar"2000} % quad/2
\edef\emquad                  {\normalUchar"2001} % quad

\let\ideographicspace         \enquad
\let\ideographichalffillspace \emquad

\edef\twoperemspace           {\normalUchar"2002} % quad/2
\edef\threeperemspace         {\normalUchar"2004} % quad/3
\edef\fourperemspace          {\normalUchar"2005} % quad/4
\edef\sixperemspace           {\normalUchar"2006} % quad/6
\edef\figurespace             {\normalUchar"2007} % width of zero
\edef\punctuationspace        {\normalUchar"2008} % width of period
\edef\breakablethinspace      {\normalUchar"2009} % quad/8
\edef\hairspace               {\normalUchar"200A} % quad/8
\edef\zerowidthspace          {\normalUchar"200B} % 0
\edef\zerowidthnonjoiner      {\normalUchar"200C} % 0
\edef\zerowidthjoiner         {\normalUchar"200D} % 0
\edef\narrownobreakspace      {\normalUchar"202F} % quad/8

\udef\zerowidthnobreakspace   {\penalty\plustenthousand\kern\zeropoint}
\udef\fiveperemspace          {\hskip\dimexpr\emwidth/5\relax}
| improve this answer | |
  • Thank you for this thorough answer! It looks like \nobreak was the command I needed. I originally would've used something like \nobreak\hspace[medium] (and this works when I test it), but \nobreak\enquad creates a non-breaking space of the same width, and it does so with all the Unicode-related advantages you outlined above! – Joey McCollum Apr 15 at 12:14

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.