0

I want to use the comment package to selectively include/exclude parts of a document. While "\begin{sol}" and "\end{sol}" pairs, combined with \includecomment or \excludecomment would work for selectively including solutions to homework problems, I'd rather write \version{sol} and \endversion{sol}. Knowing that making macros whose names start with "end" is tricky, I went with \eversion. This did not work: apparently a macro that puts \end{sol} into a document is different from \end{sol} in some way I don't understand:

\documentclass{article}
\RequirePackage{comment}

\excludecomment{sol}

\newcommand{\version}[1]%
  {\begin{#1}}

\newcommand{\eversion}[1]%
  {\end{#1}}

\begin{document}
Test.
\version{sol}
Test text.
\eversion{sol}
%\end{sol}
\end{document}

This generates an error with pdflatex ("File ended while scanning use of \next") , but if I comment out the "eversion" line and uncomment \end{sol}, it works. Can someone explain why?

  • In order to capture the entire comment, there needs to be an explicit \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 (like listings, for example). – Werner Sep 30 '16 at 16:52
  • What's wrong with \begin{sol} and \end{sol}? – egreg Sep 30 '16 at 16:54
  • 1. I'm trying to make minimal changes to my documents, 2. the word "version" actually has meaning in a larger context here, one that has to do with a bunch of shell-scripts, etc. It'd be easier for me and my TAs to use that language inside the doc as well, and I thought that given Latex's programming-language-like features, that might be feasible. Now I'm just groaning and thinking of the old joke whose punch line is "But he has a marvelous tailor." – John Sep 30 '16 at 17:04
  • Also: even using \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. – John Sep 30 '16 at 17:21
1

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:

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.