171

I want to put pdflatex to not put a hyphen in a word went it comes to the end of the line.

I tried to put a \- in the beginning or the end of that word but it doesn't work.

For example if I put:This is a very long sentence that needs two lines with thatshouldnot\- be hyphened I will get:

This is a very long sentence that needs two lines with thatshouldnot-
be hyphened

and if I do it this way: This is a very long sentence that needs two lines with \-thatshouldnot be hyphened I will get:

This is a very long sentence that needs two lines with -
thatshouldnot be hyphened

I want 'thatshouldnot' to stay in one word and with no hyphen in the beginning or the end of the word.

Werner
  • 603,163
MSIS
  • 1,877
  • 3
    Do you want to suppress hyphenation of (a) all words everywhere, (b) all words of that paragraph, (c) all occurences of thatshouldnot, or (d) only this one word thatshouldnot ? – yo' Aug 17 '12 at 14:30
  • @tohecz (c) all occurences of thatshouldnot – MSIS Aug 17 '12 at 15:28
  • Is this similar to my question? http://tex.stackexchange.com/questions/100010/automatically-prevent-line-break-at-hyphen-of-specific-words-e-g-dual-self – Jörg Apr 19 '13 at 13:01
  • @Jörg Yes, it is. – axolotl Apr 29 '18 at 08:59

2 Answers2

204

if this word appears more than once in your document, and you never want it to be hyphenated, you can suppress hyphenation everywhere by putting this line in the preamble:

\hyphenation{thatshouldnot}

if it only occurs once, then david's answer using \mbox suffices.

addendum:
A language that treats an apostrophe as a letter (such as Italian; see Preventing hyphenation of a word with \hyphenation does not always work) and is processed with Xetex may not recognize an exception entered with \hyphenation. In the cited case, there are two workarounds. For the first, replace the ASCII apostrophe (U+0027) by the right single quotation mark (U+2019). \mbox should always work as a last resort.

  • 1
    it does not work for an acronym like `K-PLUS' – Valerio May 17 '16 at 13:01
  • @Valerio -- can you give an example? how is the acronym input/referenced? – barbara beeton May 17 '16 at 13:03
  • as a simple text: "..if we consider K-PLUS then we would..." – Valerio May 17 '16 at 13:35
  • 3
    that already contains a hyphen. the only practical way to suppress a break in that case is to box it. there is a "less practical" way, and that is to set \exhyphenpenalty=10000; since this will suppress breaks after all explicit hyphens, it has to be buried in a group along with the string on which it is supposed to act. – barbara beeton May 17 '16 at 15:41
  • thanks. could you please tell me what to put in the preamble? – Valerio May 17 '16 at 16:06
  • 4
    @Valerio -- to handle this in the preamble, you will have to make a command: \newcommand{\KPLUS}{\mbox{K-PLUS}}. it would be better, usually, to deal with it in the main text: if we consider \mbox{K-PLUS} then ... – barbara beeton May 17 '16 at 16:25
  • 1
    thanks! i've put \newcommand{\KPLUS}{\mbox{K-PLUS}\ } otherwise there is no space afterwards – Valerio May 17 '16 at 19:00
  • 1
    @Valerio -- that will always put a space after "K-PLUS", even if it is followed by a period or comma. the xspace package is meant for use in such situations (although it's overkill, and not recommended by some high-profile users); look for information by searching in this site. – barbara beeton May 17 '16 at 19:24
  • @barbarabeeton why if we put in text \mbox{K-PLUS} it can be split across the line, whereas if we put in preamble with command (\newcommand{\KPLUS}{\mbox{K-PLUS}}) it won't be split ? – Ciprian Tomoiagă Feb 25 '18 at 14:43
  • 1
    @CiprianTomoiagă -- no, anything in an \mbox is by definition unsplittable. if you did this: \newcommand{\KPLUS}{K-PLUS} without the box, it would be splittable. the \mbox needs to be applied just at the point where a break at a hyphen is not wanted. – barbara beeton Feb 25 '18 at 14:51
  • @barbarabeeton I think your addendum is misleading, it only applies to xetex and there (it turns out) you can use rather than ' in \hyphenation – David Carlisle Mar 07 '23 at 21:41
  • Okay, if I update it with those modification, will you vet it? – barbara beeton Mar 08 '23 at 01:33
178

Put it in a box:

\mbox{thatshouldnot}
doncherry
  • 54,637
David Carlisle
  • 757,742
  • 1
    If I define \newcommand{\tsn}{\mbox{thatshouldnot}} and write "Testing the \tsn command.", there is no space after thatshouldnot. Why? – Zoltan Csati Apr 15 '19 at 13:29
  • 1
    @ZoltánCsáti number 1 FAQ, the space is used to terminate the command, you can not write the \tsncommand so you need a space to end \tsn so use \tsn\ command to print a space. this is true of every tex command. – David Carlisle Apr 15 '19 at 13:45
  • Thank you. Although it would be nice if I could simply use the mbox-ed command without needing to manually type \ in the document body whenever I want a space. Is there a solution in which I create a command which automatically inserts \ if the command is inside a sentence and does nothing when it is the last word of a sentence (followed by a full stop)? This would make the document body easier to read and type. – Zoltan Csati Apr 16 '19 at 12:22
  • 2
    @ZoltánCsáti I wrote such a package, but... https://tex.stackexchange.com/questions/86565/drawbacks-of-xspace/86620#86620 – David Carlisle Apr 16 '19 at 17:46
  • Try \newcommand{\tsn}{\mbox{thatshouldnot}\xpspace} – vy32 Jul 31 '21 at 02:23
  • Alternatively, use \tsn{}. – Nicolai Weitkemper Jun 20 '23 at 11:59