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!!

3
  • 2
    You have \url for this
    – user156344
    Apr 25, 2019 at 13:59
  • 4
    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. Apr 25, 2019 at 14:03
  • If you replace the # in the URL by \# it works.
    – user121799
    Apr 25, 2019 at 14:03

1 Answer 1

1

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.

3
  • 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, 2019 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
    – user156344
    Apr 25, 2019 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. Apr 26, 2019 at 11:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .