即使在空白行后也强制一次性不缩进

即使在空白行后也强制一次性不缩进

我在下面的 MWE 中定义了一个注释宏。我希望它在注释后开始一个新行,没有缩进。如果我删除宏后的空白行,就不会有缩进,但我并不总是记得这样做。有没有办法强制一次性不缩进,以覆盖空白行的效果?谢谢任何建议。请注意,我想要全局强制不缩进(这很简单),只是在这个特定的例子中。

\documentclass{amsart}
\usepackage{lipsum}
\parindent=10 pt
\def\CommentCR#1{{\sf #1\leavevmode\newline\noindent}}
\begin{document}
\lipsum[1]
\CommentCR{I want no indentation even if I leave a space after this line.}

\lipsum[1]
\end{document}

答案1

即兴地,但我想有更优雅的解决方案:

\documentclass{amsart}
\usepackage{lipsum}
\parindent=10pt
\parskip=20pt

\makeatletter
\newcommand\CommentCR[1]{%
  % If you wish the comment not to form a paragraph on its own,
  % but to be part of the paragraph which LaTeX is about to create,
  % then uncomment "\ifhmode\else" and "\fi".
  %\ifhmode\else
    \par
    % If you don't wish vertical \parskip-gap to the next line,
    % uncomment "\vskip-\parskip".
    %\vskip-\parskip
    \noindent
  %\fi
  {\sffamily#1}\par
  % If you don't wish vertical \parskip-gap to the next line,
  % uncomment "\vskip-\parskip".
  %\vskip-\parskip
  \noindent
  \@ifnextchar\par{\@afterindentfalse\@afterheading\vskip-\parskip}{}%
}%
\makeatother

\begin{document}

\lipsum[1]
\CommentCR{I want no indentation even if I leave a space after this line.} 

\lipsum[1]

\lipsum[1]

\newpage

\lipsum[1]
\CommentCR{I want no indentation even if I leave a space after this line.}
\par\lipsum[1]

\lipsum[1]

\newpage

\lipsum[1]
\CommentCR{I want no indentation even if I leave a space after this line.}
\lipsum[1]

\lipsum[1]

\end{document}

答案2

您的规范非常不完整。这里有一组宏,用于检查出现的情况;如果是\par<space>\par,则\par\noindent发出 。否则什么也不做。

假设您不会玩恶意的伎俩并且您的输入就像您所展示的那样简单。

请注意,该功能\sf已被弃用超过 25 年。

\documentclass{amsart}
\usepackage{lipsum}

\setlength{\parindent}{10pt}

\makeatletter
\newcommand\CommentCR[1]{%
  {\sffamily#1}\suppress@start
}
\newcommand\suppress@start{%
  \futurelet\CR@a\suppress@parindent
}
\newcommand\suppress@parindent{%
  \ifx\CR@a\par
    \let\CR@b\suppress@par
  \else
    \let\CR@b\suppress@space
  \fi
  \CR@b
}
\newcommand{\suppress@par}[1]{\par\noindent}
\newcommand{\suppress@space}{%
  \ifx\CR@a\@sptoken
    \long\def\CR@c##1{\space\suppress@start##1}%
  \else
    \let\CR@c\relax
  \fi
  \CR@c
}
\makeatother

\begin{document}

\lipsum[1][1-3]
\CommentCR{I want no indentation even if I leave a space after this line.}%

\lipsum[1][1-3]

\lipsum[1][1-3]
\CommentCR{I want no indentation even if I leave a space after this line.}

\lipsum[1][1-3]

\lipsum[1][1-3]
\CommentCR{I want no indentation even if I leave a space after this line.}
\lipsum[1][1-3]

\lipsum[1][1-3]
\CommentCR{I want no indentation even if I leave a space after this line.}xyz
\lipsum[1][1-3]

\end{document}

在此处输入图片描述

答案3

也许你想要的是这个

\documentclass{amsart}
\usepackage{lipsum}
\parindent=10 pt
\def\CommentCR#1{{\sf #1\leavevmode\\\mbox{}\\}}
\begin{document}
\lipsum[1]
\CommentCR{I want no indentation even if I leave a space after this line.}
\lipsum[1]
\end{document}

相关内容