Getting “n-dimensional” to look right in LaTeX

LaTeX has some interesting rules for dashes and hyphenation that are hard to get right in certain scenarios. For example, if you use the term “n-dimensional” and it happens to fall near the end of a line, you’ll end up with something like this:

text text text text text n-

To get this right, try inserting this snippet in your document’s preamble:


Then, replace “n-dimensional” in the document with the following:


or, to get the math environment look to the “n”, do this:


What’s going on in the snippet?

The #1 refers to whatever you added as a parameter to the command, which in the example above is “n”.

The \nobreakdash needs to be combined with a dash character. That means that in other situations you may just need to do this:


While the output will be fine for many use cases, the previous snippet with the \discretionary command is a more general-case solution. (See this forum post for more info.)

The \hspace{0pt} tells LaTeX not to allow any space (including a newline) between the previous character and the one that follows, acting like glue between the dash added with the \discretionary command and the “d” of dimensional. Without it, you’ll get output like this:

text text text text text n-

Edit: Turns out you need the amsmath package for LaTeX to know what the \nobreakdash command is. I’ve added the necessary command.