如何忽略或標記環境和命令之外的內容

如何忽略或標記環境和命令之外的內容

在 LaTeX 文件的序言中,命令被處理,但所有會導致輸出的內容都會導致錯誤Missing \begin{document}

\documentclass{article}
This leads to an error.
\begin{document}
\end{document}

tikzpicture環境中,所有無法解釋為 tikz 指令的內容都會被忽略。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  Something that is ignored
  \node {A};
  Something that is ignored
\end{tikzpicture}
\end{document}

問題:在專用環境中實現類似效果的最簡單方法是什麼?舉個例子,文檔

\documentclass{article}
\newenvironment{IgnoreOrFlagUnknownStuff}{}{}% ??? to be defined
\newenvironment{A}{\paragraph{A:}}{}
\newcommand\B[1]{\paragraph{B:} #1\par}
\begin{document}
\begin{IgnoreOrFlagUnknownStuff}
  Something to ignore or to complain about.
  \begin{A}
    This is OK.
  \end{A}
  Something to ignore or to complain about.
  \B{This is also OK.}
  Something to ignore or to complain about.
\end{IgnoreOrFlagUnknownStuff}
\end{document}

應該要么抱怨這些Something to ignore句子,要么忽略它們,同時處理其餘的句子。

答案1

根據 Enrico 的評論,可以透過切換到\nullfont.

\newenvironment{IgnoreUnknownStuff}{\nullfont}{}

這將產生類似於日誌檔案中的警告Missing character: There is no S in font nullfont!,可以透過設定來抑制\tracinglostchars=0。被忽略的東西可能仍然會導致額外的垂直和水平空間,可能是因為進入和離開 hmode 以及在行尾添加空格,所以被忽略的東西不會變得完全不可見。

若要為被忽略的內容(如 LaTeX 格式對前導碼中的字元所做的​​那樣)發出錯誤訊息,可以\everypar透過設定在輸入 hmode 時產生錯誤\everypar{\ErrorUnknownStuff}。當需要忽略字元時,必須確保 TeX 處於 vmode 下;請注意\par以下命令。

\newenvironment{FlagUnknownStuff}%
  {\everypar{\ErrorUnknownStuff}%
   \nullfont
   \par
   \tracinglostchars=0
  }{}
\newcommand\ErrorUnknownStuff{\GenericError{}{Unknown Stuff}{}{}}
% Commands and environments that may appear in the environment
\newenvironment{A}{\normalfont\paragraph{A:}}{\par}
\newcommand\B[1]{{\normalfont\paragraph{B:} #1\par}}
\newcommand\C[1]{{\everypar{}\normalfont #1\par}}

\everypar必須在\everypar{}故意輸入 hmode 之前恢復。在上面的定義中將\paragraph隱式地執行此操作,但 的定義\C必須明確地執行此操作。

左圖是下面程式碼的結果。環境中的四個忽略和抱怨行中的每一個都會發出一個錯誤FlagUnknownStuff。右圖是刪除所有要忽略的行後的輸出;注意間距的差異。

在此輸入影像描述 在此輸入影像描述

\documentclass{article}
\newenvironment{IgnoreUnknownStuff}{\nullfont}{}
\newenvironment{FlagUnknownStuff}%
  {\everypar{\ErrorUnknownStuff}%
   \nullfont
   \par
   \tracinglostchars=0
  }{}
\newcommand\ErrorUnknownStuff{\GenericError{}{Unknown Stuff}{}{}}
% Commands and environments that may appear in the environment
\newenvironment{A}{\normalfont\paragraph{A:}}{\par}
\newcommand\B[1]{{\normalfont\paragraph{B:} #1\par}}
\newcommand\C[1]{{\everypar{}\normalfont #1\par}}
\begin{document}
Before.
\begin{IgnoreUnknownStuff}
  Something to ignore.
  \begin{A}
    This is OK.
  \end{A}
  Something to ignore.
  \B{This is also OK.}%
  Something to ignore.
  \C{This also.}
  Something to ignore.
\end{IgnoreUnknownStuff}
In-between.
\begin{FlagUnknownStuff}
  Something to ignore and to complain about.
  \begin{A}
    This is OK.
  \end{A}
  Something to ignore and to complain about.
  \B{This is also OK.}
  Something to ignore and to complain about.
  \C{This also.}
  Something to ignore and to complain about.
\end{FlagUnknownStuff}
After.
\end{document}

相關內容