0

I found a nice solution to edit the look of hyperlinks, but I have another problem:

\documentclass{article}

\usepackage{hyperref}
\newcommand{\link}[1]{\href{#1}{#1}}

\begin{document}

% error: Illegal parameter number in definition of \Hy@tempa. ...tes/guides/collections/designfaq.html#a14}
\link{https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a14}
%   \href{https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a14}{Test}

\end{document}

Why does \link{...} not work? Exact the same url does work with \href{...}{...}.

Thank you for your help!!

  • 2
    You have \url for this – The old JouleV Apr 25 at 13:59
  • 3
    You shouldn't put a full url in an argument. hyperref then no longer can do its catcode magic. There is a reason why the "nice" solution got only one upvote, and my answer got accepted. – Ulrike Fischer Apr 25 at 14:03
  • If you replace the # in the URL by \# it works. – marmot Apr 25 at 14:03
2

Why don't use

\documentclass{article}

\usepackage{hyperref}

\begin{document}
\url{https://docs.oracle.com/javase/8/docs/technotes/guides/collections/designfaq.html#a14}
\end{document}

But take care! Shorten your link or use \href with a good description, because such a long link makes the document looks bad.

  • I need a description, so I wanted to use something like \newcommand{\link}[2]{\href{#1}{#2}}, but the \link does not work at all.. But a direct call with the same URL and description works with \href. What's the reason for this? – mrbela Apr 25 at 14:35
  • @mrbela Well, description of tex.stackexchange.com, for example, should be TeX.SE or TeX.SX or TeX StackExchange, but not tex.stackexchange.com itself. It is not a description anymore – The old JouleV Apr 25 at 15:18
  • @mrbela So the syntax of \link would be the same as the syntax of \href. If you insist in having the thing named \link, you can "copy" the\href-macro via \let\link=\href. By the way: The reason why using the \link-macro does not work out as expected is: Both \url and \href change the category-code-régime (e.g., the catcode of #) before LaTeX reads their arguments from the .tex-input-file and tokenizes them. This change of category-code-régime is outmaneuvered by having LaTeX read and tokenize the arguments for the \link-macro under unchanged category-code régime. – Ulrich Diez Apr 26 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.