# Can I have an “almost-non-breaking” space in LaTeX?

As suggested in, e.g., this question, I use non-breaking spaces in LaTeX to avoid some bad line breaks, e.g., "`the dimension of~\$X\$`"

However, in some cases, this leads to words being hyphenated in an unpleasant way. For instance, when writing "`Let \$E\$ be a vector space of dimension~\$d\$`", everything is usually fine, but sometimes LaTeX will decide to hyphenate "`dimension`". Yet I find the line break "`of dimension / \$d\$`" less jarring than the line break, e.g., "`of dimen-/sion \$d\$`".

I fix examples by hand when they occur, by removing the non-breaking space; but this is unpleasant because it needs to be added back whenever the text changes.

It seems to me like the correct solution would be to have an "almost-non-breaking space", i.e., I would like to be able to write "`Let \$E\$ be a vector space of dimension#\$d\$`" with some character or macro `#`, with the effect that the space should not be broken except if it would cause a hyphenation. Is there a reasonable way to achieve this?

[As TeX has a way to tweak the penalty associated with hyphenations (see, e.g., this answer), I would imagine that there ought to be a way to specify that a line break at one point should be possible but discouraged unless a word needs to be hyphenated... hence this question.]

• There is always `Let \$E\$ be a vector space of \mbox{dimension}~\$d\$`, but that, of course, makes appropriate hyphenation harder. In such cases, `sloppypar` is an option. – Steven B. Segletes Dec 22 '16 at 18:41
• @StevenB.Segletes: Thanks, I hadn't even thought of this! It's a good idea, but as you pointed out this will make LaTeX mess up in cases where hyphenating "dimension" is the only reasonable solution... so it's not ideal. – a3nm Dec 22 '16 at 18:47
• Try `dimension\penalty1000\ \$d\$`. This means that the space is a viable breakpoint but breaking will cost a penalty of 1000. A penalty of 10000 would correspond to `~`. (My advice, don’t try to outsmart the system) – Henri Menke Dec 22 '16 at 19:10
• @Werner: Yes, thanks for your point, but the hope would be that such a macro would reduce the number of cases where manual intervention would be required. I don't like leaving stuff for "end-of-document modifications" because one always finds more "last things" to change to the document in the end... – a3nm Dec 22 '16 at 19:10
• @HenriMenke: Thanks! After testing, this appears to work fine with a penalty of 100; starting at 101 LaTeX seems to prefer to hyphenate a word. Then I guess the easiest way to use it would be, e.g., to `\newcommand{\nb}{\penalty100\ }`, right? If yes, please feel free to post your comment as an answer! – a3nm Dec 22 '16 at 19:26

## 1 Answer

``````   a\nolinebreak [3] b
``````

Has a normal space and discouraged line break

• Thanks! From trying it out, this works, but the value should be `[1]`, not `[3]`, otherwise LaTeX still prefers to hyphenate the word. As per tex.stackexchange.com/a/94220 this is consistent with my experiments with @HenriMenke's solution in the comments to my question. – a3nm Dec 22 '16 at 22:41
• @a3nm [1] is a penalty of 51 so hardly any discouragement at all (51 in a range 0-10000) , it will be almost always do the same as just using a normal space, but if it does what you want then that's fine:-) – David Carlisle Dec 22 '16 at 22:45
• however downvoted, care to leave a comment saying why? – David Carlisle Dec 22 '16 at 22:47
• @a3nm: Elaborating on David’s comment, it’s true that `\linebreak[1]\space` charges a “penalty” of 51 (arbitrary units) for breaking the line at the `\space`, but, on the other hand, a break at a hyphen (inserted during hyphenation) is charged 50 units under the conventions usually adopted by LaTeX. Thus we see that `[1]` does correspond to the minimum penalty value that will make TeX prefer the break at the `\space` over the break at the hyphen, if this does not otherwise affect the “badness” of the lines being formed (actually, the total demerits, but this would be a pretty long story…). – GuM Dec 22 '16 at 23:22
• @GustavoMezzetti yes `~` is like `\nobreak\ ` rather than `\nobreak\space` so ignores spacefactor – David Carlisle Dec 22 '16 at 23:29