对齐没有空格但有很多连字符的长行

对齐没有空格但有很多连字符的长行
% !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}
\addtolength{\trimedge}{-\paperwidth}
\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}

在此处输入图片描述

如您所见,由于行数过长且包含大量连字符,导致两行对齐失败。有什么方法可以解决这个问题吗?

答案1

您可能需要使用“可拉伸”连字符;尺寸是通过反复试验计算出来的。

\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}
\addtolength{\trimedge}{-\paperwidth}
\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\kern.01em\nobreak\leaders\hrule\fakehyphenspec plus .1em minus .05em
    \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}

在此处输入图片描述

答案2

一种可能的方法是“作弊”,用一个特殊的宏替换相应短语中的连字符,该宏将在前后插入连字符和一些胶水(可拉伸空间),例如

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

此外,您可能希望将有问题的段落放入一个组中,并\hyphenpenalty=10000在该组内进行设置。

% !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}
\addtolength{\trimedge}{-\paperwidth}
\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}

在此处输入图片描述

答案3

替代解决方案是,我们使用一个小环,以便插入胶水。

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

该宏\@tfor来自 LaTeX 内核。我们所做的就是在连字符前后添加一些胶水。您可以在下图中看到它的工作原理,该图是对上述代码进行了小幅修改后获得的。

在此处输入图片描述

\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}
\addtolength{\trimedge}{-\paperwidth}
\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}

答案4

如果您愿意在 LuaLaTeX 而不是 XeLaTeX 下编译此文档,这似乎是该microtype软件包的一个很好的用途。请注意,在本例中,我已将软件包的“突出”设置设置为 false;通常,我会将其设置为“true”(默认值)。

我还会 (i) 在后面放置一个\(反斜杠)来Mr.告诉 TeX in.不是Mr.句末句号(句号)以及 (ii) 在“who”和“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}
\addtolength{\trimedge}{-\paperwidth}
\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}

相关内容