该\mbox方法

该\mbox方法

我有时会引用两三篇参考文献。当引用位于行末时,LaTeX 会插入换行符。我该如何防止这种情况发生?

我已经尝试了建议的方法这里


笔记:我使用包natbib,如果不使用cite[nobreak]natbib我会得到想要的结果。但是,如果可能的话,我想继续使用natbib

这是一个 MWE(如果引用在您的机器上没有被破坏,请插入各种长度的随机乱码):

\documentclass{article}

\usepackage[numbers,sort]{natbib}
\makeatletter
\renewcommand*{\NAT@spacechar}{~}
\makeatother

\begin{document}

A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.


\begin{thebibliography}{9}

\bibitem{lamport94}
  Leslie Lamport,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{foo}
  Mr. Foo,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{bar}
  Mr. Bar,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\end{thebibliography}

\end{document}

答案1

这里描述的两种方法都取自Stefan Kottwitz 的回答关于python:使用natbib在\citet中不间断空格?

\mbox方法

您可以重新定义\cite为包裹\mbox

\let\oldcite\cite
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

或者,更好的是,使用letltxmacro封装(参见何时使用 \LetLtxMacro?

\usepackage{letltxmacro}
\LetLtxMacro{\oldcite}{\cite}
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

平均能量损失

\documentclass{article}

\usepackage[numbers,sort]{natbib}

\usepackage{letltxmacro}
\LetLtxMacro{\oldcite}{\cite}
\renewcommand{\cite}[1]{\mbox{\oldcite{#1}}}

\begin{document}

A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.

A shorter sentence, foo \cite{lamport94,foo,bar}.

\begin{thebibliography}{9}

\bibitem{lamport94}
  Leslie Lamport,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{foo}
  Mr. Foo,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{bar}
  Mr. Bar,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\end{thebibliography}

\end{document}

最小工作示例截图

\NAT@spacechar方法

我们不应该重新定义\NAT@spacechar为不可破坏的空间,而应该增加宏中的惩罚\NAT@separator(定义为,比使用[ ]\penalty\@m施加的惩罚更低)。因此,我们应该重新定义为\nolinebreak\@M\NAT@separator

\renewcommand{\NAT@separator}{\NAT@sep\nolinebreak}

平均能量损失

\documentclass{article}

\usepackage[numbers,sort]{natbib}

\makeatletter
\renewcommand{\NAT@separator}{\NAT@sep\nolinebreak}
\makeatother

\begin{document}

A really really really really really really really really long sentence, foo \cite{lamport94,foo,bar}.

A shorter sentence, foo \cite{lamport94,foo,bar}.

\begin{thebibliography}{9}

\bibitem{lamport94}
  Leslie Lamport,
  \emph{\LaTeX: a document preparation system},
gg  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{foo}
  Mr. Foo,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\bibitem{bar}
  Mr. Bar,
  \emph{\LaTeX: a document preparation system},
  Addison Wesley, Massachusetts,
  2nd edition,
  1994.

\end{thebibliography}

\end{document}

输出与方法中的相同\mbox

相关内容