使用版本包进行颜色编码

使用版本包进行颜色编码

我使用软件包版本来管理我的测试和答案的多个类似版本。我希望能够对它们进行颜色编码,这样我就可以在查看单个输出的同时轻松比较每个版本。我尝试创建包含的新命令\begin{TestA}\end{TestA},但当排除该版本时,我无法编译文档。我尝试的代码是

\newcommand{\ta}[1]{\begin{testA} \textcolor{blue}{#1} \end{testA}}
\newcommand{\tb}[1]{\begin{testB}\textcolor{green}{#1}\end{testB}}

当我使用

\includeversion{testA}\excludeversion{testB}

我收到错误消息

Extra \fi.

对于我使用后的行\tB{insert text for version B here}

我正在寻找一种不需要我\textcolor{blue}在每次开始文档中的新版本时都添加的东西,这样当我去打印所需的测试版本时,我可以通过在一个地方更改颜色轻松地将所有内容切换为黑色。

总代码可能是:

\documentclass{article}

\usepackage{xcolor}%to use the colors

\usepackage{versions}

\newcommand{\ta}[1]{\begin{testA} \textcolor{blue}{#1} \end{testA}}%This way I can see what is in version A in the compiled document by turning it blue (This works when version A is included as above)

\newcommand{\tb}[1]{\begin{testB}\textcolor{green}{#1}\end{testB}} %this is to make version B show up green so I can compare both at a glance but when B is not included I get the error message from above


\includeversion{testA}%included so everything in test A will show up

\excludeversion{testB} %excluded so that version B does not show up

\begin{document}
This text is for both versions

\ta{This is just for version A}

\tb{This is just for version B}

This is for both again

\end{document}

答案1

您无需创建新的宏,而是可以使用钩子为现有环境添加一些颜色:

\documentclass{article}

\usepackage{xcolor}%to use the colors

\usepackage{versions}

\AddToHook{env/testA/begin}{\color{blue}}
\AddToHook{env/testB/begin}{\color{green}}

\includeversion{testA}%included so everything in test A will show up
\excludeversion{testB} %excluded so that version B does not show up

\begin{document}
This text is for both versions

\begin{testA}
This is just for version A
\end{testA}

\begin{testB}
This is just for version B
\end{testB}

This is for both again

\end{document}

答案2

文档指出(第 2 页)

然而,\begin{〈version〉}...\end{〈version〉}宏观论证内部存在断裂,例如\footnote(它甚至很脆弱)。因此,有\processifversion{〈version〉}{〈code〉}类似的工作(分组也是如此),依赖于\excludeversion等等(它甚至很强大)。

因此,如果你想使用宏而不是环境,你需要使用\processifversion

\documentclass{article}

\usepackage{xcolor}
\usepackage{versions}
\includeversion{testA}
\excludeversion{testB}

\newcommand{\ta}[1]{\processifversion{testA}{\textcolor{blue}{#1}}}
\newcommand{\tb}[1]{\processifversion{testB}{\textcolor{green}{#1}}}

\begin{document}
This text is for both versions

\ta{This is just for version A}

\tb{This is just for version B}

This is for both again

\end{document}

在此处输入图片描述

相关内容