典型错误:xspace 总是向前看,从不向后看

典型错误:xspace 总是向前看,从不向后看

我们如何解释这两个输出之间的差异,其中第二个输出}紧随其后\xspace

平均能量损失

\documentclass{article}

\usepackage{xspace}

\usepackage[citestyle=authoryear]{biblatex}

\newcommand{\coolxspaceunprotected}{\ifhmode\unskip\xspace\fi}
\newcommand{\coolxspaceprotected}{{\ifhmode\unskip\xspace\fi}} % How to avoid the braces?

\newcommand{\spacetciteunprotected}{\coolxspaceunprotected\textcite}
\newcommand{\spacetciteprotected}{\coolxspaceprotected\textcite}

\begin{filecontents}{\jobname.bib}
    
    @book{smith96,
        author = {Smith, Adam},
        year = {1996},
        title = {Economy},
        publisher = {My company} 
    }
    
\end{filecontents}

\addbibresource{\jobname.bib}



\begin{document}

\section{\spacetciteunprotected{smith96} and the blob hypothesis}

\section{\spacetciteprotected{smith96} and the blob hypothesis}

\end{document}

在此处输入图片描述

答案1

在此处输入图片描述

您的主要问题是您在 中添加了一个空格hmode,但就在 parindent(或 sec number)框之后,您hmode不想有空格。\xspace在这里没有帮助。你想看看后退不是向前,这避免在(节点类型 1)之后添加空格,hbox因此它会在字符之后添加空格。

\documentclass{article}

\usepackage[citestyle=authoryear]{biblatex}

\DeclareRobustCommand\coolspaceunprotected{%
\ifhmode\ifnum\lastnodetype=1 \else\unskip\unskip\space
\fi\fi}


\DeclareRobustCommand\spacetciteunprotected{\coolspaceunprotected\textcite}



\begin{filecontents}{\jobname.bib}
    
    @book{smith96,
        author = {Smith, Adam},
        year = {1996},
        title = {Economy},
        publisher = {My company} 
    }
    
\end{filecontents}

\addbibresource{\jobname.bib}



\begin{document}

\tableofcontents

\section{\spacetciteunprotected{smith96} and the blob hypothesis}
a

\section{\textcite{smith96} and the blob hypothesis}
b
\section{Foo,\spacetciteunprotected{smith96} and the blob hypothesis}
c

\section{Foo,\textcite{smith96} and the blob hypothesis}
d

\end{document}
 

相关内容