How to ask hyperref
works ONLY with \url
and \href
commands?
[Tex/LaTex] How to ask hyperref works only with href
hyperreflinks
Related Solutions
You have to absorb the #
as a "normal character"
\makeatletter
\newcommand*{\myhref}{\begingroup\@makeother\#\@myhref}
\newcommand*{\@myhref}[2][]{%
\href{http://myserver/#2}{\ifthenelse{\equal{#1}{}}{#2}{#1}}%
\endgroup}
\makeatother
The package hyperref does a good job with these strange characters, but here the problem is that when you say \myhref{somewhere#anchor}
the #
has already been absorbed by TeX with category code 6, which breaks \href
.
Here we tell LaTeX to open a group in which #
will be considered as a normal character and only after this we absorb the arguments.
Package hyperref
supports only a subset of possible options to turn off "hyperfeatures":
bookmarks=false
: bookmarks/outlinespdfpagelabels=false
: PDF page labelshyperfootnotes=false
: footnoteshyperindex=false
: indexpageanchor=false
: page anchors- ...
Also hyperref
provides environment NoHyper
.
URL links only
A modified version of NoHyper
could be used to disable all, but URI links.
For example:
\documentclass[12pt]{article}
\usepackage[a5paper,landscape]{geometry}% smaller test image for TeX.sx
\usepackage[
bookmarks=false,
pdfpagelabels=false,
hyperfootnotes=false,
hyperindex=false,
pageanchor=false,
colorlinks,
]{hyperref}
\makeatletter
\let\saved@hyper@linkurl\hyper@linkurl
%\let\saved@hyper@linkfile\hyper@linkfile
\let\saved@hyper@link@\hyper@link@
\AtBeginDocument{%
% Since the whole document is affected, only the \begin part of
% environment `NoHyper' is needed.
\NoHyper
\let\hyper@linkurl\saved@hyper@linkurl % needed by \url
%\let\hyper@linkfile\saved@hyper@linkfile % needed by \href{<file>}
\let\hyper@link@\saved@hyper@link@ % needed by \href{<url>}
}
\makeatother
\begin{document}
\tableofcontents
\section{Hello World}
\label{sec:hello}
This is section \ref{sec:hello} on page \pageref{sec:hello}.%
\footnote{This is a footnote}\\
\url{http://tex.stackexchange.com/}\\
\href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}\\
\IfFileExists{t.pdf}{\href{t.pdf}{Local PDF file}}{(No local file.)}\\
\cite{abc} is a good book.
\begin{thebibliography}{9}
\bibitem{abc} A good book.
\end{thebibliography}
\end{document}
Citation links only
The following example tries to enable citation links (not tested with natbib
).
Also anchors need to be enabled to get link targets for the citations.
\documentclass[12pt]{article}
\usepackage[a5paper,landscape]{geometry}% smaller test image for TeX.sx
\usepackage[
bookmarks=false,
pdfpagelabels=false,
hyperfootnotes=false,
hyperindex=false,
pageanchor=false,
colorlinks,
]{hyperref}
\makeatletter
\let\saved@hyper@link@\hyper@link@
\let\saved@hyper@link\hyper@link
% anchors are needed for the citations
\let\saved@hyper@anchorstart\hyper@anchorstart
\let\saved@hyper@anchorend\hyper@anchorend
\def\textstring@cite{cite}
\def\new@hyper@link@[#1]{%
\def\@temp{#1}%
\ifx\@temp\textstring@cite
\expandafter\saved@hyper@link@
\else
\expandafter\disabled@hyper@link@
\fi
[{#1}]%
}
\def\new@hyper@link#1{%
\def\@temp{#1}%
\ifx\@temp\textstring@cite
\expandafter\saved@hyper@link
\else
\expandafter\disabled@hyper@link
\fi
{#1}%
}
\AtBeginDocument{%
\NoHyper
\let\disabled@hyper@link@\hyper@link@
\let\disabled@hyper@link\hyper@link
\let\hyper@link@\new@hyper@link@
\let\hyper@link\new@hyper@link
\let\hyper@anchorstart\saved@hyper@anchorstart
\let\hyper@anchorend\saved@hyper@anchorend
}
\makeatother
\begin{document}
\tableofcontents
\section{Hello World}
\label{sec:hello}
This is section \ref{sec:hello} on page \pageref{sec:hello}.%
\footnote{This is a footnote}\\
\url{http://tex.stackexchange.com/}\\
\href{http://tex.stackexchange.com/}{\TeX\ Stack Exchange}\\
\IfFileExists{t.pdf}{\href{t.pdf}{Local PDF file}}{(No local file.)}\\
\cite{abc} is a good book.
\begin{thebibliography}{9}
\bibitem{abc} A good book.
\end{thebibliography}
\end{document}
Best Answer
\usepackage[implicit=false]{hyperref}