# How do I underline the DOI in the bibliography using biblatex/biber? (Context: continued work on SAE class)

I've been continuing the work of developing a modern SAE class, and have found previous helps on this forum extremely useful. The next thing I am trying to accomplish is underlining the DOI in the bibliography to meet the SAE guidelines for the article class (for starters).

See here for the previous answers. For context, I'd like to provide a preamble that seems to work relatively well for this SAE class, as well as the latest *.cls file. This is the result of work by myself but also of a number of other people that I am building on.

\documentclass[letterpaper,9pt]{saeRyan}

% This package provides a handy way to deal with page number handling (fancy headers and footers)
\usepackage{fancyhdr}
\usepackage{lastpage}
\pagestyle{fancy}
\fancyhf{}
\lfoot{Page \thepage \hspace{1pt} of \pageref{LastPage}}
\renewcommand{\footrulewidth}{0pt}
\widowpenalty 10000
\clubpenalty 10000

% This package allows for footnoting within tables
\usepackage{enumitem,booktabs,cfr-lm}
\usepackage{tabularx}
\usepackage[referable]{threeparttablex}
\renewlist{tablenotes}{enumerate}{1}
\makeatletter
\setlist[tablenotes]{label=\tnote{\alph*}, ref=\alph*, itemsep=\z@, topsep=\z@skip, partopsep=\z@skip,parsep=\z@,itemindent=\z@,labelindent=\tabcolsep,labelsep=.2em,leftmargin=*,align=left,before={\footnotesize}}
\makeatother

% This package is used for language localization. SAE uses American English!
\usepackage[american]{babel}

% This package is used for advanced context-sensitive quotes. It is strongly recommended by the biblatex package, especially for writing in languages other than American English.
\usepackage{csquotes}

% This package provides some more advanced formatting of DOI within the document and for the bibliography.
\usepackage{doi}

% This package allows detailed control over the usage of underscores within a document
\usepackage{underscore}

% This package allows detailed control of spacing around other environments (i.e. enumitem, glossary, appendix)
\usepackage{atbeginend}

% This package allows the use of color within the document.
\usepackage{color}

% This package allows for detailed control of urls within the document and in the bibliography.
\usepackage{url}

% This package is the latest and greatest used for inserting graphics into your document.
\usepackage{graphicx}
\graphicspath{{figures/}} % Provide a path to the document's figures.

% This package is for multi-figure usage (deprecated, but still useful here)
\usepackage{subfigure}

% This package is used to get more control over the positioning of floating objects (graphics, tables, etc.)
\usepackage{float}

% This package is highly recommended by the biblatex package and extends the patching capability of the etoolbox package to biblatex macros, drivers, and formatting directives.
\usepackage{xpatch}

% For setting bibliography styles, this is the master package. Note: the following packages are NOT compatible with the biblatex package and should NOT be loaded: babelbib, backref, bibtopic, bibunits, chapterbib, cite, citeref, inlinebib, jurabib, mcite, mciteplus, multibib, natbib, splitbib, titlesec, ucs.
\usepackage[style=numeric, maxbibnames=4, minbibnames=3, sorting=none,firstinits=true, url=false, doi=true, backend=biber]{biblatex}

% This command will put a comma after the author list, before the title in the bibliography.

% This command will reformat the pages field to remove the "pp." prior to the pages listing in the bibliography.
\DeclareFieldFormat{pages}{#1}

% This command will remove the ", " prior to pages, and replace it with a colon.

% Show author names by last name for all classes.
\DeclareNameAlias{default}{last-first}
\DeclareNameAlias{sortname}{last-first}

%\DeclareFieldFormat*{title}{\mkbibquote{#1\isdot}}

% Remove the "in:" text prior to the "journaltitle" field in the bibliography
\renewbibmacro*{in:}{}

\renewbibmacro*{journal+issuetitle}{%
\usebibmacro{journal}%
\iffieldundef{series}
{}
{\newunit
\printfield{series}%
\usebibmacro{volume+number+eid}%
\usebibmacro{issue}%
\newunit}

\DeclareFieldFormat[article,periodical]{number}{\mkbibparens{#1}}
\renewbibmacro*{volume+number+eid}{%
\printfield{volume}%
\printfield{number}%
\printfield{eid}}

\renewbibmacro*{note+pages}{%
\printfield{note}%
\setunit{\bibpagespunct}%
\printfield{pages}%
\newunit
\usebibmacro{date}}

\renewbibmacro*{publisher+location+date}{%
\printtext[parens]{%
\printlist{location}%
\printlist{publisher}%
\usebibmacro{date}}%
\newunit}

\DeclareFieldFormat{url}{\url{#1}}
\DeclareFieldFormat{urldate}{\bibstring{urlseen}\space#1}
\renewbibmacro*{url+urldate}{%
\usebibmacro{url}%
\iffieldundef{urlyear}
{}
\usebibmacro{urldate}}}

\DefineBibliographyStrings{american}{urlseen = {accessed}}

% Remove the default italics in the "journaltitle" field in the bibliography
\DeclareFieldFormat[article]{journaltitle}{#1}

% This block will help with the formatting of the DOI in the references. It will put the DOI on a new line (rather than split across two lines), and makes a lowercase "doi:" rather than "DOI:"
\DeclareFieldFormat{doi}{%
\newline
\ifhyperref
\renewcommand*{\mkbibacro}[1]{%
{\MakeLowercase{#1}}}

% This block will prevent biblatex from showing issns and other fields in the output for the article class. SAE prefers using DOI over ISSN. This code is cleaner than having to remove these things from your *.bib file(s). The lower block removes the publisher and editor in all classes except for books.
\AtEveryBibitem{% Clean up the bibtex rather than editing it
\ifentrytype{article}{
\clearfield{date}
\clearfield{eprint}
\clearfield{isbn}
\clearfield{issn}
\clearlist{location}
\clearfield{month}
\clearfield{series}
}{}

\ifentrytype{book}{}{% Remove publisher and editor except for books
\clearlist{publisher}
\clearname{editor}
}
}

% Typesets the URL (and DOI) in the same font as the document
\urlstyle{same}

% This line defines which file(s) bibtex will look for your bib entries.
\bibliography{library}

% This package is used for hyperlinks within the document and to manage external hyperlinks in a final pdf. The biblatex package recommends loading this package after biblatex.
\usepackage{hyperref}

% Subfigure label control
\setlength{\subfigcapskip}{-0.5cm}
\setlength{\subfigcapmargin}{-0.15cm}
\renewcommand*{\thesubfigure}{\alph{subfigure})}

% This should give a pretty decent compromise with hyphenation (as in reduce the amount of hyphenation in the paper without making things look bad).
\tolerance=1
\emergencystretch=1.4in%\maxdimen % Using \maxdim can cause too much spacing between the bibliography entries.
\hyphenpenalty=1000

\PaperTitle{Title}
\PaperNumber{2000-00-00}
\AddAuthor{Author, co-author list ({\color{red}Do NOT enter this information. It will be pulled from participant tab in MyTechZone})}{Affiliation ({\color{red}Do NOT enter this information. It will be pulled from participant tab in MyTechZone})}


The following is the current SAE class, (work in progress, beta, based on class originally written by Axel Franke, and modified by others since, called SAERyan.cls in this example):

%%
%% LaTeX document style sae', for use with LaTeX2e
%% This is a style for typesetting an SAE technical paper.
%%
%% COPYRIGHT 1999,2000 by Axel Franke, axel.franke@forbrf.lth.se
%%
%% Copying of part or all of any file in the sae.cls style
%% is allowed under the following conditions only:
%% (1) You may freely distribute unchanged copies of the files. Please
%%     include the documentation when you do so.
%% (2) You may modify a renamed copy of any file, but only for personal
%%     use or use within an organization.
%% (3) You may copy fragments from the files, for personal use or for use
%%     in a macro package for distribution, as long as credit is given
%%     where credit is due.
%%
%% You are NOT ALLOWED to take money for the distribution or use of
%% these files or modified versions or fragments thereof, except for
%% a nominal charge for copying etc.
%%

% Identification
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{saeRyan}[2000/06/28 SAE Technical paper format^^J

\DeclareOption{times}{%
\renewcommand{\familydefault}{ptm}%
\typeout{^^JSetting document font to Times^^J}%
}
\DeclareOption{helvetica}{%
\renewcommand{\familydefault}{phv}%
\typeout{^^JSetting document font to Helvetica^^J}%
}
\DeclareOption{a4paper}{%

\typeout{^^JPaper size: A4^^J}%
\AtBeginDocument{ \columnsep 15mm }
}
\DeclareOption{letterpaper}{%
\typeout{^^JPaper size: Letter^^J}%
\AtBeginDocument{ \columnsep .5in }
}

\DeclareOption{pdftex}{%
\PassOptionsToClass{\CurrentOption}{article}%
\PassOptionsToPackage{\CurrentOption}{geometry}
}

\DeclareOption{nonumber}{%
\pagestyle{empty}%
}

%% What we do not understand:
\DeclareOption*{\typeout{^^JOption [\CurrentOption] not %
supported by this style.^^J}}

%% default otions
\ExecuteOptions{times,letter,9pt}
\ProcessOptions

% Using classes and packages
\RequirePackage{geometry}

\RequirePackage{colortbl} % colored tables
\RequirePackage{color}
\RequirePackage{xcolor}

% SAE color palette
%\definecolor{SAEblue}{cmyk}{0.86, 0.08, 0.04, 0.20}
\definecolor{SAEblue}{rgb}{0, .62, .91}
\definecolor{SAEdarkblue}{cmyk}{1.0, 0.6, 0.04, 0.19}
\definecolor{SAElightgreen}{cmyk}{0.69, 0, 0.97, 0}
\definecolor{SAEdarkgreen}{cmyk}{1.00, 0.02, 1.00, 0.1}
\definecolor{SAEyellow}{cmyk}{0, 0.31, 0.95, 0}
\definecolor{SAEorange}{cmyk}{0, 0.62, 0.95, 0}
\definecolor{SAEred}{cmyk}{0, 0.95, 1.00, 0}
\definecolor{SAElightgray}{cmyk}{0, 0, 0, 0.2}
\definecolor{SAEmediumgray}{cmyk}{0, 0, 0, 0.45}
\definecolor{SAEdarkgray}{cmyk}{0, 0, 0, 0.8}

% TexShop
\RequirePackage{graphicx}
% caption font
\RequirePackage{caption}
%\renewcommand\captionfont{\color{SAEblue}\small}
\renewcommand\captionfont{\color{SAEblue}\small}
%\renewcommand\captionlabelfont{\bfseries}

\renewcommand\@biblabel[1]{#1.}

%% Declarations

% sections etc. not numbered
\setcounter{secnumdepth}{-2}

% LaTeX uses rubber length for \parskip
% SAE style seems to use fixed length
\setlength{\parskip}{\baselineskip}
%\setlength{\parskip}{0 pt}

% distance between the preceding text and a list item (description
% list, enumerated list, etc.) was too large
\setlength{\topsep}{-\parskip}
%  This places floats where they should go (suggested by Ross Pursifull,
%  "Pursifull, Ross (R.)" <rpursifu@visteon.com>, 19991207)
\renewcommand{\bottomfraction}{1}
\renewcommand{\textfraction}{0}
\setcounter{topnumber}{10}
\setcounter{bottomnumber}{10}
\setlength{\parindent}{0pt}

%\renewcommand{\section}{\@startsection
%  {section}%
%  {1}%
%  {0mm}%
%  {-.1\baselineskip}%
%  {.1\baselineskip}%
%  {\large\bfseries\uppercase}%
%}

\renewcommand{\section}{\@startsection
{section}%
{1}%
{0mm}%
{-.1\baselineskip}%
{.1\baselineskip}%
{\Large\bfseries}%
}

%\renewcommand\section{\@startsection
%{section}
%{1}
%{\z@}
%{-10\p@ \@plus -4\p@ \@minus -4\p@}
%{1\p@ \@plus 1\p@ \@minus 1\p@}
%{\normalfont\large}
%}

\renewcommand{\subsection}{\@startsection
{subsection}%
{2}%
{0mm}%
{-\baselineskip}%
{.1\baselineskip}%
{\bfseries\large\itshape}%
}

\renewcommand{\subsubsection}{\@startsection
{subsubsection}%
{3}%
{0mm}%
{-\baselineskip}%
{-\baselineskip}%
{\flushleft\normalsize}%
}

\renewcommand{\paragraph}{\@startsection
{paragraph}%
{4}%
{0mm}%
{-\baselineskip}%
{-\baselineskip}%
{\flushleft\normalsize}%
}

\renewcommand{\refname}{\flushleft\uppercase\bfseries REFERENCES}

%% Build title page
\def\@maketitle{%
\null
\parbox[t][70mm][t]{\textwidth}{%
\begin{flushright}%
\fontsize{14}{18}\selectfont%
\usebox{\numberbox}\\
\usebox{\titlebox}\\
\vskip 1 cm
\usebox{\authorbox}\\[2ex]%
\end{flushright}%
\vfill
\begin{flushleft}%
\end{flushleft}
}%b
}

% Boxes for the title
\newsavebox{\authorbox}
\savebox{\authorbox}{}
\newsavebox{\titlebox}
\newsavebox{\numberbox}

\savebox{\authorbox}{%
\parbox[t]{\textwidth}{%
\flushright%
\usebox{\authorbox}\\%
\fontfamily{\familydefault}\fontsize{12}{14}%
\fontseries{b}\fontshape{n}\selectfont%
#1\\%
{\fontfamily{\familydefault}\fontsize{10}{11}%
\fontseries{m}\fontshape{n}\selectfont%
#2}\fontsize{12}{12}\selectfont\\[2ex]%
}%
}
}
\newcommand{\PaperTitle}[1]{%
\savebox{\titlebox}{%
\parbox[t]{\textwidth}%
{\flushright%
\fontfamily{\familydefault}\fontsize{18}{22}%
\fontseries{b}\fontshape{n}\selectfont%
#1}%
}%
}
\newcommand{\PaperNumber}[1]{%
\savebox{\numberbox}{
\fontfamily{\familydefault}\fontsize{14}{18}%
\fontseries{b}\fontshape{n}\selectfont%
#1}%
}

{\fontfamily{\familydefault}\fontsize{8}{10}%
\fontseries{m}\fontshape{n}\selectfont%
}%
}


You can use Leo Liu's solution from I cannot get a (properly) underlined hyperlink in blue

\usepackage[normalem]{ulem}

\makeatletter
\DeclareUrlCommand\ULurl@@{%
\def\UrlLeft{\uline\bgroup}%
\def\UrlRight{\egroup}}
\DeclareRobustCommand*\ULurl{\hyper@normalise\ULurl@}
\makeatother

\DeclareFieldFormat{doi}{%
\newline
\ifhyperref
{\href{http://dx.doi.org/#1}{\ULurl{#1}}}
{\ULurl{#1}}}


Or without packages

\DeclareFieldFormat{doi}{%
\newline
`