1

I am re-writing our template for exams, and I would like to create a command \questionpart{Q}{A} which I can set up in a way, so that I can turn the solutions on and off, and also format und number them automatically and the such.

I also would like to have a version \questionpart{Q} which ONLY creates the question, without the solution environment.

Now, for some reason, I can get the desired output writing \questionpart{Q}{}, but not \questionpart{Q}. Why is that so? And how could I make my command behave the way I like?

    \documentclass[11pt,a4paper]{article}

    \usepackage{ifthen}

    \newcommand\test[2]{
        #1
        \ifthenelse{\equal{#2}{}}
            {}
            {XXX: #2}
    }

    \begin{document}
        \test{question}{solution}

        \test{question without solution}

        \test{question with empty solution}{}
    \end{document}

Thanks in advance!

PS: If you van come up with a more meaningful title, feel free to change it.

  • 1
    if the argument is optional it should be in [] not {} but then \newcommand can only define optional arguments as the first command. As you have it #2 is the \par which is the blank line which does not test equal to {} – David Carlisle Jul 18 '17 at 14:39
  • 1
    Not sure I understand your question, but maybe look into packages like exercise, they can do this out of the box – user36296 Jul 18 '17 at 14:40
  • 1
    Do you know ctan.org/topic/exam and ctan.org/topic/exercise – clemens Jul 18 '17 at 14:56
1

You could define \questionpart to behave differently if followed by one or two braced groups:

\usepackage{xparse}

\NewDocumentCommand{\questionpart}{+m +g}{%
  \formatquestion{#1}%
  \IfValueT{#2}{\formatanswer{#2}}%
}

where \formatquestion and \formatanswer will be responsible for formatting the two parts.

This way you will get different results from

\questionpart{Q}{A}
\questionpart{Q}
\questionpart{Q}{}

Not that I recommend this: a command should have a well defined syntax and using optional braced arguments is against all LaTeX normal conventions.

With a more standard

\usepackage{xparse}

\NewDocumentCommand{\questionpart}{+m +o}{%
  \formatquestion{#1}%
  \IfValueT{#2}{\formatanswer{#2}}%
}

the syntax would be

\questionpart{Q}[A]
\questionpart{Q}
\questionpart{Q}[]
0

Of course, if your solution contains square brackets, they must be grouped.

\documentclass[11pt,a4paper]{article}
\newcommand\test[1]{\def\savetestarg{#1}\testaux}
\newcommand\testaux[1][\TestAux]{\savetestarg\ifx\TestAux#1\relax\else: (#1)\fi}
\begin{document}
\test{question}[solution]

\test{question}[s{[o]}lution containing square brackets]

\test{question without solution}

\test{question with empty solution}[]
\end{document}

enter image description here

0

Thanks everyone for the comments!

I didn't know about the exercise package, but I think I can not use it. The same goes for the xparse package. The reason is quite simple - I work with lots of other people, who can use Latex, but would never be able to understand that code, in case they need to change something.

Luckily, the comment by David Carlisle (No idea how to link people), actually solved my problem. I just have to replace \equal{#2}{} by \equal{#2}{\par}, and it works as intended. Thanks a lot!

Here is the working code, with a screenshot from the original document, where you can see all 3 options:

    \documentclass[11pt,a4paper]{article}

    \usepackage{ifthen}

    \newcommand\test[2]{
        #1
        \ifthenelse{\equal{#2}{\par}} % Inserted \par here
            {\par} % For some reason the linebreak disappeared, so I reinserted it
            {XXX: #2}
    }

    \begin{document}
        \test{question}{solution}

        \test{question without solution}

        \test{question with empty solution}{}
    \end{document}

enter image description here

  • Try with no empty line in between. – egreg Jul 18 '17 at 17:08
  • It seems that \equal{#2}{\par} doesn't play well with equations :/ So I just leave everything be as it is. Well, at least I learned some things :D – Maxim Moloshenko Jul 19 '17 at 8:32

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.