9

I edit a newsletter using LaTeX and I find hyperref very limiting. I'm using hyperref with only one argument: hidelinks. If I click on a \url link in the newsletter the URL opens in the same window, but without the ability to return to the newsletter via the browser's 'back' button. DUH! How can I emulate the HTML target value _blank feature and open the \url in a new window?

doncherry
  • 54,637
BlackMagic
  • 349
  • 1
  • 2
  • 12
  • 1
    This is an issue of the HTML browser, if the "back" button is not working. Try a different browser. The "back" button worked for me with firefox/Linux and its default internal PDF viewer. – Heiko Oberdiek Sep 11 '14 at 21:26
  • The new title is misleading because hyperref doesn't have a feature to make web links open in a new browser window. It can open links to other PDF documents in new browser windows, but not links to http or https addresses. – BlackMagic Sep 21 '14 at 03:31
  • @BlackMagic Feel free to edit the title again! I just felt like it was very unspecific the way it was and we usually prefer titles that give you a good idea of the question just on their own. It might suffice to just change “links” to “link to other PDF files”? (However, there’s no need to make the question title fit the answer. If you didn’t know of the limitation you’re mentioning beforehand (not sure if that was the case), it doesn’t have to be in the title.) – doncherry Sep 23 '14 at 11:06
  • @doncherry The question was basically 'how do I get hyperref to open web links in a new browser window?'. I thought it was pretty unusual behaviour not to be able to do so. I've since found an identical question http://tex.stackexchange.com/q/179729/62165 which I answered as a result of what I discovered from contributions from hyperref's maintainer. Can you select a new title based on what you know now? – BlackMagic Sep 24 '14 at 12:21
  • There seems to be a way to configure pdf.js in Firefox browser: https://tex.stackexchange.com/questions/611518/how-to-open-footnote-url-link-in-new-tab-overleaf#comment1529920_611518 – user202729 May 14 '23 at 10:09

3 Answers3

10

There is an option pdfnewwindow for the command \href. This option does not work with the command \url, but \url{URL} is actually equivalent to \href{URL}{\nolinkurl{URL}}.

This means that you can define a new command like this:

\newcommand{\urlNewWindow}[1]{\href[pdfnewwindow=true]{#1}{\nolinkurl{#1}}}

and then say

\urlNewWindow{URL}
Boris
  • 38,129
  • 1
    Thank you Boris, but it doesn't work for me. MWE: \documentclass{article} \usepackage[hidelinks]{hyperref} \newcommand{\click}[1]{\href[pdfnewwindow=true]{#1}{\nolinkurl{#1}}} \begin{document} In the meantime please keep yourself updated by visiting our website at \click{www.ieee.org/queensland}. \end{document} – BlackMagic Sep 11 '14 at 03:57
  • @BlackMagic: does it work if you add http:// to the start of your target URL? – cyberSingularity Sep 11 '14 at 06:35
  • 1
    @cyberSingularity Your suggesting fixes the problem. However, \url and \href don't require the http:// prefix. I'm looking for a solution similar to \href with the format \href{www.hidden.url}{text that appears on the page}. – BlackMagic Sep 11 '14 at 15:00
  • 1
    @cyberSingularity and Boris. When I said it works I hadn't tested it fully. Yes, I get a clickable url in a pdf document. But when I open the pdf document in a browser and click on the url the url replaces the pdf document in the browser instead of opening a new web page. This is the behaviour I am trying to avoid. See http://ewh.ieee.org/r10/queensland/v2/lib/exe/fetch.php/mwe.pdf – BlackMagic Sep 11 '14 at 15:26
  • If you want to use the command without the http:// prefix, just say \newcommand{\click}[1]{\href[pdfnewwindow=true]{http://#1}{\nolinkurl{#1}}} – Boris Sep 11 '14 at 16:57
  • @Boris I've partially solved the problem as follows: \newcommand{\click}[2]{\href{http://#1}{\underline{#2}}} The browser still opens in the same window. DUH! The underline isn't coloured, but it's a step forward. – BlackMagic Sep 15 '14 at 04:10
  • @Boris even better:\newcommand{\click}[2]{\href{http://#1}{\textcolor{blue}{\underline{#2}}}} – BlackMagic Sep 15 '14 at 04:13
  • @Boris and cyberSingularity Best solution to date: \documentclass{article} \usepackage{xcolor} \usepackage[hidelinks]{hyperref} \newcommand{\click}[2]{\href{http://#1}{\colorlet{temp}{.}\color{blue}{\underline{\color{temp}#2}}\color{temp}}} \begin{document} Please visit our \click{ewh.ieee.org/r10/queensland/v2/doku.php}{website} and sign the visitor's book. \end{document} This solution puts a blue \underline under the link name and leaves other colours intact. Thank you for getting me this far. – BlackMagic Sep 19 '14 at 00:48
  • @cyberSingularity Please see my comment to Boris above. I've solved the problem of getting an acceptable-looking clickable link into my PDF documents. Thank you for your assistance. Opening a new browser tag from a PDF document is possible according to the published Adobe pdf specs but hyperref doesn't have that feature at present. – BlackMagic Sep 19 '14 at 00:56
  • I believe this answer does not do anything in case of link to website instead of another PDF document, refer to https://tex.stackexchange.com/questions/179729/how-tell-hyperref-to-insert-a-target-in-the-resulting-href?noredirect=1&lq=1#comment1701202_202493 . – user202729 May 14 '23 at 10:14
3

The answer to my question: hyperref doesn't have a feature to direct a web browser to open in a new tab from a PDF document. The Adobe PDF specs define a method for opening new browser tabs from a PDF document, but the example provided by Adobe is a Javascript implementation and doesn't apply to LaTeX.

A subsidiary issue in the thread: creating a command - using \href - that puts a coloured line under a clickable web link in a PDF document without invoking \hypersetup is solved as follows:

\documentclass{article}  
\usepackage{xcolor}  
\usepackage[hidelinks]{hyperref}  
\newcommand{\click}[2]{\href{http://#1}{\colorlet{temp}{.}\color{blue}{\underline{\color{temp}#2}}\color{temp}}}  
\newcommand{\clicks}[2]{\href{https://#1}{\colorlet{temp}{.}\color{blue}{\underline{\color{temp}#2}}\color{temp}}}  
\begin{document}  
Please visit \click{www.disney.com}{Disneyland} and sign the visitors' book. 
This is how engineers make \clicks{www.youtube.com/watch?v=aX4Oi6pPOpQ}{tea}.  
\end{document} 

I have illustrated two commands: \click for ordinary http access and \clicks for https access. Commands for other protocols can be created by changing the protocol designator. Change the colour of the underline to suit your needs.

The main benefit of using the commands is being able to treat different protocols in different ways when using standard hyperref, eg, e-mail addresses without underlines, web addresses highlighted by coloured underlines. Another benefit is that the commands don't change the background font.

BlackMagic
  • 349
  • 1
  • 2
  • 12
  • 1
    Blue underlining can be done using a PDF feature: \usepackage{xcolor} \hypersetup{linkbordercolor=blue, pdfborderstyle={/S/U/W 1}}. But the style applies to all link types. (It's documented in hyperref's README). – Heiko Oberdiek Sep 19 '14 at 05:28
  • Please have a look at how to markup posts. you can also have a look at our TeX.SX starter guide if you like. – Johannes_B Sep 19 '14 at 07:34
  • Remark, I believe it's possible to embed JavaScript in https://tex.stackexchange.com/questions/45106/how-to-make-interactive-pdf-documents-with-tex but as Heiko mentioned below, the browser may not support it anyway. ■ also I believe it's possible in PDF format somehow on further research https://superuser.com/questions/135618/word-document-to-pdf-open-hyperlinks-in-new ■ another demo how to use JavaScript can be found in https://community.adobe.com/t5/acrobat-discussions/ensuring-pdf-hyperlinks-open-in-a-new-tab-or-window-in-all-browsers/m-p/10952017 although as mentioned it's not portable – user202729 May 14 '23 at 10:31
1

Boris' answer should work, but dare I say, I think we've found a bug in hyperref, at least in the hpdftex.def driver v6.83m (I haven't checked any of the others).

The issue is that the pdfnewwindow=true option is being ignored. If you examine the output pdf file in a text editor, you should notice something like this:

/Subtype/Link/A<</Type/Action/S/URI/NewWindow true/URI(http://www.ieee.org/queensland)>>

In particular, there should be something saying /NewWindow true, but it doesn't seem to be there, unless we make the following patch first (basically adding a call to \Hy@SetNewWindow in the appropriate place):

\documentclass{article}
\usepackage[hidelinks]{hyperref}
\newcommand{\click}[1]{\href[pdfnewwindow=true]{http://#1}{\nolinkurl{#1}}}
\makeatletter
\def\hyper@linkurl#1#2{%
  \begingroup
    \Hy@pstringdef\Hy@pstringURI{#2}%
    \hyper@chars
    \leavevmode
    \pdfstartlink
      attr{%
        \Hy@setpdfborder
        \ifx\@pdfhightlight\@empty
        \else
          /H\@pdfhighlight
        \fi
        \ifx\@urlbordercolor\relax
        \else
          /C[\@urlbordercolor]%
        \fi
      }%
      user{%
       /Subtype/Link%
       \ifHy@pdfa /F 4\fi
       /A<<%
         /Type/Action%
         /S/URI%
          \Hy@SetNewWindow
         /URI(\Hy@pstringURI)%
         \ifHy@href@ismap
           /IsMap true%
         \fi
         \Hy@href@nextactionraw
       >>%
      }%
      \relax
    \Hy@colorlink\@urlcolor#1\Hy@xspace@end
    \close@pdflink
  \endgroup
}
\makeatother
\begin{document}
In the meantime please keep yourself updated by visiting our website at \click{www.ieee.org/queensland}.
\end{document}
  • 2
    This isn't a bug, hyperref is correct here. URI actions do not know a NewWindow setting. NewWindow is defined for GoToR actions (for opening other PDF files). How an HTML browser manages its windows it outside the control of a PDF viewer. – Heiko Oberdiek Sep 11 '14 at 21:22
  • @HeikoOberdiek: Okay, sorry, I stand corrected... I should have guessed from the way the key is named and from the wording of its behaviour in the manual. Oops! – cyberSingularity Sep 11 '14 at 21:27
  • @HeikoOberdiek What's the point of having the pdfnewwindow=true option if it doesn't produce the expected behaviour at the browser level? http://en.wikibooks.org/wiki/LaTeX/Hyperlinks says "pdfnewwindow(=true,false) define if a new window should get opened when a link leads out of the current document". It makes no difference whether pdfnewwindow is included or excluded from a pdf document: the behaviour is the same in either case. – BlackMagic Sep 12 '14 at 00:01
  • The hyperref manual says: "pdfnewwindow: make links that open another PDF file start a new window". Notice the use of PDF. Internally pdfnewwindow addresses GoToR actions (GoToR: go to remote PDF file). \href creates a few link annotation types, GoToR is among them. – Heiko Oberdiek Sep 12 '14 at 00:14
  • @HeikoOberdiek Semantically you are correct. But using semantics to defend a flawed implementation of hyperref doesn't cut the mustard. The people who wrote the wikibooks articles didn't detect the subtle semantics of your implementation process. Neither did I. Also, treating e-mail addresses as equivalent to web addresses is another flaw in the implementation of hyperref. All in all I found hyperref to be the most disappointing Latex package that I have encountered so far. Perhaps you disagree, but as a user of the package I find it doesn't come close to my requirements. – BlackMagic Sep 12 '14 at 13:30
  • @BlackMagic URIs are equivalent regardless of the protocol (http, https, ftp, mailto, ...) with the exception of some file types that are supported by other means depending on the driver. – Heiko Oberdiek Sep 12 '14 at 13:35
  • 7
    @BlackMagic Very disappointing that hyperref does not implement and embed a full-blown web browser in the PDF file. How do you think, hyperref could control the back button of a web browser? It does not even create a web page, but a PDF document here. It is a quite odd expectation, that a LaTeX package could and should fix bugs and missing features in arbitrary other software, not even related to TeX. – Heiko Oberdiek Sep 12 '14 at 13:43
  • @\documentclass{article} \usepackage[hidelinks]{hyperref} \begin{document} In the meantime please keep yourself updated by visiting our website at \url{www.ieee.org/queensland}. \end{document} – BlackMagic Sep 15 '14 at 01:13
  • @HeikoOberdiek I am not asking that hyperref embeds a full-blown web browser in the PDF file. I edit a newsletter for an international organisation with over 400,000 members and my newsletters are always read in a web browser. What I'm asking is: 1) hyperref gives a content generator the ability to open a browser in a new window. In the HTML world this everyday behaviour is achieved by passing a parameter to the browser. Is it impossible for hyperref to pass the same parameter? – BlackMagic Sep 15 '14 at 03:12
  • @HeikoOberdiek 2) hyperref treats all hyperlinks in the same way. As a content generator I might want to underline web links in a Latex PDF document but not underline e-mail addresses. Treating them as identical entities limits hyperref's usefullness for content generators like me. – BlackMagic Sep 15 '14 at 03:13
  • 2
    @BlackMagic 2) TeX is a macro language. Define a macro that sets the email address in a group, where the settings of hyperref and the appearance of the email address can be changed locally. – Heiko Oberdiek Sep 15 '14 at 11:02
  • @BlackMagick 1) Please send me a working PDF for analyzing, which shows your required feature. I am not aware of any feature in the PDF format, that allows the configuration of an HTML browser. – Heiko Oberdiek Sep 15 '14 at 11:14
  • @HeikoOberdiek Thank you for your reply. Not sure what you mean by 'working PDF' but click on http://ewh.ieee.org/r10/queensland/v2/doku.php/newsletters:archive then select August from the drop-down menu. Notice that clicking on e-mail addresses opens an e-mail session, but clicking on web links opens a browser in the same window. Read my reply to Boris above to see how I implemented the url behaviour. Not opening a new browser window poses difficulties for a newsletter reader, especially if he/she closes the browser window before returning to the newsletter. – BlackMagic Sep 15 '14 at 13:47
  • @BlackMagic I can only repeat myself: I am not aware of any feature in the PDF format, which controls, how a HTML browser opens a link. And if such a feature exists, I have some doubts, that this would work in a way that could be called reliable. There are too many web browser and ways to open a PDF file by a web browser (directly using JavaScript, plugin, external PDF viewer). Please read the PDF specification and show me the feature, which could be used to implement your request. – Heiko Oberdiek Sep 15 '14 at 14:09
  • @HeikoOberdiek for information on launching a new browser window from a pdf document see page 114 of this Adobe developer document: http://partners.adobe.com/public/developer/en/acrobat/sdk/AcroJS.pdf – BlackMagic Sep 17 '14 at 03:33
  • @BlackMagic Thanks for finding it. However, JavaScript is not much of an option, because many PDF viewers are not supporting it. – Heiko Oberdiek Sep 17 '14 at 09:58
  • @HeikoOberdiek I'm not suggesting you use Javascript. The point of showing you the Adobe developer document is to confirm that an option exists within a PDF document to tell the user's default browser to open a new browser window. – BlackMagic Sep 17 '14 at 13:12
  • @BlackMagic How do you want to use a JavaScript function without JavaScript? – Heiko Oberdiek Sep 17 '14 at 13:16
  • @HeikoOberdiek OK. I get the picture. For the last 2 weeks you have been obfuscating about this issue. When I produce evidence that Adobe has an option in its PDF specs to allow a browser to open in a new page you continue to obfuscate, this time about Javascript. Why not do some research and find out how to implement the Adobe option in LaTex? Is that too difficult? Until you provide the Adobe option hyperref will continue to be flawed, just as it is flawed in the way it handles default choice of colours, bright green rectangular boxes around web links, etc. – BlackMagic Sep 17 '14 at 23:29
  • 4
    @BlackMagic If you think, nagging on volunteers will help that features of third-party software will fall out of the sky, then continue. For me it is end of thread. – Heiko Oberdiek Sep 17 '14 at 23:57
  • 1
    @HeikoOberdiek End of the thread for me too. Your contribution wasn't helpful, therefore no point in continuing the thread. – BlackMagic Sep 19 '14 at 04:35