1

I want to make a quiz using TeX that "looks like"

Problem 1) Let $f(x)=x^2$. Evaluate $f'(1)$.

Problem 2) Let $A$ be a matrix such that $$A=\begin{bmatrix}3&1\0&1\end{bmatrix}$$ What is the value of $|A|$?

Answer1) 2

Answer2) 3

And I made a TeX file "written as"

\documentclass{article}

\usepackage{amsmath,amssymb}

\newcounter{num} \newcommand{\prob}{\par\bigskip\bigskip\noindent\refstepcounter{num}\textbf{Problem \arabic{num}) }} \newcommand{\ans}{\par\bigskip\bigskip\noindent\refstepcounter{num}\textbf{Answer \arabic{num}) }}

\begin{document} \prob Let $f(x)=x^2$. Evaluate $f'(1)$.

\prob Let $A$ be a matrix such that $$A=\begin{bmatrix}3&1\0&1\end{bmatrix}.$$ What is the value of $|A|$?

\setcounter{num}{0}

\ans 2 \ans 3 \end{document}

It prints the result pretty well, except for the fact that the problem statements and answers are separated.

I want to type answer 1 just after problem 1, which would prevent many kinds of errors.

How can I fix my code?

CarLaTeX
  • 62,716

1 Answers1

2

With the options answerdelayed of exercise package, you can write the answer near the problem in your code, but print them only when you like with \shipoutAnswer.

I think exercise is the simplest package for exercises, if you find its documentation lengthy, I don't know what you would say of TikZ one! :D

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[lastexercise,answerdelayed]{exercise}
\renewcommand{\ExerciseName}{Problem}
\renewcommand{\ExerciseHeader}{\noindent\ExerciseName\ \ExerciseHeaderNB)}
\renewcommand{\AnswerName}{Answer}
\renewcommand{\AnswerHeader}{\noindent\AnswerName\ \ExerciseHeaderNB)\ }

\begin{document} \begin{Exercise} Let $f(x)=x^2$. Evaluate $f'(1)$. \end{Exercise} \begin{Answer} 2 \end{Answer} \begin{Exercise} Let $A$ be a matrix such that [A=\begin{bmatrix}3&1\0&1\end{bmatrix}.] What is the value of $|A|$? \end{Exercise} \begin{Answer} 3 \end{Answer} \shipoutAnswer \end{document}

enter image description here

Edit: if you prefer commands instead of environments:

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[lastexercise,answerdelayed]{exercise}
\renewcommand{\ExerciseName}{Problem}
\renewcommand{\ExerciseHeader}{\noindent\ExerciseName\ \ExerciseHeaderNB)}
\renewcommand{\AnswerName}{Answer}
\renewcommand{\AnswerHeader}{\noindent\AnswerName\ \ExerciseHeaderNB)\ }

\newcommand{\prob}[1]{\begin{Exercise}#1\end{Exercise}} \newcommand{\ans}[1]{\begin{Answer}#1\end{Answer}}

\begin{document} \prob{Let $f(x)=x^2$. Evaluate $f'(1)$.} \ans{2} \prob{Let $A$ be a matrix such that [A=\begin{bmatrix}3&1\0&1\end{bmatrix}.] What is the value of $|A|$?} \ans{3} \shipoutAnswer \end{document}

CarLaTeX
  • 62,716
  • 1
    Wow.. So the answer is the Exercise package and its special option. It looks good except for the fact that you used environment, while 'command' (like \exercise) will be much simpler. Thanks a lot! – govindah Jan 03 '22 at 17:00
  • 1
    @govindah You can create also your own commands, see my edit. But if the problem text is long, it is better to use environments (you can use environments for long texts and commands for short text in the same document, if you like). – CarLaTeX Jan 03 '22 at 17:10
  • Just a curiosity: but $$ is it necessary? – Sebastiano Jan 03 '22 at 17:27
  • 1
    @Sebastiano I copied the OP's code, I think [...] should be better. I'll edit my answer, thank you. – CarLaTeX Jan 03 '22 at 17:33
  • 1
    @govindah Off-topic, please look at: https://tex.stackexchange.com/questions/503/why-is-preferable-to – CarLaTeX Jan 03 '22 at 17:36
  • @CarLaTeX Thanks for your additional method, using only method! – govindah Jan 04 '22 at 03:41
  • @CarLaTeX Plus, I originally prefer ( and [ to $ and $$. But I thought that ( and [ don't work in this webpage, that's why I used $ and $$. I'll use ( and [ next time! Thanks for your comment! One more comment. I looked the exercise manual file onece more. The expression 'lengthy' I used is not right I think. Like you said, it is simple compared to tikz or enumitem. This is because of my immatureness. But after using many years of TeX, I'm not that accustomed to the CTAN things, just my personal problem. – govindah Jan 04 '22 at 03:48
  • @govindah Don't worry, if you have problems don't hesitate to ask here! – CarLaTeX Jan 04 '22 at 06:29
  • Thanks for your hospitable comment. Actually I'm not native english user. Maybe that's (partially) why I'm having difficulty still in this good website(StackExchange) and CTAN. Thanks for your good alternative code, I got to use it on my own. But I have one more minor question. I understand what \ExerciseName and \ExerciseHeaderNB mean (they are so obvious). But what is the single backslash? i.e. \noindent\ExerciseName"\" \ExerciseHeaderNB Also, in \noindent\AnswerName\ \ExerciseHeaderNB)\ You've used two backslashes. What do they mean? – govindah Jan 06 '22 at 16:07
  • Is it like, \% or \#? I mean, they visualize % and # literally. Is \(backslash + indentation) making an indentation? – govindah Jan 06 '22 at 16:11
  • @govindah A backslash followed by a space simply adds a space. – CarLaTeX Jan 06 '22 at 16:11
  • @govindah see point 14. here: https://tex.stackexchange.com/questions/74353/what-commands-are-there-for-horizontal-spacing/74354 – CarLaTeX Jan 06 '22 at 16:22