# Justify long line with no spaces but a lot of hyphens

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8 Unicode

\documentclass[11pt]{memoir}
\usepackage{fontspec}
\usepackage{showframe}

%%% Book Layout %%%
\setstocksize{4in}{5.75in}
\settrimmedsize{4in}{5.75in}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{3in}{4.52in}{*}
\setulmargins{0.5in}{*}{*}
\setlrmargins{0.6in}{*}{*}
\setmarginnotes{0pt}{0pt}{0pt}
\checkandfixthelayout

%%% Other Settings %%%
\sloppy

%%% Document Proper %%%
\begin{document}

“It’s not at all clear,” Roger Mexico’s been saying, “what they have in
mind, not at all, the Witchcraft Act’s more than 200 years old, it’s a
relic of an entirely different age, another way of thinking. Suddenly
here we are 1944 being hit with convictions right and left. Our Mr.
Eventyr,” motioning at the medium who s across the room chatting
with young Gavin Trefoil, “could be fallen upon at any moment—pouring
in the windows, hauling dangerous tough Eventyr away to the
Scrubs on pretending-to-exercise-or-use-a-kind-of-conjuration-to-cause-the-spirits-of-deceased-persons-to-be-present-in-fact-at-the-place-where-he-then-was-and-that-those-spirits-were-communicating-with-living-persons-then-and-there-present my God what imbecile Fascist \textit{rot}.~.~.~.”

\end{document}


As you can see, two lines failed to justify because of the unusually long line with lots of hyphens. Is there any way to fix that?

-

You may want to use a "stretchable" hyphen; the dimensions have been computed by trial and error.

\documentclass[11pt]{memoir}
\usepackage{fontspec}

\usepackage[pass,showframe]{geometry} % better than showframe

%%% Book Layout %%%
\setstocksize{4in}{5.75in}
\settrimmedsize{4in}{5.75in}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{3in}{4.52in}{*}
\setulmargins{0.5in}{*}{*}
\setlrmargins{0.6in}{*}{*}
\setmarginnotes{0pt}{0pt}{0pt}
\checkandfixthelayout

% The hyphen is .567ex high, 0.134ex thick, .332em wide
\newcommand{\fakehyphenspec}{height .567ex depth -.433ex\hskip.332em }
\newcommand{\longword}{%
\begingroup
\begingroup\lccode~=-
\lowercase{\endgroup\def~}{%
\nobreak\kern0.01em\penalty\exhyphenpenalty}%
\catcode-=\active
\hyphenpenalty10000
\dolongword}
\newcommand{\dolongword}[1]{#1\endgroup}

%%% Document Proper %%%
\begin{document}

“It’s not at all clear,” Roger Mexico’s been saying, “what they have in
mind, not at all, the Witchcraft Act’s more than 200 years old, it’s a
relic of an entirely different age, another way of thinking. Suddenly
here we are 1944 being hit with convictions right and left. Our
Mr.~Eventyr,” motioning at the medium who s across the room chatting
with young Gavin Trefoil, “could be fallen upon at any moment—pouring
in the windows, hauling dangerous tough Eventyr away to the Scrubs on
\longword{pretending-to-exercise-or-use-a-kind-of-conjuration-%
to-cause-the-spirits-of-deceased-persons-to-be-present-in-fact-%
at-the-place-where-he-then-was-and-that-those-spirits-were-communicating-%
with-living-persons-then-and-there-present}
my God what imbecile Fascist \textit{rot}\dots”

\end{document}


-
puzzled by this depth -.433ex. –  Yiannis Lazarides Mar 29 '13 at 11:36
That looks great. But I may have to adjust the dimensions if I use a different font, right? –  zzjing Mar 29 '13 at 16:34
@zzjing Yes, I'm afraid. –  egreg Mar 29 '13 at 16:38
@Yiannis Lazarides: Maybe it should be 0.443ex? Since 0.567-0.443=0.124, which is the given thickness. –  zzjing Mar 29 '13 at 16:38
@YiannisLazarides Oh, I've written 0.124 where it should have been 0.134. The negative depth makes the rule appear higher over the baseline (it cancels something from the height). –  egreg Mar 29 '13 at 16:48

A possible way is to "cheat" and replace the hyphens in the respective phrase with a special macro that will insert a hyphen plus some glue (stretchable space) before and after, e.g.

\newcommand*{\sh}{\nobreak\hspace{0pt plus 1pt}-\hspace{0pt plus 1pt}}


In addition, you may want to put the offending paragraph into a group and set \hyphenpenalty=10000 within the group.

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8 Unicode

\documentclass[11pt]{memoir}
\usepackage{fontspec}
\usepackage{showframe}

%%% Book Layout %%%
\setstocksize{4in}{5.75in}
\settrimmedsize{4in}{5.75in}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{3in}{4.52in}{*}
\setulmargins{0.5in}{*}{*}
\setlrmargins{0.6in}{*}{*}
\setmarginnotes{0pt}{0pt}{0pt}
\checkandfixthelayout

%%% Other Settings %%%
\sloppy

\newcommand*{\sh}{\nobreak\hspace{0pt plus 1pt}-\hspace{0pt plus 1pt}}

%%% Document Proper %%%
\begin{document}

\begingroup

\hyphenpenalty=10000% optional

“It’s not at all clear,” Roger Mexico’s been saying, “what they have in
mind, not at all, the Witchcraft Act’s more than 200 years old, it’s a
relic of an entirely different age, another way of thinking. Suddenly
here we are 1944 being hit with convictions right and left. Our Mr.
Eventyr,” motioning at the medium who s across the room chatting
with young Gavin Trefoil, “could be fallen upon at any moment—pouring
in the windows, hauling dangerous tough Eventyr away to the
Scrubs on pretending\sh to\sh exercise\sh or\sh use\sh a\sh kind\sh of\sh conjuration\sh to\sh cause\sh the\sh spirits\sh of\sh deceased\sh persons\sh to\sh be\sh present\sh in\sh fact\sh at\sh the\sh place\sh where\sh he\sh then\sh was\sh and\sh that\sh those\sh spirits\sh were\sh communicating\sh with\sh living\sh persons\sh then\sh and\sh there\sh present my God what imbecile Fascist \textit{rot}.~.~.~.”

\endgroup

\end{document}


-
This is the first thing I tried myself, but couldn't make it work right. egreg's solution looks better, but this would work in a pinch. Thanks. –  zzjing Mar 29 '13 at 16:56
@zzjing You're welcome - and don't forget that while you can accept only one answer per qeestion, you can upvote as many as you like. ;-) –  lockstep Mar 29 '13 at 16:58

Alternative solution, we use a small loop, to allow glue to be inserted.

\def\breakhyphen#1{%
\@tfor\inext:=#1\do{%
\if\inext-%
\hskip0pt plus 1pt minus1pt
\inext\hskip0pt plus 1pt minus1pt
\else\inext\fi%
}%
}


The macro \@tfor is from the LaTeX kernel. What we do is add a bit of glue, before and after the hyphen. You can see it working in the image below, which was obtained with a small modification to the above code.

\documentclass[11pt]{memoir}
\usepackage{fontspec}
\usepackage{showframe}
\usepackage{microtype}

%%% Book Layout %%%
\setstocksize{4in}{5.75in}
\settrimmedsize{4in}{5.75in}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{3in}{4.52in}{*}
\setulmargins{0.5in}{*}{*}
\setlrmargins{0.6in}{*}{*}
\setmarginnotes{0pt}{0pt}{0pt}
\checkandfixthelayout

%%% Other Settings %%%
\sloppy

%%% Document Proper %%%

\makeatletter
\def\breakhyphen#1{%
\@tfor\i:=#1\do{%
\if\i-%
\hskip0pt plus 1pt minus1pt
\i\hskip0pt plus 1pt minus1pt
\else\i\fi%
}%
}

\makeatother
\begin{document}

“It’s not at all clear,” Roger Mexico’s been saying, “what they have in
mind, not at all, the Witchcraft Act’s more than 200 years old, it’s a
relic of an entirely different age, another way of thinking. Suddenly
here we are 1944 being hit with convictions right and left. Our Mr.
Eventyr,” motioning at the medium who s across the room chatting
with young Gavin Trefoil, “could be fallen upon at any moment—pouring
in the windows, hauling dangerous tough Eventyr away to the
Scrubs on \breakhyphen{pretending-to-exercise-or-use-a-kind-of-conjuration-to-cause-the-spirits-of-deceased-persons-to-be-present-in-fact-at-the-place-where-he-then-was-and-that-those-spirits-were-communicating-with-living-persons-then-and-there-present}

\end{document}

-
This puts a hyphen at the beginning of a line, which is probably not intended. –  zzjing Mar 29 '13 at 16:19
@zzjing It just wraps this long word rather. I think it can get confusing otherwise. –  Yiannis Lazarides Mar 29 '13 at 16:41

If you're willing to compile this document under LuaLaTeX instead of XeLaTeX, this would seem like a good use of the microtype package. Note that I've set the package's "protrusion" setting to false for this example; ordinarily, I'd set it to 'true' (the default).

I'd also (i) put a \ (backslash) after Mr. to tell TeX that the . in Mr. isn't a sentence-ending period (full stop) and (ii) insert an apostrophe between "who" and "s".

% !TEX TS-program = lualatex
\documentclass[11pt]{memoir}
\usepackage{fontspec}
\usepackage[pass,showframe]{geometry} % borrowed from egreg's solution

\usepackage[stretch=40,protrusion=false]{microtype}
% default value of 'stretch' parameter: 20
% I've set 'protrusion' to false just for this example;
%   ordinarily, I'd set it to 'true' (the default)

%%% Book Layout %%%
\setstocksize{4in}{5.75in}
\settrimmedsize{4in}{5.75in}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{3in}{4.52in}{*}
\setulmargins{0.5in}{*}{*}
\setlrmargins{0.6in}{*}{*}
\setmarginnotes{0pt}{0pt}{0pt}
\checkandfixthelayout

%%% Other Settings %%%
\sloppy

%%% Document Proper %%%
\begin{document}

“It’s not at all clear,” Roger Mexico’s been saying, “what they have in
mind, not at all, the Witchcraft Act’s more than 200 years old, it’s a
relic of an entirely different age, another way of thinking. Suddenly
here we are 1944 being hit with convictions right and left. Our Mr.\
Eventyr,” motioning at the medium who’s across the room chatting
with young Gavin Trefoil, “could be fallen upon at any moment—pouring
in the windows, hauling dangerous tough Eventyr away to the
Scrubs on pretending-to-exercise-or-use-a-kind-of-conjuration-to-cause-the-spirits-of-deceased-persons-to-be-present-in-fact-at-the-place-where-he-then-was-and-that-those-spirits-were-communicating-with-living-persons-then-and-there-present my God what imbecile Fascist \textit{rot}.~.~.~.”

\end{document}
`
-
Nice solution. Unfortunately, I am using XeLaTeX for its font features (not shown here), so microtype is out. Pity. Good catch on the . and missing apostrophe. Thanks. –  zzjing Mar 29 '13 at 16:11
@zzjing - Indeed, it's a pity that microtype's expansion feature doesn't work under Xe(La)TeX. –  Mico Mar 29 '13 at 19:40