1

Requirements:

  • Underlined URLs in a bibliography built from an external file.
  • Margins are respected. No URLs go into the margins.
  • The final output is a pdf file.
  • Other format requirements still apply, like the KOMA class and such. This is for meeting thesis formatting requirements.

I would like to have underlined URLs with line breaks. I have tried a couple of approaches:

  1. Using the packages url and ulem. This gives underlining but the page margins are not respected. I.e., no line breaks.

    \usepackage{url}
    \urlstyle{same}%Use the same font and style as the main document.
    \usepackage[normalem]{ulem}
    \def\UrlLeft#1\UrlRight{%
        \expandafter\uline\expandafter{#1}
    }
    
  2. I have tried using the soul package with the \ul command. That did not work.

  3. I tried inserting the \ul, and \uline command in the url field in the bib entry in the external .bib file.

Sample bib:

% Encoding: UTF-8

@Misc{Author,
  author =       {Some Author},
  title =        {A Title for a website},
  howpublished = {\url{https://AVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURL}},
  note =         {(Accessed on 10/07/2017)},
  review =       {my Review},
  url =          {https://AVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURLAVeryLongURL}
}

MWE

\documentclass[
oneside,
12pt,
numbers=noenddot,
listof=entryprefix,
listof=totoc,
bibliography=totoc,%add bibliography to table of contents
index=totoc,
toc=indent,
paper=8.5in:11in,DIV=calc %Set the paper size
]{scrbook}[2017/01/03]%I have chosen to use the KOMA class.


\usepackage[utf8]{inputenc}
\usepackage{geometry}%This is for the margins and footer placement.
\geometry{margin=1in,%Each page, except major headings gets a one inch margin.
    footskip=\dimexpr.25in+\dp\strutbox\relax %Place the footer / page number .75in from bottom of page.
}

\usepackage[english]{babel}%For bibliography language fonts to be converted to English

\usepackage{lipsum}%Sample text generator
\usepackage{graphicx} %Will be used in final product.
\graphicspath{{../figures/}}%Directory all figures will be found in.
\usepackage{scrhack}

%Page Numbering
\usepackage[automark]{scrlayer-scrpage}
\KOMAoptions{onpsinit={\linespread{1}\selectfont}}%Double spacing KOMA style.
\clearpairofpagestyles
\cfoot*{\pagemark} %Page numbers in the footer area, center of page.

%Line overlay for measuring 
\usepackage{tikz}
%Page Margine .75inch from bottom of page.
%\ofoot*{\tikz[remember picture,overlay]\draw[red](current page.south)--+(0,.75in);}
%Two inch margin marker, every page
%\ohead*{\tikz[remember picture, overlay]\draw[red](current page.north)--+(0, -2in);}

\usepackage[all]{nowidow}%Try and prevent orphaned lines


%Chapter Heading formatting.
\RedeclareSectionCommand[
beforeskip=1in plus  \parskip ,%1inch margin plus one more inch for major headings.
%Major headings need a 2 inch margin.
innerskip=0pt,
afterskip=.5\baselineskip plus 0\baselineskip minus 0\baselineskip,
font=\normalfont\bfseries
]{chapter}
\renewcommand\raggedchapter{\centering}
\renewcommand\chapterformat{\MakeUppercase{\chaptername} \thechapter:\ }
\renewcommand\chapterlinesformat[3]{#2\MakeUppercase{#3}}

%Table of Contents formatting
\usepackage{xpatch}
\xpatchcmd{\addchaptertocentry}{%
    \addtocentrydefault{chapter}{#1}{#2}%
}{% Our numberless chapters still need a protect tag.
    \ifstr{#1}{}
    {\addtocentrydefault{chapter}{#1}{\protect\chaptertocentryformat{#2}}}
    {\addtocentrydefault{chapter}{CHAPTER #1:}{\protect\chaptertocentryformat{#2}}}
}{}{\PatchFailed}


%Resize the spacing on the table of contents number width.
\RedeclareSectionCommand[
tocdynnumwidth
]{chapter}

\newcommand\chaptertocentryformat[1]{#1}
\AfterTOCHead[toc]{\renewcommand\chaptertocentryformat[1]{\MakeUppercase{#1}}}
\addtokomafont{chapterentry}{\normalfont}

%End Page in Table of Contents.
\newcommand\EndPageInToC{%
    \addtocontents{toc}{\RedeclareSectionCommand[tocpagenumberbox=\protect\EndPageBox]{chapter}}%
    \renewcommand\chapterpagestyle{empty}%
}
\newcommand\EndPageBox[1]{\makebox{END PAGE}}

%Formatting for sections and lower levels.
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=.5in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{section}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=1in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subsection}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=1.5in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subsubsection}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=2in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{paragraph,subparagraph}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=2in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subparagraph}


%\@tocrmarg This is related to the column for the page number.
\makeatletter
%\def\@pnumwidth{1in}
\def\@tocrmarg{1.5in}%TOC entries will give the page numbers 1.5 inches of space.
\makeatother

%Formating for List of figures, and tables. 
\DeclareTOCStyleEntry[
linefill=\hfill,
beforeskip=1\baselineskip plus 0\baselineskip minus 0\baselineskip%Trying to get double spacing out of entries.
]{tocline}{figure}

\DeclareTOCStyleEntry[
linefill=\hfill
beforeskip=1\baselineskip plus 0\baselineskip minus 0\baselineskip%Trying to get double spacing out of entries.
]{tocline}{table}

\usepackage{setspace}%I think this is used for line spacing.

%Rename the TOC and the Bibliography.
\renewcaptionname{english}{\contentsname}{Table of Contents}
\renewcaptionname{english}{\bibname}{References}

\setlength\parindent{.5in}%Paragraph indents are a half inch.
\usepackage{listings}

%Algorithms formatting
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx


\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}
\newcommand*\DNA{\textsc{dna}}

%Bullet List formatting, all have the same symbol
\renewcommand{\labelitemi}{$\bullet$}
\renewcommand{\labelitemii}{$\bullet$}
\renewcommand{\labelitemiii}{$\bullet$}
\renewcommand{\labelitemiv}{$\bullet$}


\usepackage{url} %For bibliography, the URL tag will not line break in a crazy way
\urlstyle{same}%Use the same font and style as the main document.

%\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks%  save the current one
%   \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
%   \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
%   \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
%   \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
%   \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
%   \do\Y\do\Z}


%\usepackage[%hidelinks,
% colorlinks=false,urlcolor=black]{hyperref}
%\hypersetup{breaklinks=true,
%       %,colorlinks=false,
%   pdfborderstyle={/S/U/W 1},
%   pdfborder=0 0 0
%}

%Underline all URLS 
\usepackage{soul}
%\def\UrlLeft#1\UrlRight{%
%   \ul{#1}
%}

%\makeatletter
%\patchcmd{\hyper@link@}
%{{\Hy@tempb}{#4}}
%{{\Hy@tempb}{\uline{#4}}}
%{}{}
%\makeatother

%Underlined, but no line breaks
\usepackage[normalem]{ulem}
\def\UrlLeft#1\UrlRight{%
    \expandafter\uline\expandafter{\expandafter#1\expandafter}\expandafter
}



%This did not work.
\usepackage[anythingbreaks]{breakurl}


%% Set basic information about the thesis
\title{A custom Title}

\begin{document}
    \begin{titlepage}
        A custom Title
    \end{titlepage}

    \addchap*{Dedication}
    \pagenumbering{gobble}
    \doublespacing
    \lipsum[1]  

    \addchap*{Acknowledgments}
    \pagenumbering{gobble}
    \doublespacing
    \lipsum[1]

    \frontmatter    
    \singlespacing
    \listoftables
    \listoffigures
    \tableofcontents

    \doublespacing
    \chapter{Abstract}
    \lipsum[1]
    \mainmatter
    \chapter{A VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, Long Chapter}
    \lipsum[1]

    \chapter{Another Chapter}
    \lipsum[1]  \cite{Author}
    \section{Some Section}
    \lipsum*
    \begin{algorithm}
        \caption{Counting mismatches between two packed \DNA strings
            \label{alg:packed-dna-hamming}}
        \begin{algorithmic}[1]
            \Require{$x$ and $y$ are packed \DNA strings of equal length $n$}
            \Statex
            \Function{Distance}{$x, y$}
            \Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
            \Let{$\delta$}{$0$}
            \For{$i \gets 1 \textrm{ to } n$}
            \If{$z_i \neq 0$}
            \Let{$\delta$}{$\delta + 1$}
            \EndIf
            \EndFor
            \State \Return{$\delta$}
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
    \lipsum[1]
    \subsection{Some SUB Section}
    \lipsum[1]
    \begin{figure}
        \centering
        \includegraphics[width=0.5\textwidth]{FIG_PoliciesAsSets}
        \caption{Sample Figure}
        \label{fig:FIG_PoliciesAsSets}
    \end{figure}


    Reference Algorithm  \ref{alg:packed-dna-hamming} for the steps.

    \bibliographystyle{ieeetr}%chosen style for bibliography. I require underlined URLS.
    \bibliography{testref}%Really want to use and external bib file.

    \backmatter
    \EndPageInToC
    \chapter{About the Author}
    \lipsum[1]

\end{document}
  • 1
    Just out of idle curiosity: What's the purpose of underlying URL strings? (I haven't seen such formatting in a dozen years or more...) – Mico Oct 7 '17 at 23:44
  • Package hyperref can draw a line under the link, or a whole box around it (default ctaually). This box or line will not be printed, it is just for highlighting the link in the pdf. Nobody needs a highlighted link in a printed version. – Johannes_B Oct 8 '17 at 6:29
  • Your best chance of not going out of the margin is to put \raggedright before the bibliography. It is quite common to do so. – Johannes_B Oct 8 '17 at 6:32
1

I ended up going with a href solution. The key take away for me was to set up the href with hidelinks then set the hyper style right before the bibliography. The underlines are not as pretty as \underline, \ul, or \uline. But I am pretty sure it meet the formatting requirements for my thesis.

Here is the MWE that I ended up with.

\documentclass[
oneside,
12pt,
numbers=noenddot,
listof=entryprefix,
listof=totoc,
bibliography=totoc,%add bibliography to table of contents
index=totoc,
toc=indent,
paper=8.5in:11in,DIV=calc %Set the paper size
]{scrbook}[2017/01/03]%I have chosen to use the KOMA class.


\usepackage[utf8]{inputenc}
\usepackage{geometry}%This is for the margins and footer placement.
\geometry{margin=1in,%Each page, except major headings gets a one inch margin.
    footskip=\dimexpr.25in+\dp\strutbox\relax %Place the footer / page number .75in from bottom of page.
}

\usepackage[english]{babel}%For bibliography language fonts to be converted to English

\usepackage{lipsum}%Sample text generator
\usepackage{graphicx} %Will be used in final product.
\graphicspath{{../figures/}}%Directory all figures will be found in.
\usepackage{scrhack}

%Page Numbering
\usepackage[automark]{scrlayer-scrpage}
\KOMAoptions{onpsinit={\linespread{1}\selectfont}}%Double spacing KOMA style.
\clearpairofpagestyles
\cfoot*{\pagemark} %Page numbers in the footer area, center of page.

%Line overlay for measuring 
\usepackage{tikz}
%Page Margine .75inch from bottom of page.
%\ofoot*{\tikz[remember picture,overlay]\draw[red](current page.south)--+(0,.75in);}
%Two inch margin marker, every page
%\ohead*{\tikz[remember picture, overlay]\draw[red](current page.north)--+(0, -2in);}

\usepackage[all]{nowidow}%Try and prevent orphaned lines


%Chapter Heading formatting.
\RedeclareSectionCommand[
beforeskip=1in plus  \parskip ,%1inch margin plus one more inch for major headings.
%Major headings need a 2 inch margin.
innerskip=0pt,
afterskip=.5\baselineskip plus 0\baselineskip minus 0\baselineskip,
font=\normalfont\bfseries
]{chapter}
\renewcommand\raggedchapter{\centering}
\renewcommand\chapterformat{\MakeUppercase{\chaptername} \thechapter:\ }
\renewcommand\chapterlinesformat[3]{#2\MakeUppercase{#3}}

%Table of Contents formatting
\usepackage{xpatch}
\xpatchcmd{\addchaptertocentry}{%
    \addtocentrydefault{chapter}{#1}{#2}%
}{% Our numberless chapters still need a protect tag.
    \ifstr{#1}{}
    {\addtocentrydefault{chapter}{#1}{\protect\chaptertocentryformat{#2}}}
    {\addtocentrydefault{chapter}{CHAPTER #1:}{\protect\chaptertocentryformat{#2}}}
}{}{\PatchFailed}


%Resize the spacing on the table of contents number width.
\RedeclareSectionCommand[
tocdynnumwidth
]{chapter}

\newcommand\chaptertocentryformat[1]{#1}
\AfterTOCHead[toc]{\renewcommand\chaptertocentryformat[1]{\MakeUppercase{#1}}}
\addtokomafont{chapterentry}{\normalfont}

%End Page in Table of Contents.
\newcommand\EndPageInToC{%
    \addtocontents{toc}{\RedeclareSectionCommand[tocpagenumberbox=\protect\EndPageBox]{chapter}}%
    \renewcommand\chapterpagestyle{empty}%
}
\newcommand\EndPageBox[1]{\makebox{END PAGE}}

%Formatting for sections and lower levels.
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=.5in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{section}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=1in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subsection}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=1.5in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subsubsection}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=2in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{paragraph,subparagraph}
\RedeclareSectionCommands[
font=\normalfont,
toclinefill=\hfill,
tocindent=2in,
beforeskip=0\baselineskip plus  -\parskip,
afterskip=1sp minus 1sp
]{subparagraph}


%\@tocrmarg This is related to the column for the page number.
\makeatletter
%\def\@pnumwidth{1in}
\def\@tocrmarg{1.5in}%TOC entries will give the page numbers 1.5 inches of space.
\makeatother

%Formating for List of figures, and tables. 
\DeclareTOCStyleEntry[
linefill=\hfill,
beforeskip=1\baselineskip plus 0\baselineskip minus 0\baselineskip%Trying to get double spacing out of entries.
]{tocline}{figure}

\DeclareTOCStyleEntry[
linefill=\hfill
beforeskip=1\baselineskip plus 0\baselineskip minus 0\baselineskip%Trying to get double spacing out of entries.
]{tocline}{table}

\usepackage{setspace}%I think this is used for line spacing.

%Rename the TOC and the Bibliography.
\renewcaptionname{english}{\contentsname}{Table of Contents}
\renewcaptionname{english}{\bibname}{References}

\setlength\parindent{.5in}%Paragraph indents are a half inch.
\usepackage{listings}

%Algorithms formatting
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx


\newcommand*\Let[2]{\State #1 $\gets$ #2}
\algrenewcommand\algorithmicrequire{\textbf{Precondition:}}
\algrenewcommand\algorithmicensure{\textbf{Postcondition:}}
\newcommand*\DNA{\textsc{dna}}

%Bullet List formatting, all have the same symbol
\renewcommand{\labelitemi}{$\bullet$}
\renewcommand{\labelitemii}{$\bullet$}
\renewcommand{\labelitemiii}{$\bullet$}
\renewcommand{\labelitemiv}{$\bullet$}

\usepackage{url} %For bibliography, the URL tag will not line break in a crazy way
\urlstyle{same}%Use the same font and style as the main document.

%I need flush right margins. This did the trick. But is highly discouraged.
\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks%  save the current one
    \do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j%
    \do\k\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t%
    \do\u\do\v\do\w\do\x\do\y\do\z\do\A\do\B\do\C\do\D%
    \do\E\do\F\do\G\do\H\do\I\do\J\do\K\do\L\do\M\do\N%
    \do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V\do\W\do\X%
    \do\Y\do\Z}


\usepackage[hidelinks]{hyperref}

%% Set basic information about the thesis
\title{A custom Title}

\begin{document}
    \begin{titlepage}
        A custom Title
    \end{titlepage}

    \addchap*{Dedication}
    \pagenumbering{gobble}
    \doublespacing
    \lipsum[1]  

    \addchap*{Acknowledgments}
    \pagenumbering{gobble}
    \doublespacing
    \lipsum[1]

    \frontmatter    
    \singlespacing
    \listoftables
    \listoffigures
    \tableofcontents

    \doublespacing
    \chapter{Abstract}
    \lipsum[1]
    \mainmatter
    \chapter{A VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, VERY, Long Chapter}
    \lipsum[1]

    \chapter{Another Chapter}
    \lipsum[1]  Cite some author \cite{Author}.
    \section{Some Section}
    \lipsum*
    \begin{algorithm}
        \caption{Counting mismatches between two packed \DNA strings
            \label{alg:algo1}}
        \begin{algorithmic}[1]
            \Require{$x$ and $y$ are packed \DNA strings of equal length $n$}
            \Statex
            \Function{Distance}{$x, y$}
            \Let{$z$}{$x \oplus y$} \Comment{$\oplus$: bitwise exclusive-or}
            \Let{$\delta$}{$0$}
            \For{$i \gets 1 \textrm{ to } n$}
            \If{$z_i \neq 0$}
            \Let{$\delta$}{$\delta + 1$}
            \EndIf
            \EndFor
            \State \Return{$\delta$}
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
    \lipsum[1]
    \subsection{Some SUB Section}
    \lipsum[1]
    \begin{figure}
        \centering
        \includegraphics[width=0.5\textwidth]{fig1}
        \caption{Sample Figure}
        \label{fig:fig1}
    \end{figure}


    Reference Algorithm \ref{alg:algo1} for the steps.

    %When I want to underline:
    \hypersetup{
        breaklinks=true,
        colorlinks=false,%Use boxes instead of colored links.
        pdfborderstyle={/S/U/W 1},%Use uderline box instead of default rectangle
        pdfborder=0 0 1,%line width, a non zero value will give an underline
        urlbordercolor=black
    }
    \singlespacing
    \bibliographystyle{ieeetr}%chosen style for bibliography. I require underlined URLS.
    \bibliography{testref}%Really want to use and external bib file.

    \backmatter
    \EndPageInToC
    \doublespacing
    \chapter{About the Author}
    \lipsum[1]

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.