
これは最近の投稿の続編です:余分なスペースなしで削除可能なコメントコマンドを作成する. 私は、一般的なことをやろうとしています。つまり、削除可能なコメント コマンドを作成して、ドキュメントの投稿者がお互いに、また自分自身にメモを残すことができるようにし、そのメモを制作前に自動的に削除できるようにすることです。問題は、ここで余分なスペースを生成するのが非常に簡単だということです。
前述の投稿の解決策は次のようになりました。
\newif\ifnotes
\makeatletter
\newcommand{\note}[1]{\ifnotes{#1}\else\@bsphack\@esphack\fi}
\makeatother
これはかなりうまく機能しますが、完璧ではありません。具体的には、2 つの音符が連続して出現する場合に余分なスペースが追加されます。たとえば、次の場合を考えてみましょう。
\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
適切に排除されていますいくつかの余分なスペースがなくなり、1 つの音符だけが残っている場合はうまく機能します。ただし、2 つの音符が隣接して表示される場合は、結果にさらに多くのスペースが生成されます。
私が検討した 1 つのアプローチは、\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}