0

This question already has an answer here:

I'm trying to create a class that, among other things, generates URLs from parameters passed to an environment. For some weird reason, if the URL string contains a /, hyperref tries to create a URL that points to a file named <URL>.pdf.

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}

\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
  link    .tl_set:N = \l_myenvironment_link_tl,
  print   .bool_set:N = \l_myenvironment_print_bool,
}

\NewDocumentEnvironment { myenvironment } { m }
{
  \keys_set:nn { mydocument / myenvironment } { #1 }
    \minipage{\textwidth}

    \tl_clear_new:N \l_item_url_tl

    \tl_put_left:Nn \l_item_url_tl { https:// }
    \tl_put_right:Nx \l_item_url_tl { \l_myenvironment_link_tl }

    \href{\l_item_url_tl}{TeX.StackExchange} \\

    \url{\l_item_url_tl} \\

}
{
    \endminipage
}
\ExplSyntaxOff

\begin{document}

\begin{myenvironment}
  { 
    link = tex.stackexchange.com,
  }
  Here is some content in my environment with a good link
\end{myenvironment}

\begin{myenvironment}
  { 
    link = tex.stackexchange.com/users/3351/robbie,
  }
  Here is some content in my environment with a \textbf{bad} link
\end{myenvironment}

\end{document}

The above MWE exhibits the behaviour I'm describing: the first instance of the environment has a link to https://tex.stackexchange.com, but the second creates a link to https://tex.stackexchange.com/users/3351/robbie.pdf. Weirdly enough, it doesn't mangle the URL if I use \url, but I can't use that command in my document as the link text is also generated.

Why does it do this, and how do I make it use the raw URL value in the hyperlink target?

marked as duplicate by TeXnician, JouleV, Raaja, Stefan Pinnow, barbara beeton Mar 25 at 18:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    you are aware that you will run into problems with links contains special chars like % or ~? – Ulrike Fischer Mar 25 at 14:45
  • I wasn't—thanks for the heads up—but the links I'm generating contain only alphanumeric characters and forward slashes, and the issue I'm describing only seems to occur when the URL is longer than just a domain name. – Robbie Mar 25 at 14:51
3

hyperref has an heuristic to decide the link type. It gets confused if the colon has catcode letter instead of other in your http:// text.

\documentclass{article}

\usepackage{expl3}
\usepackage{xparse}
\usepackage{verbatim}
\usepackage{hyperref}


\ExplSyntaxOn
\keys_define:nn { mydocument / myenvironment }
{
  link    .tl_set:N = \l_myenvironment_link_tl,
  print   .bool_set:N = \l_myenvironment_print_bool,
}

\NewDocumentEnvironment { myenvironment } { m }
{
  \keys_set:nn { mydocument / myenvironment } { #1 }
    \minipage{\textwidth}

    \tl_clear_new:N \l_item_url_tl    
    \tl_put_left:Nx \l_item_url_tl { http\c_colon_str// }
    \tl_put_right:Nx \l_item_url_tl { \l_myenvironment_link_tl }

    \href{\l_item_url_tl}{TeX.StackExchange} \\

    \url{\l_item_url_tl} \\

}
{
    \endminipage
}
\ExplSyntaxOff

\begin{document}

\begin{myenvironment}
  {
    link = tex.stackexchange.com,
  }
  Here is some content in my environment with a good link
\end{myenvironment}

\begin{myenvironment}
  {
    link = tex.stackexchange.com/users/3351/robbie,
  }
  Here is some content in my environment with a \textbf{bad} link
\end{myenvironment}




\end{document}

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