使用 xurl 包 LaTeX 允许在电子邮件中的 @ 处换行

使用 xurl 包 LaTeX 允许在电子邮件中的 @ 处换行

我需要在 LaTeX 中允许电子邮件地址换行。我以前使用软件包来做到这一点url,地址在 @ 处换行,这是可以接受的。

我最近切换到xurl在其他 URL(不是电子邮件地址)中使用换行符,并注意到,一旦我这样做,我的电子邮件地址就会在边缘的任何字符处中断。这是有道理的,因为xurl允许在任何字母数字字符处换行,但我希望 LaTeX 优先在电子邮件地址的 @ 处换行,并且只在必要时在其他地方换行。我查看了本网站上的其他答案和其他在线资源,但我只能找到使用这些软件包允许换行的说明,而找不到有关如何创建在 @ 处换行(url软件包样式)而不是在其他地方换行的首选项(由 启用xurl)的说明。

如果 MWE 有用,我想镜像此输出:

\documentclass[12pt]{article}
\usepackage{url}
\urlstyle{same}
\begin{document}
\begin{minipage}{2in}
\url{[email protected]}
\end{minipage}
\end{document}

但是xurl启用了该包(因为我需要文件中的其他地方使用它来解决不同的问题)。

答案1

根据手册,您可以\useOriginalUrlSetting在组内使用旧设置:

{\useOriginalUrlSetting\url{[email protected]}}

答案2

也许像这样的方法可以达到目的:

\makeatletter
\AtBeginDocument{%
  \@ifpackageloaded{hyperref}{%
                     \newcommand*\@printemailaddressUrlLeftCopy{}%
                     \newcommand\AtIfhyperrefloaded[2]{#1}%
                   }{%
                     \newcommand\AtIfhyperrefloaded[2]{#2}%
                   }%
  \@ifpackageloaded{url}{}{\RequirePackage{url}}%
  \@ifdefinable\printemailaddress{%
    \DeclareRobustCommand\printemailaddress{%
      \leavevmode\begingroup
      \let\do\@makeother \dospecials \catcode`{\@ne  \catcode`}\tw@  \catcode`\ =10  \catcode`\#=6 %
      \@ifstar{%
                \expandafter\expandafter\expandafter\@gobble
                \expandafter\@gobbletwo\@printemailaddress
              }{%
                \AtIfhyperrefloaded{\expandafter\expandafter\expandafter\@firstofone}%
                                   {\expandafter\expandafter\expandafter\@gobble}%
                \expandafter\@gobbletwo\@printemailaddress
              }%
    }%
  }%
  \DeclareUrlCommand\@printemailaddress{%
    {%
      \let\@printemailaddressUrlLeftCopy= \UrlLeft
      \def\UrlLeft##1\UrlRight{\href{mailto:##1}{\@printemailaddressUrlLeftCopy ##1\UrlRight}}%
    }%
    \def\UrlBreaks{\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
                   \do\)\do\,\do\?\do\&\do\'\do+\do\=\do\#\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%
                   \do0\do1\do2\do3\do4\do5\do6\do7\do8\do9}%
    \edef\UrlSpecials{%
      \unexpanded\expandafter{\UrlSpecials
        \do\@{\hbox{\string @}\hfil\penalty\numexpr\UrlBreakPenalty-206\relax\hfilneg}%
        \do\.{\hbox{\string .}\hfil\penalty\numexpr\UrlBreakPenalty-205\relax\hfilneg}%
      }%
    }%
  }%
}%
\makeatother

\documentclass[12pt]{article}

\usepackage{hyperref}
\usepackage{xurl}

\begin{document}

%\urlstyle{same}
\urlstyle{tt}

\twocolumn

\begin{minipage}{2in}
text \url{[email protected]} text 
\end{minipage}

\bigskip

\hrule

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress{sample_username.something.else@nice.weirdlengthy.lengthydomainlengthydomainlengthydomain.com} text
\end{minipage}

\newpage

\begin{minipage}{2in}
text \csname \AtIfhyperrefloaded{nolink}{}url\endcsname{[email protected]} text
\end{minipage}

\bigskip

\hrule

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{[email protected]} text
\end{minipage}

\bigskip

\begin{minipage}{2in}%
text \printemailaddress*{sample_username.something.else@nice.weirdlengthy.lengthydomainlengthydomainlengthydomain.com} text
\end{minipage}

\end{document}

在此处输入图片描述

相关内容