0

I am writing an exam for a course I am teaching. I want to be able to easily compile a version of the document with solutions and one without solutions. Below a minimal working example:

  \documentclass[12pt]{article}
\usepackage{amssymb}
\usepackage{amsmath}
\begin{document}
\begin{enumerate}
    \item{[1 point] Two plus two is equal to what?}
        \begin{footnotesize}
        \begin{itemize}
        \item  $2+2 = 4$
        \end{itemize}
        \end{footnotesize}
    \item{[1 point] How many months are there in a year?}
        \begin{footnotesize}
        \begin{itemize}
        \item There are 12 months in the year
        \end{itemize}
        \end{footnotesize}
    \end{enumerate}
\end{document}

What I want is a way of automatically commenting out the solution lines. Such as an if statement. If solution = true, then print the solution of the exam. Else do not print.

2
  • Without conditional nor boolean variables, to hide or show every \solution{<whatever>} in the document you can switch between some like \def\solution{#1} and \def\solution{} in the preamble, so you need only type/remove #1, or have both definitions and just inactivate one with a % before.
    – Fran
    Commented May 4, 2021 at 13:15
  • That would work for me but I am unsure how to implement it.
    – phdstudent
    Commented May 4, 2021 at 13:20

1 Answer 1

1

Option 1: using "comments"

Use the comment package https://ctan.org/pkg/comment?lang=en

Toggle whether you want to show solutions by commenting out one of the \includecomment and \excludecomment lines, and uncommenting the other.

\documentclass[12pt]{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{comment}

%\includecomment{solution}
\excludecomment{solution}

\begin{document}
\begin{enumerate}
    \item{[1 point] Two plus two is equal to what?}
        \begin{solution}
        \begin{footnotesize}
        \begin{itemize}
        \item  $2+2 = 4$
        \end{itemize}
        \end{footnotesize}
        \end{solution}
    \item{[1 point] How many months are there in a year?}
        \begin{solution}
        \begin{footnotesize}
        \begin{itemize}
        \item There are 12 months in the year
        \end{itemize}
        \end{footnotesize}
        \end{solution}
    \end{enumerate}
\end{document}

Option 2: use the exam class

It looks like you are writing a quiz/exam. I would highly encourage you to look into the exam class https://ctan.org/pkg/exam?lang=en

The class includes features like automatically tallying the total number of points, hiding/unhiding solutions, and much more.

You must log in to answer this question.

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