3

I use the package versions to manage multiple similar versions of my tests and answer keys. I would like to be able to color code them so that I can easily compare each version while looking at one single output. I have tried creating new commands that include the \begin{TestA}\end{TestA} in them, but when that version is excluded I cannot get the document to compile. The code I tried is

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

When I use

\includeversion{testA} and \excludeversion{testB}

I get the error message

Extra \fi.

for the line after I use \tB{insert text for version B here}

I am looking for something that does not require me to add the \textcolor{blue} every time I begin a new version in the document so that when I go to print the desired version of the test I can easily switch everything to be in black by just changing the color in one place.

The total code could be:

\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}
2
  • 1
    Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Nov 21, 2023 at 14:45
  • I just edited the question to include a sample document.
    – VMarth
    Nov 21, 2023 at 16:04

2 Answers 2

2

Instead of creating new macros, you could use hooks to add some colour to the existing environments:

\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
  • That is perfect and exactly what I was looking for. Thank you so much. I taught myself LaTex with a few inherited documents from classmates and a lot of google, so I had never heard of adding a hook before.
    – VMarth
    Nov 21, 2023 at 16:18
  • @VMarth Hooks are (relatively) new, so your class mates might not yet know them. A chance for you to teach them something new :) Nov 21, 2023 at 16:20
2

The documentation states (p. 2)

However, \begin{〈version〉}...\end{〈version〉} breaks inside macro arguments, e.g. of \footnote (it is even fragile). So there is \processifversion{〈version〉}{〈code〉} working analogously (grouping as well), depending on \excludeversion etc. (it is even robust).

Hence, if you want to use a macro rather than an environment you need to use \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}

enter image description here

1
  • 1
    Thank you. I will use this to adjust my headers in my tests to easily track which version I am printing. I had tried putting the test version in my fancy header just using \begin{testA} A \end{testA} etc., and run into that issue. Since it was a relatively quick thing to adjust I have been adjusting it manually, but occasionally forgetting it so this will also be helpful.
    – VMarth
    Nov 21, 2023 at 17:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .