2

I prefer to wrap lines in my LaTeX source, and my editor will often choose a hyphen as a good place to split the line. Unfortunately, if a hyphenated word doesn't appear in the output file at a line break, then LaTeX will insert a space immediately after the hyphen.

I'm looking for a way to train my editor not to break there. Alternatively, is there a way I can tell LaTeX not to insert a space at a line break if the line ends in a hyphen?

  • Train? hmmmmmmmm interesting :-) – azetina Nov 15 '13 at 19:11
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Nov 15 '13 at 19:30
  • 1
    have you tried with a percent sign % after the hyphen as in text-%[line break in source]text[on next line]? – user4686 Nov 16 '13 at 9:26
  • 1
    Change editor; if it breaks lines at hyphens it has no use for anything. – egreg Nov 16 '13 at 10:48
  • @jfbu a % at the end of the line produces the exact behavior I'm looking for, and seems safe enough for a find-all / replace. Ideally, I'd like to find a "reflow safe" solution (akin to the solution from karlkoeller below) but this is great for now. – user41103 Nov 18 '13 at 16:53
2

Try this code

\documentclass{article}

\def\myhyphen{-}
\catcode`-=\active
\def-{\myhyphen\ignorespaces}

\begin{document}
text text text text text text text text text text text text text-
text text text text text text text text text text text text text
\end{document} 

Output:

enter image description here

  • 1
    this works "too well". if your text includes the phrase "sub- and superscripts", the "necessary" space would be gobbled. the % approach is (unfortunately) more reliable. – barbara beeton Nov 16 '13 at 18:47
  • @barbarabeeton I know... But I don't know how to check whether the hyphen is last character in the line. – karlkoeller Nov 16 '13 at 19:08

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.