这是超链接是否应包含尾随标点作为链接文本的一部分?,这个问题是关于自动处理尾随字符。我希望能够指定标点符号后宏如下:
\CreateLink{<linke style>}{<link target>}{<link text>},
我使用逗号作为示例。我更喜欢上面的代码,而不是:
\CreateLink{<linke style>}{<link target>}{<link text>,}
原因有二:
- 它更具可读性(代码中的标点符号更明显)。
- 允许我推迟天气决定是否将标点符号作为链接文本的一部分。
因此,我想修改\CreateLink
宏,以便能够检查后续字符是否为空格。如果不是,则将下一个字符视为标点符号,并将该字符包含在 的输出中\CreateLink
。
我意识到解决方案可能涉及\@ifnextchar
但我无法让它工作。在下面的尝试中,-
如果发现非空格字符,我会输出。由于-
在所有情况下的输出中都是,所以我的尝试显然缺少了一些东西。
参考:
代码:
% The following \def allows to have one version of this code for this questions,
% and the previous question at:
%
% https://tex.stackexchange.com/questions/125357/should-hyperlinks-include-trailing-puctuation-as-part-of-the-link-text
%
\def\SmartVersion{}% Should be uncommented for this question.
\documentclass{article}
\usepackage{xparse}
\usepackage{xcolor}
\usepackage{soul}
\usepackage{xstring}
\usepackage{xspace}
\usepackage[colorlinks=true]{hyperref}
%% ----------------------------------------------------------------
%% https://tex.stackexchange.com/questions/36894/
%% underline-omitting-the-descenders
%%
%%
\makeatletter
\ExplSyntaxOn
\cs_new:Npn \white_text:n #1
{
\fp_set:Nn \l_tmpa_fp {.01}
\fp_mul:Nn \l_tmpa_fp {#1}
\llap{\textcolor{white}{\the\SOUL@syllable}\hspace{\fp_to_decimal:N \l_tmpa_fp em}}
\llap{\textcolor{white}{\the\SOUL@syllable}\hspace{-\fp_to_decimal:N \l_tmpa_fp em}}
}
\NewDocumentCommand{\whiten}{ m }
{
\int_step_function:nnnN {1}{1}{#1} \white_text:n
}
\ExplSyntaxOff
\NewDocumentCommand{ \varul }{ D<>{5} O{0.2ex} O{0.1ex} +m } {%
\begingroup
\setul{#2}{#3}%
\def\SOUL@uleverysyllable{%
\setbox0=\hbox{\the\SOUL@syllable}%
\ifdim\dp0>\z@
\SOUL@ulunderline{\phantom{\the\SOUL@syllable}}%
\whiten{#1}%
\llap{%
\the\SOUL@syllable
\SOUL@setkern\SOUL@charkern
}%
\else
\SOUL@ulunderline{%
\the\SOUL@syllable
\SOUL@setkern\SOUL@charkern
}%
\fi}%
\ul{#4}%
\endgroup
}%
\makeatother
%% ----------------------------------------------------------------
\newcommand{\CreateLinkA}[2]{%
\hypersetup{pdfborder={0 0 1}, urlcolor=.}%
\href{#1}{#2}%
}%
\newcommand{\CreateLinkB}[2]{%
\hypersetup{pdfborder={0 0 0}, urlcolor=red}%
\href{#1}{#2}%
}%
\newcommand{\CreateLinkC}[2]{%
\hypersetup{colorlinks=false,urlcolor=.}%
\setulcolor{blue}%
\href{#1}{\varul[0.5pt][1.0pt]{#2}}%
}%
\makeatletter
\newcommand{\CreateLink}[3]{%
\IfStrEqCase{#1}{%
{1}{\CreateLinkA{#2}{#3}}%
{2}{\CreateLinkB{#2}{#3}}%
{3}{\CreateLinkC{#2}{#3}}%
}%
\ifdefined\SmartVersion%
% If the next character is anything other than a space,
% include that char as part of the text.
% The "-" is supposed to whatever the next character is,
% but don't know how to get that character.
% Note that the "-" is shown in all cases, so this is NOT working.
\@ifnextchar{\space}{\space}{-}%
\else% "ELSE" clause is unused in this question
\xspace%
\fi
}%
\makeatother
\begin{document}
\textbf{Punctuation NOT part of link:}\medskip
\par Go and check out \CreateLink{1}{www.yahoo.com}{the yahoo}, and also\ldots
\par Go and check out \CreateLink{2}{www.yahoo.com}{the yahoo}, and also\ldots
\par Go and check out \CreateLink{3}{www.yahoo.com}{the yahoo}, and also\ldots
\bigskip
\textbf{Punctuation part of link:}\medskip
\par Go and check out \CreateLink{1}{www.yahoo.com}{the yahoo,} and also\ldots
\par Go and check out \CreateLink{2}{www.yahoo.com}{the yahoo,} and also\ldots
\par Go and check out \CreateLink{3}{www.yahoo.com}{the yahoo,} and also\ldots
\end{document}
答案1
以下是一次尝试。
以下代码仅使用您的第一个链接样式,因为不同的样式对于问题来说似乎并不重要。它使用包etoolbox
它的列表处理工具和\ifblank
宏,以及 Heiko Oberdiek 的ltxcmds
因为它\ltx@ifnextchar
允许通过检查以下空间\ltx@ifnextchar{ }{<true>}{<false>}
。
下面的宏\CreateLink{<address>}{<text>}
检查后面是否有空格。如果没有,则取下一个标记,并检查它是否在使用 设置的标记列表中\setlinkpunctuation{<punctuation marks>}
。此宏可让您完全控制链接中应包含哪些标点符号。
我们得到:
\documentclass{article}
\usepackage{xcolor}
\usepackage{etoolbox,ltxcmds}
\usepackage[colorlinks=true]{hyperref}
\makeatletter
% list parser with no separator:
\DeclareListParser*\@add@punctuation{}
% set the punctuation list (the parser does not like blank lists):
\def\setlinkpunctuation#1{%
\def\@punctuation{}%
\ifblank{#1}
{}
{\@add@punctuation{\listadd\@punctuation}{#1}}%
}
% check if a space follows; if yes, only create the link, else
% eat the next token:
\newcommand*\CreateLink[2]{%
\ltx@ifnextchar{ }
{\@create@link{#1}{#2}{}}
{\@create@link{#1}{#2}}%
}
% check if #3 is among the punctuation marks
% and either place it inside the link or outside:
\def\@create@link#1#2#3{%
\ifinlist{#3}{\@punctuation}
{\@create@actual@link{#1}{#2#3}}
{\@create@actual@link{#1}{#2}#3}%
}
% the actual link:
\def\@create@actual@link#1#2{%
\hypersetup{pdfborder={0 0 1}, urlcolor=.}%
\href{#1}{#2}%
}
\makeatother
\begin{document}
only \texttt{,}\par\medskip
\setlinkpunctuation{,}
Go and check out \CreateLink{www.yahoo.com}{the yahoo}, and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo} and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}. and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}! and also\ldots\par
\bigskip
nothing\par\medskip
\setlinkpunctuation{}
Go and check out \CreateLink{www.yahoo.com}{the yahoo}, and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo} and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}. and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}! and also\ldots\par
\bigskip
\texttt{,.} and \texttt{!}\par\medskip
\setlinkpunctuation{.,!}
Go and check out \CreateLink{www.yahoo.com}{the yahoo}, and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo} and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}. and also\ldots\par
Go and check out \CreateLink{www.yahoo.com}{the yahoo}! and also\ldots\par
\end{document}