cryptobib / db

CryptoBib Database for Developers
40 stars 67 forks source link

Replace "_" with "\_" in doi #221

Closed xagawa closed 1 year ago

xagawa commented 1 year ago

The current cryptobib with splncs04.bst causes a lot of errors because of _ in typical doi of papers in LNCS. Replacing _ with \_ in doi eliminates this type of error and works well with and without hyperref.

splncs04.bst will output

\providecommand{\url}[1]{\texttt{#1}}
\providecommand{\urlprefix}{URL }
\providecommand{\doi}[1]{https://doi.org/#1}

just after \begin{thebibliography}{nn}. When doi contains plain _, say, 10.10_1, this is converted as https://doi.org/10.10_1 and we have the error Missing $ inserted.

GeeLaw commented 1 year ago

I remember (at least a previous version of) llncs.cls had other problems with \doi if hyperref is loaded. Over the time I find the best fix is to add the following to the preamble after \documentclass{llncs} (or otherwise loading llncs):

% LNCS detects whether "hyperref" is loaded
% in \AtBeginDocument and overwrites \doi if so.
% We overwrite \doi again using our version.
\AtBeginDocument{\def\doi#1{\url{https://doi.org/#1}}}

This also suppresses the effect of \providecommand{\doi} of splncs04.bst because \doi will already be defined by the time TeX processes bibliography. It assumes that you load hyperref (which I think is the norm these days?).

GeeLaw commented 1 year ago

Adding a version that works without hyperref:

\documentclass{article}

% This excerpt of code is placed in the public domain.
\makeatletter
\def\doi{\begingroup\catcode`\%=12\relax\doi@}
\def\doi@#1{\endgroup DOI {\ttfamily\doi@loop#1\doi@end}}
\def\doi@loop{\@ifnextchar\doi@end{\doi@end}{\begingroup\let\bgroup\relax
  \@ifnextchar\doi@brace@{\endgroup\doi@brace}{\endgroup\doi@token}}}
\let\doi@brace@\bgroup
\def\doi@brace#1{\string{\doi@loop#1\doi@end\string}\doi@loop}
\def\doi@token#1{\expandafter\doi@check\string#1\doi@loop}
\def\doi@check{\expandafter\@ifnextchar\string~{\raisebox{-0.2em}}{}}
\def\doi@end\doi@end{}
\makeatother

\begin{document}
\doi{`1234567890-=~!@#$%^&*()_+[]\ {xyz}|;':",./<>?\egroup\bgroup}.
\end{document}

It feels wrong to skew the simplest, unescaped encoding of DOI due to incapability of certain TeX macros.

xagawa commented 1 year ago

@GeeLaw Thank you for the fixes! The first one works well and I'll use it with \urlstyle{same} to use a roman font as llncs did.