格式化的书目条目中带下划线且带换行符的 URL

格式化的书目条目中带下划线且带换行符的 URL

要求:

  • 从外部文件构建的参考书目中的带下划线的 URL。
  • 尊重页边距。URL 不得进入页边距。
  • 最终输出是一个pdf文件。
  • 其他格式要求仍然适用,例如 KOMA 类等。这是为了满足论文格式要求。

我希望 URL 带有下划线和换行符。我尝试了几种方法:

  1. 使用软件包urlulem。这会产生下划线,但不考虑页边距。即没有换行符。

    \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. 我尝试过soul通过命令使用该包\ul。但没有效果。

  3. 我尝试在外部文件的 bib 条目中的字段中插入\ul、和\uline命令。url.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}
}

平均能量损失

\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

我最终选择了 href 解决方案。对我来说,关键是使用 hidelinks 设置 href,然后在参考书目之前设置超样式。下划线不如 \underline、\ul 或 \uline 漂亮。但我很确定它符合我的论文的格式要求。

这是我最终得到的 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.

%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}

相关内容