# How to hyperref prefixed URLs?

Is it possible to define URL prefixes and correctly provide hyperrefs for them?

For example:

\defineprefix{dbo}{http://dbpedia.org/ontology/}
\prefixurl{dbo:City}


Should have the same effect as:

\href{http://dbpedia.org/ontology/City}{dbo:City}


Edit: This is the MWE of the accepted code where it causes an error:

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\newcommand\defineprefix[2]{%
\def\urlprefixabbrv@#1{#2}}
\newcommand\prefixurl[2]{%
\href{\urlprefixabbrv@#1/#2}{#1:#2}}
\makeatother

\begin{document}
\defineprefix{owl}{http://www.w3.org/2002/07/owl\#}
\defineprefix{xsd}{http://www.w3.org/2001/XMLSchema\#}

\prefixurl{owl}{Class}
\prefixurl{xsd}{string}

\end{document}

• Is your syntax for \prefixurl flexible? It would be somewhat easier to code if you allow the syntax \prefixurl{dbo}{City}. – Willie Wong Oct 22 '15 at 14:47
• Yes the syntax is flexible, the second one would equally useful. – Konrad Höffner Oct 22 '15 at 14:49

The following should work

\documentclass{article}
\usepackage{hyperref}

\makeatletter
\newcommand\defineprefix[2]{%
\expandafter\def\csname urlprefixabbrv@#1\endcsname{#2}}
\newcommand\prefixurl[2]{%
\href{\csname urlprefixabbrv@#1\endcsname/#2}{#1:#2}}
\makeatother

\begin{document}
\defineprefix{dbo}{http://dbpedia.org/ontology/}
\prefixurl{dbo}{City}

\end{document}


You are responsible yourself for keeping track you don't define the same prefix twice and overwrite each other.

• Thank you very much, this will be very useful for Semantic Web papers! – Konrad Höffner Oct 22 '15 at 14:57
• Unfortunately this only works with one defined prefix, how can I modify this so that I can define multiple prefixes? The error message is: "! Use of \urlprefixabbrv@ doesn't match its definition." – Konrad Höffner Dec 10 '15 at 11:48
• @KonradHöffner: it works for me. Can you past your code? The command \urlprefixabbrv@ should never be called by itself: the defined command should be \urlprefixabbrv@string where string is the class of prefix (in the example above, the command defined is \urlprefixabbrv@dbo). If you can construct a minimum non-working example, please edit it into the question and ping me again, and I'll see if I can debug. Right now I cannot reproduce your error so I don't know what it is that went wrong. – Willie Wong Dec 10 '15 at 14:30
• I added an MWE of the problem to the question. – Konrad Höffner Dec 11 '15 at 12:31
• @KonradHöffner: Ah, I sort-of see the problem. Try the version I am about to put up in 5 minutes. Some veterans however may come by and chide me for my poor code form. – Willie Wong Dec 11 '15 at 17:36