添加可以在编译期间打开或关闭的文本

添加可以在编译期间打开或关闭的文本

我用 LaTeX 写了一本书,目前正在审阅。但是,我想在书中插入更多细节,例如印刷版。因此,我正在寻找一个可能的选项,以包含基于变量(例如调试标志)编译并显示在文档中的文本。我想举个例子。第一个版本应该有以下文本:

The keywords has to be added to the \textit{fvSolution} file, 
otherwise, the solver might indicate an error.

对于第二个版本我想扩展文本:

The keywords has to be added to the \textit{fvSolution} file, 
otherwise, the solver might indicate an error. \secondVersion{However, default
values are used if nothing is specified}

我还可能在第二个版本中使用它来为文本着色,例如:

The \secondVersion{\{\red}keywords\secondVersion{\}} has to be 
added to the \textit{fvSolution} file, otherwise, the solver might
indicate an error.

\secondVersion{}在编译过程中,无论是否设置了标志,都应编译内部文本。我想这样的事情可能是可能的。任何提示都值得赞赏。

提前致谢。

答案1

您可以定义两个命令:

\documentclass{article}
\usepackage{xcolor}

\makeatletter
\newif\ifsecondversion

% text that should be omitted in the first version
\newcommand{\sv}[1]{%
  \ifsecondversion
    #1%
  \else
    \@bsphack\expandafter\@esphack
  \fi
}
% text that should be colored in the second version
\newcommand{\svx}[2][red]{%
  \ifsecondversion
    \textcolor{#1}{#2}%
  \else
    #2%
  \fi
}

\begin{document}

\secondversionfalse

The \svx{keywords} has to be added to the \textit{fvSolution} file, 
otherwise, the solver might indicate an error. \sv{However, default
values are used if nothing is specified.} Blah, \svx[blue]{blah}.

\secondversiontrue

The \svx{keywords} has to be added to the \textit{fvSolution} file, 
otherwise, the solver might indicate an error. \sv{However, default
values are used if nothing is specified.} Blah, \svx[blue]{blah}.

\end{document}

在此处输入图片描述

答案2

感谢@Phelype Oleinik 在评论部分提供的提示。我使用了以下方法https://tex.stackexchange.com/a/33579/116084这导致:

\newif\ifsecondVersion
\secondVersiontrue

\begin{document}
The \ifsecondVersion {\red \fi keywords \ifsecondVersion }\fi has to be 
added to the \textit{fvSolution} file, otherwise, the solver might
indicate an error.
\end{document}

运行完美。

答案3

为什么不简单地

...
\newcommand{\secondVersion}[1]{#1}
\newcommand{\secondVersion}[1]{}
...

并注释掉您不想要的版本?

相关内容