0

I am trying do define a macro that automatically links to a paragraph in the Java Language Specifications. But for some reason, it is not working.

I want to call the command with a paragraph number, e.g. \pg{6.6}, and it should display as §6.6 which links to the correct JLS entry (in this case this link http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6).

This is what I have so far:

\newcommand{\pg}[1]{\href{http://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html\# jls-{#1}}{§#1}}

But this code causes a problem with the link. It is not working correctly. When I hover over the text, it indicates a link, but clicking won't do anything.

Another problem is that this command would only work for paragraphs in chapter 6, because of the chapter declaration in the link. Is there a way around this?

EDIT: This my current solution which is based on Davids Solution and uses the xstring package:

\newcommand{\pg}[1]{%
    \StrLeft{#1}{1}[\firstletter]%
    \href{http://docs.oracle.com/javase/specs/jls/se8/html/jls-\firstletter.html\#jls-#1}{§#1}%
}

It has the advantage that it works with paragraphs like 5.3.1.4 and does not need more than one argument. I can just use it like this: \pg{5.3.1.4}

  • you were nearly there but you put a space and a spurious {} into the URL your URL ended html# jls-{6.1} not html#jls-6.1 – David Carlisle Mar 14 '17 at 14:18
  • For me using evince and firefox the link works. – Skillmon likes topanswers.xyz Mar 14 '17 at 14:19
1

enter image description here

\documentclass{article}
\usepackage{hyperref}

\newcommand{\pg}[2]{%
\href{http://docs.oracle.com/javase/specs/jls/se8/html/jls-#1.html\#jls-#1.#2}%
{\textsection#1.#2}}
\begin{document}


zzz\pg{6}{1}

\end{document}
|improve this answer|||||

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.