如何制作条件文档功能

如何制作条件文档功能

我在另一个问题中看到关于如何创建条件的内容,其中推荐的最佳做法是\iftoggle。我想创建一个宏,允许创建教师笔记,一个具有可定义外观和感觉的部分,如果我定义这不是教师版本,它将有条件地编译掉。

事实证明,错误在于我没有意识到必须使用 usepackage 才能使用 etoolbox。这是(现在可以使用的)代码。我如何定义边框和阴影以嵌入到教师宏中,有没有更好的方法来实现这一点?

\documentclass[11pt]{book}              % Book class in 11 points
\parindent0pt  \parskip10pt             % make block paragraphs
\raggedright                            % do not right justify
\usepackage{etoolbox}

\title{\bf Document Title}    % Supply information
\author{Author}              %   for the title page.
\date{\today}                           %   Use current date. 

\newtoggle{teacherFlag}
\toggletrue{teacherFlag}

\newcommand{\teacher}[1] {
\iftoggle{teacherFlag}{
    {#1}
}{
}

}


% Note that book class by default is formatted to be printed back-to-back.
\begin{document}                        % End of preamble, start of text.
\frontmatter                            % only in book class (roman page #s)
\maketitle                              % Print title page.
\tableofcontents                        % Print table of contents
\mainmatter                             % only in book class (arabic page #s)
\part{A Part Heading}                   % Print a "part" heading
\chapter{A Main Heading}                % Print a "chapter" heading
Most of this example applies to \texttt{article} and \texttt{book} classes
as well as to \texttt{report} class. In \texttt{article} class, however,
the default position for the title information is at the top of
the first text page rather than on a separate page. Also, it is
not usual to request a table of contents with \texttt{article} class.

\section{A Subheading}                  % Print a "section" heading
The following sectioning commands are available:
\begin{quote}                           % The following text will be
 part \\                                %    set off and indented.
 chapter \\                             % \\ forces a new line
 section \\ 
 subsection \\ 
 subsubsection \\ 
 paragraph \\ 
 subparagraph 
\end{quote}                             % End of indented text
\teacher{foo}
But note that---unlike the \texttt{book} and \texttt{report} classes---the
\texttt{article} class does not have a ``chapter" command.

\end{document}

答案1

我建议你使用另一种方法comment包裹:

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{comment}
\usepackage{lipsum}

\specialcomment{teacher}{\begin{tcolorbox}}{\end{tcolorbox}}

%\excludecomment{teacher}

\begin{document}

\section{Test section}
\lipsum[4]

\begin{teacher}
\lipsum[2]
\end{teacher}

\section{Another test section}
\lipsum[4]

\begin{teacher}
\lipsum[2]
\end{teacher}

\end{document}

这将产生完整版本:

在此处输入图片描述

只需取消注释该行\excludecomment{teacher},即可获得学生版本:

在此处输入图片描述

我用了tcolorbox制作一个漂亮的框架,在教师部分周围带有阴影背景。

答案2

请始终发布完整的小文档和/或显示完整的错误消息,但是您实际上并不需要为此使用布尔切换。您只需要一个命令来吃掉它的参数或使用它,因此只需

\newcommand\teacher[1]{#1}
%\newcommand\teacher[1]{}

然后与您想要其他版本时\teacher{stuff}相同,只需向上移动以注释掉第一个版本,然后就不会执行任何操作。stuff%\teacher{stuff}

相关内容