3

How can I use manual hyphenation commands (see https://de.wikibooks.org/wiki/LaTeX-W%C3%B6rterbuch:_Silbentrennung) inside the text part of \href?

\documentclass[a5paper]{article}

\RequirePackage[ngerman]{babel}

\usepackage[hidelinks]{hyperref}

\begin{document}

Hier steht Text. Hier steht Text. Hier steht Text. Ein \href{https://google.com}{laaaaaa"-aaaaaaaaaaaaaaaaaaanges} Wort \ldots

\end{document}

enter image description here

As requested by @Mico the specific use case (in a footnote):

enter image description here

Jake
  • 33
  • Welcome to TeX.SE. – Mico Jun 04 '22 at 12:35
  • Does \href really support line breaks? If it did so, the parts before and after the break could end up on separate pages, with the footer of the first page and the header of the second page between them. That would complicate the creation of the "clickable area" in PDF quite a lot. – Heiko Theißen Jun 04 '22 at 12:42
  • Thanks for posting an actual use case, rather than misdirecting us with the made-up string "laaaaaaaaaaaaaaaaaaaaaaaaanges", and also for mentioning that the use case occurs in a footnote. (This could prove to be an important piece of information.) Please tell us more about how (and possibly if) you're using the babel package (along with a suitable language option, right?) to allow extra hyphenation points in compound hyphenated words. – Mico Jun 04 '22 at 16:50
  • Please see the fully revised answer i just posted. – Mico Jun 04 '22 at 19:38

2 Answers2

3

You can use manual hyphenation commands, but you can't always use the german shorthands: in some cases hyperref disables them while trying to detect the link type. So in the following it work for \href{blub} but not for \href{https://...}. Use other commands to indicate hyphenation points, e.g. \- or the \babelhyphen command of babel:

\documentclass[a5paper]{article}

\usepackage[ngerman]{babel}

\usepackage[hidelinks]{hyperref}

\begin{document}

Hier steht Text. Hier steht Text. Hier steht Text. Ein \href{blub}{laaaaaa"-a"aaaaaaaaaaaaaaanges} Wort \ldots

Hier steht Text. Hier steht Text. Hier steht Text. Ein \href{https://google.com}{laaaaaa"-a"aaaaaaaaaaaaaaanges} Wort

Hier steht Text. Hier steht Text. Hier steht Text. Ein \href{https://google.com}{laaaaaa-aäaaaaaaaaaaaaaanges} Wort

Hier steht Text. Hier steht Text. Hier steht Text. Ein \href{https://google.com}{laaaaaa\babelhyphen{soft}aäaaaaaaaaaaaaaanges} Wort \end{document}

enter image description here

Thorsten
  • 12,872
Ulrike Fischer
  • 327,261
0

(I completely rewrote this answer after the OP provided a specific use case.)

I think you may be overthinking things. There's neither a need nor requirement to provide the full title of the publication in the second argument of \href. Providing just the first word of the title is perfectly fine. A happy side-effect of making this choice is that you no longer need to worry about arranging hyphenation of compound words within \href arguments.

enter image description here

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage[german=swiss]{csquotes}
\hyphenation{pen-to-bar-bi-tal} 
\usepackage[colorlinks,allcolors=blue]{hyperref}
\setlength\textwidth{11.5cm}
\setlength\textheight{2cm}

\begin{document} xxx.\footnote{Kantonsapothekervereinigung. \href{https://www.kantonsapotheker.ch/de}{Positionspapier} betreffend die Abgabe von Pentobarbital"=Natrium zur Sterbehilfe, Oktober 2017, Ziff.~4.3: \dots} \end{document}

Mico
  • 506,678
  • 1
    I tried your snippet in Overleaf and it does not hyphenate. I also added \showlists and this shows what I suspected: \href puts its second argument in an extra \hbox(6.94444+1.94444)x146.72264 so that no line-breaking can happen. I wonder how you produced your screenshot, however. – Heiko Theißen Jun 04 '22 at 13:12
  • @HeikoTheißen - My TeX distribution is MacTeX2022, all updates applied, on a 16" MacBookPro laptop with a M1-Max CPU that runs MacOS 12.4 "Monterey". The code shown above compiles fine with both pdfLaTeX and LuaLaTeX, with both the "standard LaTeX branch" (<2021-11-15> patch level 1) and the "development LaTeX branch" (<2022-06-01> pre-release-4). Incidentally, I get a "Restricted, sorry you don’t have permission to load this page" error message when i tried to click on the link you provided. – Mico Jun 04 '22 at 13:49
  • If I use pdfLaTeX as compiler, then it works. BUT, as I pointed out in my comment to the question, if a page break happens between the two lines, then the page number in the footer also becomes part of the hyperlink. See Overleaf (public link this time). – Heiko Theißen Jun 04 '22 at 13:57
  • 1
    @HeikoTheißen in newer LaTeX this is no longer a problem. – Ulrike Fischer Jun 04 '22 at 14:42
  • @Mico Thank you for your answer. Is there no possibility to use the hyphenation commands "-, "" etc. mentioned on https://de.wikibooks.org/wiki/LaTeX-W%C3%B6rterbuch:_Silbentrennung inside \href? – Jake Jun 04 '22 at 15:34
  • @UlrikeFischer I have TeXLive 2021 in Overleaf. – Heiko Theißen Jun 04 '22 at 15:37
  • 1
    @HeikoTheißen - Judging by the link you posted, Overleaf still uses LaTeX2e <2021-06-01>. In contrast, with MacTeX2022 I can use (depending on whether I use the standard of the developmental branch) either version <2021-11-15> and/or version <2022-06-01> pre-release-4>. – Mico Jun 04 '22 at 15:57
  • @Jake - It would really help, in terms of being able to understand your use case, if you posted a real long world rather than "laaaaaaaaaaaaaaaaaaaaaaaaanges" (which, I dare assume, you made up on the spot). – Mico Jun 04 '22 at 15:59
  • 1
    @Mico Please see the image I added to the question. – Jake Jun 04 '22 at 16:18