使 url 包自定义断点设置仅适用于特定 url

使 url 包自定义断点设置仅适用于特定 url

我有一个巨大的 URL,主要由字母组成。我发现Marco 对 URL 破坏问题的回答并使用

\g@addto@macro{\UrlBreaks}{\UrlOrds}

\g@addto@macro{\UrlBreaks}{%
\do\/\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}

序言中的允许我的 URL 在一个字母处断开。但是,现在所有 URL 都以一个字母为单位断开。所以,问题是,我是否可以选择性地为某些 URL 启用它?

我尝试在有问题的 URL 后设置默认值\def\UrlBreaks,但没有效果。

MWE 如下。我怎样才能选择性地拆分第一个 URL,而不拆分第二个 URL?当前版本会拆分两者。

\documentclass[12pt]{article}
\usepackage[hyphens]{url}
\usepackage[T1]{fontenc}
\usepackage{fouriernc}
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlBreaks}{%
  \do\/\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}
\makeatother
\begin{document}

This is a \url{http://reallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongurl} which
\LaTeX\ will be unhappy about. So, how can we split this in the best way?

Here is another one which I don't want to split,
\url{http://notquitesolongastheotheroneurl}.

\end{document}

答案1

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage[hyphens]{url}
\usepackage[T1]{fontenc}
\usepackage{fouriernc}

\begin{document}

This is a 
{\def\UrlBreaks{\do\y}%
\url{http://reallyreallyreallyreallyreallyreallyreallyreallyreallyreallylongurl}}
 which
\LaTeX\ will be unhappy about. So, how can we split this in the best way?

Here is another one which I don't want to split,
\url{http://notquitesolongastheotheroneurl}.

\end{document}

答案2

您可以定义一个\longurl宏来在本地\UrlBreaks执行此操作。例如:

\newcommand{\longurl}[1]{%
    {\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks\UrlOrds%
        \do\/\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}%
    \url{#1}}%
}

相关内容