定义全局标志来有条件地删除文本

定义全局标志来有条件地删除文本

提前致歉。我删除了 mwe 的许多功能,但我发现 LaTeX 很难调试,删除操作会产生更多错误并引发更多问题,因此我目前保持原样。

我正在创建课程笔记,其中包含有关教学法的学生文本和教师文本。我想创建两个版本的文本,即教师版本和学生版本。教师版本还可以包含练习答案。

我创建了 mwepreamble.tex 以包含此模式的所有文件以及所有重复的定义,并创建了 mwe.tex 包含具体的示例文本。

在序言中,我定义了一个布尔变量 teach,并尝试定义一个新的环境 teacher,这样如果我用

\begin{teachr} ... \end{teachr}

只要 teach 变量为 false,文本就应该消失。我尝试并得到错误:

l.15 \begin{teachr}
! LaTeX Error: \begin{document} ended by \end{teachr}.

mwepreamble.tex:

\documentclass[12pt]{book}
\usepackage[titles]{tocloft}
\setcounter{tocdepth}{0}
\setlength{\cftbeforechapskip}{0pt}

\parindent0pt  \parskip10pt     % make block paragraphs
\raggedright                                % do not right justify
\usepackage{mathptmx}   % times roman, including math (where possible)
\usepackage{mathpazo}   % palatino, including math (where possible)
\usepackage{helvet}     % helvetica
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[usenames,dvipsnames]{color}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{etoolbox}
%\usepackage{comment}
%\usepackage{etoc}
\usepackage{listings}
\usepackage{grffile}
\usepackage{listing}
\usepackage{changepage}
\usepackage{multirow}
\usepackage{outlines}
\usepackage{enumitem}
\usepackage{fancyhdr}
%\usepackage{subfigure}
\usepackage{textcomp}
\usepackage{collcell}
\usepackage{tabu}
\usepackage{titlesec}
\usepackage{environ}
\definecolor{DeepPink}{rgb}{0.8,0,0.4}
\definecolor{DarkRed}{rgb}{0.5,0,0}
\definecolor{DarkBlue}{rgb}{0,0,0.5}
\titleformat{\chapter}
  {\normalfont\LARGE\bfseries\color{DarkBlue}}{\thechapter.}{1em}{}
\usepackage[top=0.5in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage[colorlinks,citecolor=DeepPink4,linkcolor=DarkRed, urlcolor=DarkBlue]{hyperref}
\usepackage{cleveref}

\cfoot{\sffamily\textcopyright... }
\rfoot{\thepage}
\graphicspath{ {img/} }
\pagestyle{fancy}
\definecolor{lgray}{rgb}{0.9,0.9,0.9}
\definecolor{white}{rgb}{1,1,1}
\definecolor{dkgreen}{rgb}{0,.5,0.1}
\definecolor{wrongcolor}{rgb}{.9,.5,.4}
\definecolor{purple}{rgb}{0.6,0,0.6}


\lstset{ 
      backgroundcolor=\color{lgray},  
%   basicstyle=\footnotesize \ttfamily \color{black} \bfseries,   
      breakatwhitespace=false,       
      breaklines=true,               
      captionpos=b,                   
      commentstyle=\color{dkgreen},   
      deletekeywords={...},          
      escapeinside={\%*}{*)},                  
      frame=single,                  
      keywordstyle=\textbf,  
      morekeywords={BRIEFDescriptorConfig,string,TiXmlNode,DetectorDescriptorConfigContainer,istringstream,cerr,exit}, 
      identifierstyle=\color{black},
      stringstyle=\color{blue},      
      language=Java,                
      numbers=right,                 
      numbersep=5pt,                  
      numberstyle=\tiny\color{black}, 
      rulecolor=\color{black},        
      showspaces=false,               
      showstringspaces=false,        
      showtabs=false,                
      stepnumber=1,                   
      tabsize=5,                     
      title=\lstname,                 
}

\newcommand{\java}{\begin{lstlisting}[language=Java]}
\newcommand{\cpp}{\begin{lstlisting}[language=C++]}

\newcommand{\qqq}{\section{Questions}\begin{enumerate}}
\newcommand{\exer}{\end{enumerate}\section{Exercises}\begin{enumerate}}
\def\endexer#1{\end{enumerate}%
\ifstrempty{#1}{%
  }{%
    \href{\quizzes/#1}{Take the quiz}}
  }%
\makeatletter
\newcounter{lab}% also defines \thelab
% roughly equivalent to \chapter
\newcommand{\lab}[1]{% #1 is lab heading
  \refstepcounter{lab}%
  \if@openright\cleardoublepage\else\clearpage\fi%
  \addtocontents{toc}{\string\contentsline {chapter}%
    {\hbox to .65in{Lab}\protect\numberline{\thelab}#1}{\thepage}}%
  \global\@topnum\z@% page number to bottom?
  \noindent%
  \@lab{#1}% fixed first character separation
}

% roughly equivalent to \@chapter
\def\@lab#1{% #1 is lab heading
  \if@twocolumn%
    \@topnewpage[{\huge\bfseries Lab \thelab: #1\par}]%
  \else{\huge\bfseries Lab \thelab: #1\par}%
  \@afterheading% no idea
  \fi}

\newcommand{\ohm}{$\Omega$ }
\newcommand{\kohm}{k$\Omega$ }

\newtoggle{teach}
\toggletrue{teach}
\NewEnviron{teachr}
  {\iftoggle{teach}{\BODY}{}}

\setenumerate[1]{label=\arabic*.}
\setenumerate[2]{label=\Alph*.}
\setenumerate[3]{label=\roman*.}
\setenumerate[4]{label=\alph*.}

麦格

\input{mwepreamble.tex}


\begin{document}                        % End of preamble, start of text.

\chapter{Introduction}
\begin{teachr}
These notes are just for teachers and should only display when printing the teacher version.
\end{teachr}

Introductory Text

\begin{itemize}
  \item Things you will need to do.
  \item Yet another bullet point.
  \item Safety third!
  \item Tired of working? Hold a meeting! Meetings, the alternative to work.
\end{itemize}

\chapter{Safety Warning}
Before starting your kindergarten course in radioactive materials, here are a few things you should know

\begin{itemize}
 \item Don't chew on the radium pacifiers.
 \begin{itemize}
   \item We know they are sugar coated and really tasty, but control yourself!
 \end{itemize}

 \item Don't put too many plutonium blocks into a big pile -- it's dangerous!
\end{itemize}

\begin{teachr}
\chapter{Teachers Notes on Engineering Competitions}

Text. text. text....
\end{teachr}

\end{document}

答案1

您对teachr环境的定义不正确。您可能想要的是这样的:

\usepackage{environ}
\NewEnviron{teachr}
  {\ifthenelse{\boolean{teach}}{\BODY}{}}

上面使用了environ包裹定义布尔值teachr并设置条件teach。如果设置为true\BODY则设置,否则teachr吞噬其全部内容。一个参考可能是抑制不必要的言论


这是您的实例中的一个最小示例:

\documentclass{book}

\usepackage{etoolbox,environ}

\newtoggle{teach}
\toggletrue{teach}
\NewEnviron{teachr}
  {\iftoggle{teach}{\BODY}{}}

\begin{document}                        % End of preamble, start of text.

\chapter{Introduction}
\begin{teachr}
These notes are just for teachers and should only display when printing the teacher version.
\end{teachr}

Introductory Text

\begin{itemize}
  \item Things you will need to do.
  \item Yet another bullet point.
  \item Safety third!
  \item Tired of working? Hold a meeting! Meetings, the alternative to work.
\end{itemize}

\chapter{Safety Warning}
Before starting your kindergarten course in radioactive materials, here are a few things you should know

\begin{itemize}
 \item Don't chew on the radium pacifiers.
 \begin{itemize}
   \item We know they are sugar coated and really tasty, but control yourself!
 \end{itemize}

 \item Don't put too many plutonium blocks into a big pile -- it's dangerous!
\end{itemize}

\begin{teachr}
\chapter{Teachers Notes on Engineering Competitions}

Text. text. text....
\end{teachr}

\end{document}

我删除了与问题无关的大多数软件包,并将条件语句的使用更新为etoolbox使用“切换”。请参阅该包为何ifthen过时了?以获得动力。

你还包括了comment包裹可用于选择性地包含/排除环境,而不是使用environ。请参阅如何通过简单地设置变量或选项来排除文本部分?或者注释掉大段内容

相关内容