如何轻松编写条件页面?

如何轻松编写条件页面?

有没有什么简单的方法可以让 Latex 只写入部分页面?

ifthen我目前的想法是按照以下方式使用 -package 创建一个函数:

\newcommand{\WhatDoYouWantToWrite}[1]{
      \ifthenelse{\equal{#1}{All}}{
text that will be written if all is wanted
}
      \ifthenelse{\equal{#1}{PartA}{
text that will be written if part A is wanted
}
}
{Wrong input chosen. Don't know what to write}

这可行,但现在我的问题是,如果我要更改文本,我必须在几个地方进行更改,这很令人沮丧。然后我的想法是创建一个单独的文件并使用\input,但我如何使用\ifthenelse-statement?

我尝试使用comment-package,但之后我必须在文本中不断移动,打开和关闭\begin{comment} \end{comment}。 可以在这种设置中巧妙地使用它吗?

它看起来像考试课,人们可以选择是否要显示问题的解决方案,但输出只有两种选择,有解决方案或没有解决方案。我想要更多!

我希望只使用文档开头的一个功能来打开/关闭我想要/不想要的部分。

答案1

你的方法是避免

\WhatDoYouWantToWrite{All}

在整个文档中,然后在稍后阶段必须将每个All参数更改为PartA(或其他内容)。最好根本不传递参数,而是在全局级别设置某些内容。

以下是使用 TeX 语句的一种方法\if

在此处输入图片描述

\documentclass{article}
\newif\ifALL% \ALLfalse
\newif\ifPARTA% \PARTAfalse
\newif\ifPARTB% \PARTBfalse

\newcommand{\ConditionalWrite}{%
  \ifALL
    Text that will be written if ALL is wanted.
  \else
    \ifPARTA
      Text that will be written if PARTA is wanted.
    \fi
    \ifPARTB
      Text that will be written if PARTB is wanted.
    \fi
  \fi
}

\begin{document}

\ALLtrue
\ConditionalWrite

\ALLfalse
\PARTAtrue
\ConditionalWrite

\PARTAfalse
\PARTBtrue
\ConditionalWrite

\end{document}

您可以在序言中设置\<cond>true或。上面的例子只是显示了如何设置选择条件。\<cond>false

也可以使用etoolbox切换。

答案2

这是一个灵活的解决方案,使用鍵盤允许将任意开关作为命令/环境的键。我定义了两个(用户)命令\SelectCommentsToPrint\SelectiveComment。第一个命令控制将打印哪些注释,第二个命令定义注释并指定应在什么条件下打印它们。这两个命令都采用任意“键”。例如。

\SelectCommentsToPrint{one,three,five}

指定应打印标记为“一”、“三”或“五”的注释,并忽略所有其他注释。此后,可以这样给出注释

\SelectiveComment[one,four,eleven]{This is a silly comment}

并且仅当使用 设置了“一”、“四”和“十一”中的任何一个时,才会打印此注释\SelectCommentsToPrint。在任何时候,您都可以发出另一个\SelectCommentsToPrint来添加要打印的额外注释。您还可以使用

\SelectCommentsToPrint{all}

打印所有评论并

\SelectCommentsToPrint{unset/four}%      stop four from being printed
\SelectCommentsToPrint{unset/all,five}%  turn off all, add five - previously set comments will print

停止打印“four”等。还有一个SelectiveEnvironment用于打印较长注释的环境,它使用环境包.语法类似:

\begin{SelectiveEnvironment}[one,four]% print if one or four is set
  some nice stuff
\end{SelectiveEnvironment}

以下是 MWE 的输出:

在此处输入图片描述

如果 MWE 中未打印注释,则将?打印注释。注释后的备注---说明为何打印或不打印注释。蓝色文字表示注释打印功能何时开启或关闭。

以下是代码:

\documentclass{article}
\usepackage{pgf,pgffor}
\usepackage{environ}
\usepackage{xcolor}% only needed for highlighting selections in MWE

% https://tex.stackexchange.com/questions/15204/is-there-a-way-to-set-a-global-key-value-using-pgfkeys
\makeatletter
\newcommand{\pgfkeysgsetvalue}[2]{\pgfkeys@temptoks{#2}\expandafter\xdef\csname pgfk@#1\endcsname{\the\pgfkeys@temptoks}}
\makeatother

\pgfkeys{/SelectiveComment/.is family,/SelectiveComment,
  set/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{1}
  },
  set/unset/.unknown/.code={
      \pgfkeysgsetvalue{/SelectiveComment/\pgfkeyscurrentname}{0}
  }
}

\newif\ifPrintComment% print comment if true
\newcommand\SelectCommentsToPrint[1]{%
   \textcolor{blue}{Setting: #1!}% delete when using properly
   \foreach \key in {#1}{\pgfkeys{/SelectiveComment,set/\key} }
}
\newcommand\MakeSelection[1]{
  \PrintCommentfalse% comments off by default
  \foreach \key in {#1,all} {
    \pgfkeysifdefined{/SelectiveComment/\key}%
        {\pgfkeysgetvalue{/SelectiveComment/\key}{\temp}
         \ifnum\temp=1\global\PrintCommenttrue\fi% print if key=1
        }{}
  }
}

\newcommand\SelectiveComment[2][]{\MakeSelection{#1}\ifPrintComment#2\fi}
\NewEnviron{SelectiveEnvironment}[1][]{\MakeSelection{#1}\ifPrintComment\BODY\fi }

\begin{document}\obeylines% onlyto decrease space taken up by MWE
  \SelectCommentsToPrint{one, two, four}% print one, two or four
  \SelectiveComment[one]{This is one}\hfil--- printed as one set
  \SelectiveComment[two]{This is two}\hfil--- printed as two set
  \SelectiveComment[three]{This is three}\hfil? --- NOT printed as three not set
  \SelectiveComment[three, four]{This is three/four}\hfil--- printed as four set
  \SelectCommentsToPrint{unset/four}% stop printing four
  \SelectiveComment[four]{This is four}\hfil? --- NOT printed as four not set
  \SelectCommentsToPrint{four}% restart printing four
  \SelectiveComment[four]{This is four}\hfil--- printed as four set
  \SelectCommentsToPrint{all}% print everything
  \SelectiveComment[five]{This is five}\hfil--- printed as all set
  \SelectCommentsToPrint{unset/all}% revert to printing only selected comments
  \SelectiveComment[six]{This is six}\hfil? --- NOT printed as six not set
  \begin{SelectiveEnvironment}[one,four]
     Inside an environment with one/four
  \end{SelectiveEnvironment}\hfil--- printed as one and four set
  \begin{SelectiveEnvironment}[three,six]% will NOT  print environment
     Inside an environment with three/six
  \end{SelectiveEnvironment}\hfil? --- NOT printed as three and six not set
\end{document}

命令\SelectCommentsToPrint调用/SelectiveComment/set其中的键,反过来,将键设置/SelectiveComment/<key>1unset命令调用/SelectiveComment/set/unset,然后将 设置/SelectiveComment/<key>0。我这样做是为了阻止键在设置和取消设置时打印它们的值。实际打印由开关 控制\ifPrintComment。默认情况下,这是关闭的,并且所有键所做的就是\PrintCommenttrue在注释的某个键设置为 时发出1

/SelectiveComment/set和键均/SelectiveComment/set/unset使用键处理程序进行控制.unknown。这允许使用任意键名。我使用了马丁·沙勒的全局设置所有 pgfkeys。

\textcolor{blue}{Setting: #1!}定义中的这一行\SelectCommentsToPrint只是为了使 MWE 的输出更容易理解。如果您想使用它,应该将其删除。

答案3

这个问题有点模糊,但我试图提供一个解决方案,展示使用 获取外部文件的\include(only)方法和方式。(当然,该命令可以扩展,但最好的方法是使用外部文件的命名方案,以便在其测试分支中生成文件名) \InputOnCondition\input\InputOnCondition\InputOnCondition

\documentclass{book}

\usepackage{blindtext}
\usepackage{ifthen}
\newcommand{\InputOnCondition}[1]{%
  \ifthenelse{\equal{#1}{All}}{%
    \input{mychap1}%
    \input{mychap2}%
  }{%
    \ifthenelse{\equal{#1}{partA}}{%
      \input{mychap1}%
    }{%
      \input{mychap2}%
    }%
  }%
}




\begin{filecontents}{mychap1.tex}

\chapter{First}
\section{First}
\blindtext[10]
\end{filecontents}

\begin{filecontents}{mychap2.tex}
\chapter{Second}
\section{First of Second}
\subsection{First}
\blindtext[5]
\end{filecontents}


\includeonly{mychap2}

\begin{document}

\tableofcontents

\include{mychap1}
\include{mychap2}

% Now with \InputOnCondition

\InputOnCondition{All}

\InputOnCondition{partA}

\InputOnCondition{partB}


\end{document}

相关内容