我正在努力对长 URL 进行连字符连接(使用命令\url{}
)。
当 URL 只是行的一部分,无法适应文本区域宽度时,换行功能可以正常工作。这意味着,如果行以 URL 之前的某些文本开头,或者带连字符的 URL 后面跟着其他带连字符的文本,则 URL 被正确连字符连接。
问题出在 URL 的某些部分,这些部分单独位于
很长的 URL 的第一行
很长的带连字符的 URL 的中间几行
从屏幕截图可以看出,两种情况都溢出了文本区域 - 情况 1 清晰可见,但情况 2 也溢出了几个像素。
在实际文档中,溢出区域根据内容而变化,有时会与右侧的对象(图像等)发生冲突。
如何正确地连字符 URL 以避免此类溢出?
理想的解决方案是完美对齐的块
但也可以接受的解决方案是使用下溢行而不是溢出行(较早的换行而不是较晚的换行)
我猜问题主要是由非比例字体引起的,如果同一行存在其他比例文本,则问题得到解决,但是如何处理长 URL 的单独部分?
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[unicode]{hyperref}
\def\UrlBreaks{\do{a}} % Force correct hyphenation for this example
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
短 MWE
\documentclass[a4paper]{book}
\usepackage{hyperref}
\def\UrlBreaks{\do{a}}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
编辑 18-12-23:附加问题
我尝试过建议的解决方案:
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100}}}%
\do\a
一开始一切都很完美。但我很长时间都没有感到高兴 :-) 在我用真实 URL 替换虚拟 URL 后,文档甚至无法编译 - 数字太大错误,请参阅屏幕截图中的详细信息。
问题只出现在“建议的解决方案”和“真实 URL”的组合上。旧方法和真实 URL 是可以的(只有错误的中断,这就是这个问题的原因),建议的解决方案和虚拟 URL 也可以正常工作。这肯定与 URL 中的特殊字符有关。
完整代码:
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[unicode]{hyperref}
% Old method - no error, but issues with breaks
%\def\UrlBreaks{\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z}
% New method - error for real URL (previous example with "aaa" works)
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100}}}
\do\-\do\/\do\0\do\1\do\2\do\3\do\4\do\5\do\6\do\7\do\8\do\9\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X\do\Y\do\Z
\usepackage{pdfpages}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
%\url{http://aaa.bbb.ccc/njrrjjriokf56opiyfl5ojiorij6m9h9r87vjvjytrjyiotryhjiothjvoi8jr8o7j8hohj8oj88jt6hj8ohjot6trhiothj8hj8ohitnhh8tr7t786ht76nvh6gtrhgoght6onbvgnhyg8o7hvntnvotr696yjvhopr76r}\newline
\url{http://www.portafontium.cz/iipimage/30063568/karlovy-vary-65\_0240-t?x=-45\&y=345\&w=927\&h=383}\newline
\end{document}
答案1
您可以使用该xurl
包:
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{showframe}
\renewcommand{\ShowFrameLinethickness}{0.3pt}
\usepackage{ulem}
\usepackage{xurl}
\usepackage[unicode]{hyperref}
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://abc.bbb.ccc/abcabcaaaaaaabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabca} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
答案2
xurl
只需在 package 之前添加 package 的调用即可hyperref
。
以下 MWE
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[hyphens]{xurl} % <==========================================
\usepackage[unicode]{hyperref}
\usepackage{pdfpages}
\usepackage{needspace}
\begin{document}
Reference text. Reference text. Reference text. Reference text.
Reference text. Reference text. Reference text. Reference text.
Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
给你:
我会将此错误告知维护者xurl
。同时,您可以使用以下(不完美,但总比没有好……)代码(我在\def
:中更正了代码\def\UrlBreaks{\do\a}
):
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{pdfpages}
\usepackage{needspace}
\usepackage{ulem}
\usepackage{showframe}
%\usepackage{xurl} % <==========================================
\usepackage[unicode]{hyperref}
\def\UrlBreaks{\do\a}
\begin{document}
Reference text. Reference text. Reference text. Reference text.
Reference text. Reference text. Reference text. Reference text.
Reference text. Reference text.
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}
结果如下:
答案3
url 没有连字符。它们被破坏了。由于它们实际上是在数学中设置的,所以这些部分是宽度相当固定的块,这有时很难填满一行。您可以在 a 后添加一点可拉伸性以避免出现此问题:
\documentclass[a4paper]{book}
\usepackage{fontspec}
\usepackage{polyglossia}
\setmainlanguage{czech}
\usepackage{ulem}
\usepackage[unicode]{hyperref}
%\def\UrlBreaks{\do{a}} %
% some stretchability:
\def\do#1{\appto\UrlSpecials{\do#1{\mathchar`#1 \mskip 0mu plus 1mu\penalty100\relax}}}%
\do\a
\begin{document}
Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text. Reference text.\newline
\url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa}\newline
Some text before. \url{http://aaa.bbb.ccc/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa} Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after. Some text after.
\end{document}