多次空間駭客創造更多空間

多次空間駭客創造更多空間

這是最近一篇文章的後續內容:建立沒有額外空間的可移動註解命令。我正在嘗試做一件常見的事情:創建一個可刪除的註釋命令,以便文件的貢獻者可以為彼此和自己留下註釋,但可以在生成之前自動刪除註釋。問題是這裡很容易產生額外的空間。

上述帖子中的解決方案結果是這樣的:

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

相關內容