Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Supports custom style hyperlink commands #350

Closed
Mikachu2333 opened this issue Aug 2, 2024 · 5 comments
Closed

[Feature] Supports custom style hyperlink commands #350

Mikachu2333 opened this issue Aug 2, 2024 · 5 comments

Comments

@Mikachu2333
Copy link

Background:

I want the hyperref package to underline my links when using \href{url}{text} and \hyperref[ref]{text}, but not add any extra styling when using \footnote{} or \footnotemark as the following shows.

This is an extremely simple implementation without any additional special character handling, and should only be used for testing.

% !TeX encoding = UTF-8
% !TEX TS-program = lualatex
\documentclass[a4paper]{report}
\usepackage[hidelinks]{hyperref}

\newcommand{\newhref}[2]{%
    \hypersetup{pdfborderstyle={/S/U/W 1},allbordercolors={0 0 0}}%
    \href{#1}{#2}%
    \hypersetup{pdfborderstyle={/S/U/W 0}}%
}
\newcommand{\newref}[2]{%
    \hypersetup{pdfborderstyle={/S/U/W 1},allbordercolors={0 0 0}}%
    \hyperref[#1]{#2}%
    \hypersetup{pdfborderstyle={/S/U/W 0}}%
}

\begin{document}
footnote\footnotemark and \hyperref[aaa]{ref} and \href{https://baidu.com}{url}.
\footnotetext{default}

footnote\footnotemark and \newref{aaa}{ref} and \newhref{https://baidu.com}{url}.
\footnotetext{just url and ref have the underline}

footnote\footnotemark and \hyperref[aaa]{ref} and \href{https://baidu.com}{url}.
\footnotetext{re-test default}

{
    \label{aaa}
    a par for testing ref
}

\end{document}

What I want

Something like
\hyperref[pdfborderstyle={/S/U/W 1},allbordercolors={0 0 0}]{ref}{text}
and
\href[pdfborderstyle={/S/U/W 1},allbordercolors={0 0 0}]{url}{text}

OR just
\usepackage[hidelink={footnote},pdfborderstyle={/S/U/W 1},allbordercolors={0 0 0}]{hyperref}

Anything else

Thanks to @muzimuzhi and @u-fischer here #195 (comment) , but that issues would change the footnote ref to be underlined both (with the command \AddToHook{pdfannot/link/GoTo/before}{\hypersetup{urlbordercolor=black,pdfborderstyle={/S/U/W 1}}} if I'd want to underline the /ref separately)...

@Mikachu2333
Copy link
Author

And, the most frustrating thing is that if I define my own commands, I also need to reprocess special characters to avoid warnings and errors, which is exactly the job that this package has perfectly completed. Reinventing the wheel really doesn't make much sense...

@u-fischer
Copy link
Member

you are mixing two link types. Special chars are only relevant if you handle urls.

If you load the pdfmanagement and its driver it is easy to change their border color and styles:

\DocumentMetadata{}
\documentclass{report}
\usepackage{hyperref}
\hypersetup{urlborderstyle={/S/U/W 3},urlbordercolor={red}}

\begin{document}
\href{https://baidu.com}{url}.
\hrefurl{https://baidu.com}{url}.
\end{document}

image

For internal links special characters are not relevant, and you can easily define your own command that accepts keys to change whatever you want.

It is not trivial in the standard definitions to change the link style only of footnotes, but with the new footnote code in latex-lab you can change its type and then use a hook to adapt the links:

\DocumentMetadata{testphase=new-or} %load new footnote code
\documentclass{article}
\usepackage{hyperref}
\hypersetup{linkborderstyle={/S/U/W 3},linkbordercolor={green}}

\ExplSyntaxOn
\tl_set:Nn \l_fnote_link_type_tl {footnote}
\ExplSyntaxOff  

\ActivateGenericHook{hyp/link/footnote}
\AddToHook{hyp/link/footnote}
  {\hypersetup{hidelinks}}

\begin{document}
\section{section}\label{test}

\ref{test} abc\footnote{blblbl}
\ref{test}

\end{document}

image

@Mikachu2333
Copy link
Author

Thanks for answering! But it seems that the package does not seem to be compatible with the tabularray package as the following pic shows. There are obvious strange jumps when footnote numbering.

20240803_10-57-42
\DocumentMetadata{testphase=new-or,lang=en-US} %load new footnote code
\documentclass{article}
\usepackage{hyperref}
\usepackage{tabularray}
\hypersetup{linkborderstyle={/S/U/W 1},linkbordercolor={black}}

\ExplSyntaxOn
\tl_set:Nn \l_fnote_link_type_tl {footnote}
\ExplSyntaxOff  

\ActivateGenericHook{hyp/link/footnote}
\AddToHook{hyp/link/footnote}{%
\hypersetup{hidelinks}%
}

\begin{document}
\tableofcontents

\section{foo}
\begin{tblr}[
        long, %same as \begin{longtblr}
    ]{
        cells={c,m},
        note{1}={Footnote for tblr.},
        vlines,
        hlines
    }
    This is a tblr footnote\TblrNote{1}  & Tabular footnote (left) \\
    This is common footnote\footnotemark & Common footnote (left)
\end{tblr}
\footnotetext{Footnote for test.}
\end{document}

Besides, the command you gave seems to incorrectly add underline to the content when generating it, is there anyway to improve this?

@muzimuzhi
Copy link
Contributor

@Mikachu2333 You need \UseTblrLibrary{counter}, see lvjr/tabularray#89.

@Mikachu2333
Copy link
Author

@Mikachu2333 You need \UseTblrLibrary{counter}, see lvjr/tabularray#89.

Thank you, under your guidance, I found a way to fix it.


And, I finally go back to the custom command as the following code shows for its convenience...

These code would only underline the \href{url}{text} and \hyperref[label]{text} if the user use the command \uhref{}{} or \uref{}{} and will not change the style of content which is generated automatically.

\usepackage{xcolor}% or use color package for color defs
\usepackage[allbordercolors=black, hidelinks]{hyperref}
\newcommand{\uhref}[2]{%
\hypersetup{pdfborderstyle={/S/U/W 1}}%
\href{#1}{#2}%
\hypersetup{pdfborderstyle={/S 0}, hidelinks}%
}
\newcommand{\uref}[2]{%
\hypersetup{pdfborderstyle={/S/U/W 1}}%
\hyperref[#1]{#2}%
\hypersetup{pdfborderstyle={/S 0}, hidelinks}%
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants