chirun-ncl / chirun

A Python package providing the command line interface for building flexible and accessible content with Chirun.
https://chirun.org.uk/
Other
29 stars 4 forks source link

Interpret \comment and \endcomment as commenting out #87

Closed prowlett closed 3 years ago

prowlett commented 3 years ago

I have a bit of LaTeX code in my exam paper to hide/show answers which works like this. It doesn't work.

The hidden version of this uses \comment and \endcomment from the verbatim package to lose a block of code. Is there any chance you could interpret \comment and \endcomment as skipping what comes between them? Or is there something makecourse-compliant that I could be doing here?

\documentclass{article}

\usepackage{verbatim}
\newenvironment{answer}{\comment}{\endcomment} % THIS LINE TO HIDE ANSWERS
%\newenvironment{answer}{[Answer: }{]} % THIS LINE TO SHOW ANSWERS

\begin{document}
    \begin{enumerate}
        \item Question \begin{answer}Answer\end{answer}
    \end{enumerate}
\end{document}
georgestagg commented 3 years ago

It looks like the \comment macro might be defined in the wrong place in plastex. Does it work if you add \usepackage{comment}?

prowlett commented 3 years ago

I don't know the package comment, but have tried. Partial success.

Swapping verbatim for comment causes a pdflatex error:

Runaway argument?
! File ended while scanning use of \next.

Including both verbatim (for pdflatex) and comment (for plastex), the file builds successfully, but it loses the content from the rest of the file in the HTML version (but it's fine in the PDF). For example

\documentclass{article}

\usepackage{comment}
\usepackage{verbatim}
\newenvironment{answer}{\comment}{\endcomment} % THIS LINE TO HIDE ANSWERS
%\newenvironment{answer}{[Answer: }{]} % THIS LINE TO SHOW ANSWERS

\begin{document}
    \begin{enumerate}
        \item Question \begin{answer}Answer\end{answer}
    \end{enumerate}

    Something else.
\end{document}

displays nothing after "Question" in the HTML version (but has "Something else" in the PDF). What's even more weird is that including \comment in my \item line changes the numbering from "1" to "(a)" in the HTML version (again the PDF is fine, using number 1).

Changing the commented out line to the other one displays "1. Question [Answer: Answer] Something else." as expected.

georgestagg commented 3 years ago

Okay, thanks for testing that.

I looked into this in further detail this morning. The primary cause seems to be LaTeX and plasTeX implementing the verbatim package differently. When you call \verbatim (or \comment in this case) the token stream is searched for the corresponding \endverbatim and everything between the two is output as-is (or ignored). Importantly, during this search macro expansion is turned off because we don't want to expand anything inside, even if it begins with \.

So, after you invoke \begin{answer} the code was looking for a \endcomment that never comes. It only sees \end{answer}, but doesn't expand it and so misses the \endcomment hiding inside. That's why all the content after the "Question" was lost.

In the latest commit I've tweaked the implementation of verbatim to add an extra search for \end{...}. If it finds it, it expands and looks inside for the real \end.... Finally, if it finds the real thing, the \end{...} is expanded, otherwise it prints it verbatim (or ignores it in the case of \comment) and continues on as before.

As far as I can tell, the following example now works for me. Hopefully it's the same case for your real exam.

 \documentclass{article}

 \usepackage{verbatim}
 \newenvironment{answer}{\comment}{\endcomment} % THIS LINE TO HIDE ANSWERS
 %\newenvironment{answer}{[Answer: }{]} % THIS LINE TO SHOW ANSWERS

 \begin{document}
     Content before enumerate
     \begin{enumerate}
         \item Question \begin{answer}Answer\end{answer}
     \end{enumerate}
     Content after enumerate
 \end{document}
prowlett commented 3 years ago

Amazing, thank you! Yes, that works with the real thing too.