使用 PDFLaTeX 和 hyperref 在 BibTeX 中插入连字点

使用 PDFLaTeX 和 hyperref 在 BibTeX 中插入连字点

如何告诉 PDFLaTeX 在我想要的位置进行连字?

我有我的文档文件:

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\begin{document}

This is text with \cite{reference}.

\bibliographystyle{unsrt}
\bibliography{bib_file}

\end{document}

bib_file.bib文件:

@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html\#concurrency}},
    note = {Last access: 12.03.2013},
}

建立该文件后,新行将位于“。”之后:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.
2nd line: html#concurrency. Last access: 12.03.2013.

但我想在其他地方插入连字符点,例如:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

甚至:

1st line: Some dummy title in bib. http://docs.oracle.com/javase/1.5.0/docs/ <-/
2nd line: relnotes/features.html#concurrency. Last access: 12.03.2013.

<-/- 是个进入箭。

我尝试在 URL 中插入{\-}和,但它会在该位置添加文本\-\discretionary{- }{}{}

答案1

解决方案在这个答案中\UrlBreaks已经展示了如何通过指定、\UrlBigBreaks和来微调 URL 的换行\UrlNoBreaks

为了在断点处获得回车符,我们将断点指定为\UrlSpecials,其中包含在那里发生换行时的符号。

\usepackage{dingbat}  % for \carriagereturn symbol
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}
\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\.{\mathchar`\.\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on

为了手动指定断点,我们使用一个希望不会出现在任何 URL 中的符号,例如星号*,并使其成为类似于上述符号的断点 URL,但从字符串中消失:

\renewcommand{\UrlSpecials}{\do\*{\urllb}}

为了使用此解决方案获得正确的链接,还需要从链接中过滤掉星号。我们使用华丽的Qrrbrbirlbel 的解决方案

\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

当然,这两种解决方案可以混合使用,即,我们可以将正常的断点指定为\UrlSpecials,以及将星号指定为额外的手动断点。

完整代码:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#*concur*rency}},
    note = {Last access: 12.03.2013},
}
\end{filecontents*}
\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}
\usepackage[margin=2.5cm]{geometry}

\usepackage{etoolbox}
\usepackage{dingbat}  % for \carriagereturn symbol
\newcommand{\urllb}{\discretionary{\carriagereturn}{}{}}
%% original breaking points of url package
% \def\UrlBreaks{\do\.\do\@\do\\\do\/\do\!\do\_\do\|\do\;\do\>\do\]%
%  \do\)\do\,\do\?\do\'\do+\do\=\do\#}%
% \def\UrlBigBreaks{\do\:\do@url@hyp}%

\makeatletter
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother

\renewcommand{\UrlBreaks}{}    % no standard breaking points
\renewcommand{\UrlBigBreaks}{}
\renewcommand{\UrlSpecials}{\do\_{\mathchar`\_\urllb}%
                            \do\/{\mathchar`\/\urllb}%
                            \do\@{\mathchar`\@\urllb}%
                            \do\\{\mathchar`\\\urllb}%
                            \do\-{\mathchar`\-\urllb}%
                            \do\#{\mathchar`\#\urllb}}% and so on
% \renewcommand{\UrlSpecials}{\do\*{\urllb}}

%% solution of Qrrbrbirlbel, see https://tex.stackexchange.com/q/103870/21591
\makeatletter
\def\strip@star#1*#2\@strip@star{%
    \expandafter\def\expandafter\strip@star@result\expandafter{\strip@star@result#1}%
    \if\relax\detokenize{#2}\relax\else
        \def\strip@star@temp{#2}%
        \expandafter\expandafter\expandafter\strip@star\expandafter\strip@star@temp
        \expandafter\@strip@star
    \fi
}
\patchcmd{\hyper@linkurl}{\Hy@pstringdef\Hy@pstringURI{#2}}{%
    \def\strip@star@result{}%
    \expandafter\strip@star#2*\@strip@star
    \Hy@pstringdef\Hy@pstringURI{\strip@star@result}%
}{}{}
\makeatother

\parindent0pt
\begin{document}
This is text with \cite{reference}.

Automatic breaking with carriage return symbol:

Text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

Text text text text text text text text text text text text
\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html#concurrency}

\bigskip

\renewcommand{\UrlSpecials}{\do\*{\urllb}}
Hand tuned URL breaking:

Text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

Text text text text text text text text text text text text
\url{http://docs.*oracle.com/*javase/*1.5.0/*docs/*rel*notes/*fea*tures.html#concur*rency}

\bibliographystyle{unsrt}
\bibliography{faramir2}
\end{document}

产量

示例输出

向 Qrrbrbirlbel 致敬,感谢他解决了最困难的部分。

答案2

如果您加载包hyperref或包urlLaTeX 能够找到正确的断点,以便进行无误的连字符连接。因此,经常会发生在对齐文本中出现大空白的情况。在 URL 中插入额外的符号-来标记连字符不是一个好主意。读者不知道 是-属于 URL 还是仅仅是一个连字符符号。URL 最好在 之后连字符连接/.等等。

我建议使用包ragged2e并使用命令\raggedright来获取左对齐的参考书目。我通过这种方式制作的所有参考书目看起来都很好!

编辑:我用从此处复制的一段代码更新了 MWE问题并以您想要的方式在 URL 中获取换行符。但请记住:使用此解决方案,LaTeX 会决定使用特殊字符来中断 URL,而不是您。

尝试一下这个 MWE:

\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@misc{reference,
    title = {{Some dummy title in bib.}},
    howpublished = {\url{http://docs.oracle.com/javase/1.5.0/docs/relnotes/features.html\#concurrency}},
    note = {Last access: 12.03.2013},
}
\end{filecontents*}

\documentclass[10pt,a4paper]{article}

\PassOptionsToPackage{hyphens}{url}
\usepackage{hyperref}

%https://tex.stackexchange.com/questions/3033/forcing-linebreaks-in-url?rq=1
\makeatletter
\g@addto@macro{\UrlBreaks}{\UrlOrds}
\g@addto@macro{\UrlNoBreaks}{\do\.} % do not break after dot '.'
\makeatother

\usepackage[%
  newcommands     % \RaggedRight=\raggedright etc. 
 ,newparameters   % use default settings of ragged2e
]{ragged2e}
\usepackage[margin=2.5cm]{geometry}

\begin{document}

This is text with \cite{reference}.

\bibliographystyle{unsrt}
{\raggedright  % group to end left justification after bib
\bibliography{\jobname}
}              % ends group for left justified bibliography

\end{document}

相关内容