2

I sometimes have long words that are unbreakable and end up sticking out into the right margin. I realize that there's nothing magical to be done about this and the only real solution is to break them or rewrite the text.

However, I'm trying to change the last-resort behavior so it breaks the line before the long word and sets it at its natural spacing, while justifying the rest of the paragraph.

I already found an answer on tex.sx that defines a command on a per-word basis, but this doesn't change the default behavior. Also, the \lword command from the linked answer causes the preceding line to be stretched slightly and inserts line breaks where they're not strictly necessary. I've attempted to fix these issues in my \Lword command.

Ideally, I'd also like the [draft] option to highlight these emergency line breaks.

Here are some examples of what I've tried so far:

output

\documentclass[draft]{article}
\usepackage{showframe}
\usepackage{ragged2e}
\usepackage{color}

% https://tex.stackexchange.com/a/62347/99343
\newcommand*\lword[1]{%
    \leavevmode
    \nobreak
    \hskip0pt plus\linewidth
    \penalty50
    \hskip0pt plus-\linewidth
    \nobreak
    \textcolor{red}{#1}%
}

% same as above, but with higher penalty and infinite stetch
\newcommand*\Lword[1]{%
    \leavevmode
    \nobreak
    \hskip0pt plus 1fil
    \penalty9999
    \hskip0pt plus -1fil
    \nobreak
    \textcolor{blue}{#1}%
}

\newcommand*\test[4]{{%
    #2%
    \strut\llap{\smash{\begin{minipage}[t]{8em}\RaggedLeft #1\end{minipage} }}%
    Aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa
    #3{bbbbbbbbbbbbbbbbbbbbbbbbbbbbb}
    aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa
    aaaa aaaa aaaa aaaa aaaa aaaa aaaaa
    #4{bbbbbbbbbbbbbbbbbbbbbbbbbbbbb}
    aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa.
    \par
}}

\setlength\parindent{0pt}
\setlength\parskip{1ex}

\begin{document}
    \test{default rendering}                           {}{}{}
    \test{manually inserted linebreak (desired result)}{}{\\}{}
    \test{lword macro}                                 {}{\lword}{\lword}
    \test{Lword macro (desired result)}                {}{\Lword}{\Lword}
    \test{sloppy}                                      {\sloppy}{}{}
    \test{emergencystretch}                            {\emergencystretch10em}{}{}
    \test{RaggedRight}                                 {\RaggedRight}{}{}
    \test{sloppy and RaggedRight}                      {\sloppy\RaggedRight}{}{}
\end{document}

Here's my actual question:

  • Is it possible to get this line breaking behavior without wrapping potential problem words in an extra command, and if so, how?
  • Failing that, do my changes to \lword have any possible side effects?
  • 1
    I am curious, which language has bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb? At least in english, this is impossile. – Johannes_B Mar 14 '16 at 11:28
  • These are of course just placeholders. Imagine it to be an URI or some long CamelCase identifier that can't or shouldn't be hyphenated. – wrtlprnft Mar 14 '16 at 11:30
  • 2
    your Lword looks OK, and It's probably best to explicitly wrap each such word in the command (usually url and identifiers are already wrapped in some markup so it should not require much additional markup in the document) trying to do it automatically is bound to be fragile and break something. – David Carlisle Mar 14 '16 at 11:48
  • check this out tex.stackexchange.com/a/241355/90297 – Elad Den Mar 14 '16 at 12:14
  • 1
    you can not (easily) add glue-penalty-negativeglue combination mid url as it just uses the math \binopenalty to allow line breaking, but \Lword{\url{...}} should allow breaking anywhere within the url or breaking short and moving the whole url over. – David Carlisle Mar 14 '16 at 12:42
1

In real life cases you'd just put \usepackage{microtype} into your preamble, which would help to solve most of your trouble.

  • I already do, but I still sometimes get them (URLs in bibliographies are especially annoying, because there's little one can do about an individual entry). Microtype definitely helps a lot in long paragraphs, though. – wrtlprnft Mar 17 '16 at 18:48

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.