This is a follow-up to Should Hyperlinks include trailing puctuation as part of the link text?, and this question is about automating the processing of the trailing character. I would like to be able to have the punctuation be specified after the macro as in:
\CreateLink{<linke style>}{<link target>}{<link text>},
where I have used a comma as an example. I prefer the above instead of:
\CreateLink{<linke style>}{<link target>}{<link text>,}
for two reasons:
- It is more readable (punctuation is more obvious in the code).
- Allows me to defer the decision of the weather to include the punctuation as part of the link text or not.
So, I would like to modify the \CreateLink
macro to be able to check if the subsequent character is a space or not. If not, then consider the next character as punctuation and include that character in the output of \CreateLink
.
I realize that the solution will probably involve \@ifnextchar
but I was not able able to get that to work. In the attempt below I output a -
if a non-space character is found. Since the -
is in the output for all cases, my attempt is clearly missing something.
References:
Code:
% 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}
\@ifnextchar{\space}...
actually checks for the presence of a macro containing a space (since\space
is a macro containing a space). Replacing by\@ifnextchar\@sptoken
would not work either for a different reason:\@ifnextchar
ignores spaces before comparing what follows with its first argument. Plenty of packages have probably fixed this oversight, but since you are usingexpl3
, I'd suggest going for\peek_catcode:NTF \c_space_token { } { - }
or similar (see documentation ofl3token
).expl3
due to all the wonderful solutions I have obtained from this site. I am still clueless aboutexpl3
and don't think I could easily adapt your suggestion. But will have to tackle it if a solution does not show up when this bounty ends.