使用 \marginnote 不影响间距?

使用 \marginnote 不影响间距?

在大型文档中,我一直使用基于 \marginnote{} 的注释命令。现在我注意到,marginnote 对间距有意外的影响,例如,它会导致章节的第一段缩进,而实际上不应该缩进。

原则上,我可以重新定义我的命令以始终添加\leavevmode,但这样我就必须追踪该命令的所有实例。另外,我的宏的行为会根据 而有所不同\ifvmode(请参阅 MWE)。我不能使用\marginpar,因为命令已在浮点中使用,并且由于某种原因,如果我根据 选择 marginpar 而不是 marginnote ,会导致“丢失浮点” \ifinner

是否有某种方法可以换行、重新定义或替换 marginnote ,以使其不干扰段落缩进或间距,并且不需要更改整个文档中命令的调用方式?

有一些相关的问题,但它们最终都以需要改变整个文档的调用的方式得到了回答。

平均能量损失

在此处输入图片描述

\makeatletter
\documentclass[draft]{article}

\usepackage{marginnote}
\usepackage{ifdraft}
\usepackage{lipsum}

\newif\if@use@leavevmode@hack

\newcounter{commentnote}
\newcommand{\commentnote}[2][0pt]{%
  \newif\if@cnmark
  \unless\ifvmode
    \unskip
    \@cnmarktrue
    \stepcounter{commentnote}%
  \else
    \if@use@leavevmode@hack
      \leavevmode
    \fi
  \fi
  \ifdraft{%
    \if@cnmark\textsuperscript{\thecommentnote}\fi
    \marginnote{\if@cnmark\textsuperscript{\thecommentnote}\,\fi#2}[#1]%
  }{}%
}

\begin{document}

\section{Without \textbackslash leavevmode}

\sloppy

\commentnote{Note (1)}
This text should not be indented.

The \verb|\unless\ifvmode\unskip| 
was introduced to allow 
  \commentnote{Note(2)}
typesetting notes on separate lines without adding  
\% characters at the end of each preceding line,
and in order to use \verb|\commentnote|
in vertical mode to create unmarked comments.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}

It is acceptable to use multiple notes before a paragraph.
Inserting \verb|\leavevmode| would cause spurious numbering.


\section{With \textbackslash leavevmode}
\@use@leavevmode@hacktrue

\commentnote{Foo}%
Using \verb|\leavevmode| leads to correct indentation, 
but then requires manually removing newlines between
\verb|\commentnote| and paragraphs, and adding \%
characters at the end of each invocation.

\commentnote{Note(3)}%
\commentnote[\baselineskip]{Note (4)}%
\commentnote[2\baselineskip]{Note (5)}%
Furthermore, it leads to spurious numbering of out-of-paragraph notes.

\end{document}

答案1

在此处输入图片描述

\makeatletter
\documentclass[draft]{article}

\usepackage{marginnote}
\usepackage{ifdraft}
\usepackage{lipsum}

\newtoks\zzz
  \newif\if@cnmark

\newcounter{commentnote}
\newcommand{\commentnote}[2][0pt]{%
  \ifvmode
\@cnmarkfalse
   \else
    \unskip
    \@cnmarktrue
    \refstepcounter{commentnote}%
  \fi
  \ifdraft{%
    \if@cnmark\textsuperscript{\thecommentnote}\fi
    {\let\everypar\zzz\marginnote{\if@cnmark\textsuperscript{\thecommentnote}\,\fi#2}[#1]%
  }{}}%
\ignorespaces}

\begin{document}

\section{Without \textbackslash leavevmode}

\sloppy

\commentnote{Note (1)}
This text should not be indented.

The \verb|\unless\ifvmode\unskip| 
was introduced to allow 
  \commentnote{Note(2)}
typesetting notes on separate lines without adding  
\% characters at the end of each preceding line,
and in order to use \verb|\commentnote|
in vertical mode to create unmarked comments.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}

It is acceptable to use multiple notes before a paragraph.
Inserting \verb|\leavevmode| would cause spurious numbering.


\section{With \textbackslash leavevmode}


\commentnote{Foo}
Using \verb|\leavevmode| leads to correct indentation, 
but then requires manually removing newlines between
\verb|\commentnote| and paragraphs, and adding \%
characters at the end of each invocation.

\commentnote{Note(3)}
\commentnote[\baselineskip]{Note (4)}
\commentnote[2\baselineskip]{Note (5)}
Furthermore, it leads to spurious numbering of out-of-paragraph notes.

\end{document}

相关内容