As suggested in the comments, I recommend using something like \begin{version}...\end{version}
together with \excludecomment{version}
.
If this is not possible then I suggest writing your own "comment-like" environment by hand. To me using \version{sol}...\endversion{sol}
seems way too cumbersome (because the sol
is a dummy argument that is not actually doing anything), so I would use \version{...}
.
Here is one way to do this:
\documentclass{article}
\newif\ifSolutions% define \ifSolutions <solutions code>\else <no solutions code>\fi.
\newcommand\version[1]{\ifSolutions #1\fi}% print #1 only if \Solutionstrue
\begin{document}
Test
\Solutionsfalse\version{Solutions are off}% should not print
\Solutionstrue\version{Solutions are on}% should print
\end{document}
This produces:
If you really want to use \version{sol}...\endversion{sol}
then here is a horrible hack that will let you do this:
\documentclass{article}
\newif\ifSolutions% define \ifSolutions <solutions code>\else <no solutions code>\fi.
\long\def\version#1#2\endversion#3{\ifSolutions #2\fi}
\begin{document}
Test
\Solutionsfalse\version{sol}Solutions are off\endversion{sol}
\Solutionstrue\version{sol}Solutions are on\endversion{sol}
\end{document}
This gives you the syntax that you wanted but I do not recommend that you use this! What the code is doing is defining \version
to be a macro that expects three arguments with \endversion
between the second and third arguments. The second argument is printed if \Solutionstrue
and the first and third arguments are discarded. The \long
is needed because this allows #2
, the real argument, to contain paragraph breaks, which you will probably want in your use-case. This macro is an abuse of the TeX language and it will probably earn me some down-votes for even suggesting this!
If you want to go this route then I recommend dropping the {sol}
's and using:
\long\def\version#1\endversion{\ifSolutions #1\fi}
\version Sol/version code\endversion
\end{sol}
; TeX uses a delimited argument that requires that. Of course, you can define it in whichever way you want, capturing it between whichever delimiters you specify (say\version{sol}
...\eversion{sol}
. But then you have to be caution about what's contained between these delimiters (likelistings
, for example).\begin{sol}
and\end{sol}
?\begin{sol}
and\end{sol}
, it turns out that if there's a space before the\end
, I get the same error. So I end up with something that looks like an ordinary environment like "equation" or "quote", but turns out to be just as special as "verbatim". I'm sure this is necessary for some reason, but it sure makes for hard-to-understand stuff for non-experts.