这是最近一篇文章的后续:创建不带额外空格的可移动注释命令。我正在尝试做一件常见的事情:制作一个可移除的注释命令,以便文档的贡献者可以互相和自己留下注释,但注释可以在生产前自动删除。问题是这里很容易产生额外的空间。
上述帖子中的解决方案是这样的:
\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{\ifnotes{#1}\else\@bsphack\@esphack\fi}
\makeatother
它运行得很好,但并不完美。具体来说,如果两个音符连续出现,则会添加额外的空间。例如,考虑:
\notesfalse
Testing \note{X} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X} \note{Y} testing testing.
\notestrue
Testing \note{X} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X} \note{Y} testing testing.
以上结果:
我们可以看到\@bsphack\@esphack
,一些冗余空间,如果只剩下一个音符,效果会很好。但如果两个音符相邻,结果中会产生更多空间。
我考虑过的一种方法是,命令\note
可以确定它位于另一个音符旁边,然后避免发出额外的\@bsphack\@esphack
。不过,我过去尝试构建类似的命令,但效果不佳。有人遇到过这样的情况吗?我的搜索一无所获。
谢谢!
答案1
问题在于\@esphack
既检查寄存器的值,然后又改变它\lastskip
。
因此,\@esphack
先前的实例会影响 连续实例的运作\note
方式。\@esphack
\note
\@esphack
某些“向前和向后跳过”的变体可能会有所帮助。
真挚地
乌尔里希
\documentclass{article}
\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{%
\@bsphack
%=== instead of \@esphack: ===
% \showthe\lastskip
\relax
\ifhmode
\spacefactor\@savsf
\ifdim\@savsk>\z@
\nobreak
\hskip\z@skip
% The previous action will change \lastskip, so:
\hskip-\@savsk
\hskip\@savsk
% now \lastskip is almost \@savsk again.
% \showthe\lastskip
\ignorespaces
\fi
\fi
%===========================
\ifnotes #1\fi
}
\makeatother
\begin{document}
\notesfalse
Testing\note{X}testing testing.
Testing \note{X}testing testing.
Testing\note{X} testing testing.
Testing \note{X} testing testing.
Testing\note{X}\note{Y}testing testing.
Testing\note{X}\note{Y} testing testing.
Testing\note{X} \note{Y}testing testing.
Testing\note{X} \note{Y} testing testing.
Testing \note{X} \note{Y}testing testing.
Testing \note{X} \note{Y} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X}\note{Y}testing testing.
\notestrue
Testing\note{X}testing testing.
Testing \note{X}testing testing.
Testing\note{X} testing testing.
Testing \note{X} testing testing.
Testing\note{X}\note{Y}testing testing.
Testing\note{X}\note{Y} testing testing.
Testing\note{X} \note{Y}testing testing.
Testing\note{X} \note{Y} testing testing.
Testing \note{X} \note{Y}testing testing.
Testing \note{X} \note{Y} testing testing.
Testing \note{X}\note{Y} testing testing.
Testing \note{X}\note{Y}testing testing.
\end{document}