如何设置一个新环境来通过标志跳过单词?

如何设置一个新环境来通过标志跳过单词?

我想创建一个环境来包含一些单词,但在设置标志时隐藏它。但是,出于某种原因,我无法放入\begin{comment} \end{comment}\newenvironment我该怎么做?

\newenvironment{FlagText}{\if\mycmd1 \begin{comment} \fi}%
{\if\mycmd1 \end{comment}\fi}

答案1

因为您只想省略一个词左右,所以我认为普通命令比环境更好。这是一种可能性。

\documentclass{article}
\newif\ifmycmd
\newcommand{\FlagText}[1]{%
  \ifhmode\unskip\fi
  \ifmycmd \else \space #1\fi
  }

\begin{document}

Here is some text \FlagText{with some words} to be ignored.

\FlagText{Start} a new paragraph with the first word flagged.

\mycmdtrue

Here is some text \FlagText{with some words} to be ignored.

\FlagText{Start} a new paragraph with the first word flagged.

\end{document}

示例代码的输出

避免\unskip可能出现的混乱间距,并且它应用于省略之前,以便当下一行的第一个单词被省略时,不会在行尾无意中留下空格。

或许使用\@bsphack\@esphack(通常的 LaTeX 方式使命令“透明”)的实现会更好。

\documentclass{article}
\newif\ifmycmd

\makeatletter
\newcommand{\FlagText}[1]{%
  \ifmycmd
    \@bsphack\expandafter\@esphack
  \else
    #1%
  \fi
}
\makeatother

\begin{document}

Here is some text \FlagText{with some words} to be ignored.

\FlagText{Start} a new paragraph with the first word flagged.

\mycmdtrue

Here is some text \FlagText{with some words} to be ignored.

\FlagText{Start} a new paragraph with the first word flagged.

\end{document}

相关内容