Open bgvoisin opened 5 years ago
Ditto to the above. I'm currently using \url rather than \doi in order to get it to wrap. Thankfully the dois I'm currently using don't contain any bad characters yet, but it would be nice to have this option (or if it's an option already - to have some info on how to use it).
Dear all, I was wondering whether anybody worked out a way around to wrap/hyphenate long doi's that do not contain hyphens (-) but only letters and numbers? I have tried to use \url from the xurl package but it is not as neat as doi. Many thanks, Patrick
@pguio as far as I can see the package does nothing special here it just wraps the generated URL in \href
so you can use any of the commands from url package to customise page breaking, specifically if you load the xurl package it will allow breaking more or less anywhere.
@davidcarlisle I am not sure to follow you, \href is a command from the hyperref package used by the \doi command in the doi package. Hyperref does not contain advanced breaking such as xurl as far as understand. As a temporary fix I redefined the command \doi to \renewcommand{\doi}[1]{\url{#1}} thus not using at all doi command from the doi package. The doi's breaking is better but the neat links of the doi package do not work. Did I miss something? Many thanks, Patrick
@pguio ah sorry hyperref loads url to allow breaking but you are right it's not actually used in this code path, so if you simply change doi.sty to use \url to allow breaking something like ` \edef\x{\toks0={\noexpand\url{#1}}}% does that do what you expect? There is no test case in this issue, so if you have a small test document to hand that would be useful....
@davidcarlisle I have tried but the font is then changed which can be remedied using \urlstyle{same} but then the problem is the url link is not working. I tried to add \doirul as for \href `\edef\x{\toks0={\noexpand\url{\doirul#1}}}% but it does not work. I am not sure to understand how it works as \url{} takes one argument while \href{}{} takes too. Do you have any suggestions? Many thanks, Patrick
Here is a small test example
thanks for the test file I'll try this evening but the usual model for a href with breakable text is \href{url}{\nolinkurl{breakable-short-form}}
so for example if you want www.example.com to be breakable as in url.sty but be associated with the full https://www.example.com then
\href{https://www.example.com}{\nolinkurl{www.example.com}}
should do the right thing, and doi.sty ought to be able to generate this form
Thank you David. Would you have a patch for doi.sty to do that? I can't see how to implement this. I am not even sure to understand how the command \doi{} works as it seems to call \href{} with only one argument here: `\edef\x{\toks2={\noexpand\href{\doiurl#1}}}% while I would have expected something like \href{#1}{#2} Patrick
There are two arguments to \href in the definition of \doi: first \the\toks2, which is a form of the DOI (prefixed by \doiurl) in which the problematic characters (having special TeX meaning) "#", "", "<" and ">" have been replaced by their URL encoding %23 etc.; and second \the\toks0 which is the original DOI text. (Don't ask me more, I never really understood how Heiko Oberdiek's code worked – all this \edef\x{...}\x trickery –, and I could never figure out why is treated differently from #, < and > either.)
After some trial-and-error, the following seems to work (the only changed line is the one containing \noexpand\nolinkurl):
\makeatletter \def\@doi#1{% \let#\relax \let_\relax \let\textless\relax \let\textgreater\relax \edef\x{\toks0={{\noexpand\nolinkurl{#1}}}}% \x \edef#{\@percentchar23}% \edef_{_}% \edef\textless{\@percentchar3C}% instead of {\string<} for Apple \edef\textgreater{\@percentchar3E}% instead of {\sting>} for Apple \edef\x{\toks2={\noexpand\href{\doiurl#1}}}% \x \edef\x{\endgroup\doitext\the\toks2 \the\toks0}% \x } \makeatother
(Sorry for the formatting, I tried to use Insert Code but if I do so all the line breaks seem to be lost on Preview.)
To be double-checked by David, of course, I'm not sure to understand how or why this works.
Personally, I used this flexibility in the definition of \doi to create a derivative \doirref{
\makeatletter
\newcommand*{\doiref}{%
\begingroup
\lccode\~=
#\relax
\lowercase{\def~{#}}%
\lccode\~=
_\relax
\lowercase{\def~{_}}%
\lccode\~=
\<\relax
\lowercase{\def~{\textless}}%
\lccode\~=
>\relax
\lowercase{\def~{\textgreater}}%
\lccode\~=0\relax \catcode
#=\active
\catcode\_=\active \catcode
\<=\active
\catcode`>=\active
\@doiref
}%
\def\@doiref#1{%
\edef#{\@percentchar23}%
\edef_{_}%
\edef\textless{\@percentchar3C}%
\edef\textgreater{\@percentchar3E}%
\edef\x{\toks2={\noexpand\href{\doiurl#1}}}%
\x
\edef\x{\endgroup\the\toks2 }%
\x
}
\makeatother
and used for example as
\bibitem{LleYou02} Llewellyn~Smith, S.~G. \& Young, W.~R. (2002) \doiref{10.1175/1520-0485(2002)032<1554:COTBT>2.0.CO;2} {Conversion of the barotropic tide}. \textit{J. Phys. Oceanogr.} \textbf{32}, 1554--1566.
But then in this case of course, the linked text is hyphenated the usual way, as any text.
@bgvoisin I tried to use your code but latex is complaining. Are you sure about your code? And do you mean the result is hyphenated with an hyphen added at the end of the line before the break?
@pguio No I just added the code to your test file. On my setup (MacTeX 2020, pdfLaTeX) it typesets just fine. I've attached the input and output files. If the line calling xurl is commented out, overfull boxes are generated, but that's about it. longdoi.zip
@bgvoisin I tried to use your code but latex is complaining. Are you sure about your code? And do you mean the result is hyphenated with an hyphen added at the end of the line before the break?
@bgvoisin Sorry my mistake, I was not clear, your first code for \doi works very nicely, I meant the second codlet, for the command \doiref seems to create trouble for latex. Sorry again
@pguio The trouble is indeed there, you're right. It comes from the way the codlet was modified when it was pasted inside this interface, some backquotes were interpreted as markup delimiting code input (the backquotes in \lccode`\~ and so forth – I had to experiment with escape sequences just to be able to enter the preceding right now).
The same problem affected my pasted example of use (\& replaced by &).
I'm helpless with this commenting interface. I can't figure out how to include verbatim input. I should have checked, sorry.
Attached is an example of use of the codlet, with two DOIs that wouldn't work just using \href, you have to use doi.sty.
@bgvoisin Thank you I think I understand now. So \doiref is a two-argument command where the first argument is the doi and the second argument is the title or any text to link to the doi? That can be nice and useful for specific situation but not easy to use with bibtex as there is no automatic mechanism to generate \doiref unless you also have a dedicated bib style. Am I correct? Many thanks, Patrick
@bgvoisin Thank you I think I understand now. So \doiref is a two-argument command where the first argument is the doi and the second argument is the title or any text to link to the doi?
Yes, originally it was described as \doiref{
}{some text} in the message where I mentioned it, but the « interpreter » removed the without my realizing. That can be nice and useful for specific situation but not easy to use with bibtex as there is no automatic mechanism to generate \doiref unless you also have a dedicated bib style. Am I correct? This I cannot say: I don’t use BibTeX (unless collaborating with people who do), I do bibliographies by hand the old-fashioned way.
Bruno
@bgvoisin Thank you Bruno. Why don't you push the patch for the \doi and the added command \doiref into the doi.sty package? It would be nice to have the \doi command breaking down nicely long dois. In the meantime I created for my personal use a xdoi.sty package containing the fix for \doi and the new command \doiref. Many thanks again! Patrick
First thanks to David for taking over the maintenance of this package, and already starting to improve it (I had been willing for some time to update it by replacing http://dx.doi.org with https://doi.org, but never got myself to do it). The following is not really an issue, more an item for the wishlist: it would be nice to be able to customize possible DOI breakpoints, similar to \UrlBreaks from url.sty. For example one reference I have to cite just today is
R.~D. Sharman \& M.~G. Wurtele, Ship waves and lee waves, \textit{Journal of the Atmospheric Sciences}, 1983, \textbf{40} (2): 396--427; \doi{10.1175/1520-0469(1983)040<0396:SWALW>2.0.CO;2}.
The output would be nicer if the DOI were allowed to break after say ">", ")", ":" and ";". Using \url{https://doi.org/10.1175/1520-0469(1983)040<0396:SWALW>2.0.CO;2} instead allows that, but isn't as clean.
Bruno Voisin