3

I need to print a box very similar to that created by the \makeemptybox{\stretch{1}} command in exam.cls except that I would like to add dotted lines inside the box. Specifically, I would like to add 12 dotted lines first and then fill the rest of the box with empty space (see image below).

Here is what I have so far:

\documentclass[10pt,a4paper,addpoints]{exam}

\begin{document}
\begin{questions}
\question[5] In a class of 30 students, 18 have a dog, 20 have a cat and 3 have neither. A student is selected at random. What is the probability that this student has both a cat and a dog? 
\makeemptybox{\stretch{1}}
\newpage

\question[6] A pencil case contains 5 faulty and 7 working pens. A boy and then a girl each need to take a pen. 
    \begin{parts}
    \part Determine the probability that two faulty pens are chosen. 
    \part Determine the probability that at least one faulty pen is chosen. 
    \part Given that exactly one faulty pen is chosen, what is the probability that the girl chose it? 
    \end{parts}
\makeemptybox{\stretch{1}}
\newpage
\end{questions}
\end{document}

Any ideas? I am new to LaTeX so any advice would be helpful! Thank you in advance.

enter image description here

4
  • 2
    Welcome to TeX.SX! Could you add a minimal working example (MWE), that illustrates your problem or what have you done so far? - Is this related jon.hk/2017/07/latex-style-for-ib-questions?
    – Bobyandbob
    Commented May 11, 2018 at 11:51
  • You can start from this link: www-math.mit.edu/~psh/exam/examdoc.pdf pag. 59.
    – Sebastiano
    Commented May 11, 2018 at 13:02
  • @Bobyandbob Thank you. I have added the MWE. Your link is actually very related to what I'm trying to do. However, the solution there doesn't have the option of automatically filling the rest of the page with empty space.
    – muy_1047
    Commented May 12, 2018 at 14:13
  • @Sebastiano I have looked through the documentation on exam.cls but wasn't able to find what I needed.
    – muy_1047
    Commented May 12, 2018 at 14:14

2 Answers 2

5

Using mdframed package and with 2 counter, following solution can be used in exam package.

\documentclass[answers]{exam}

\makeatletter
\newcount\my@repeat@count
\newcommand{\myrepeat}[2]{%
  \begingroup
  \my@repeat@count=\z@
  \@whilenum\my@repeat@count<#1\do{#2\advance\my@repeat@count\@ne}%
  \endgroup
}
\makeatother

\newcount\myloopcounter

\newcommand{\repeatit}[2][10]{%
  \myloopcounter0% initialize the loop counter
  \loop\ifnum\myloopcounter < #1 % Test if the loop counter is < #1
  #2%
  \advance\myloopcounter by 1 % 
  \repeat % start again
}

\usepackage{mdframed}
\newenvironment{mymdf}
  {\mdfsetup{
    innertopmargin=15pt   
    }
  \begin{mdframed}%
  }  
  {\end{mdframed}}



\begin{document}

\begin{questions}

\question Approximate $\displaystyle \int_0^1 \sin x^2 \, dx$ within $.001$ of
its true value.
\begin{mymdf}
\repeatit[3]{\myrepeat{150}{.}\\} %3 row, 150 dot per row.
\end{mymdf}

\question A question with a long solution.
\begin{mymdf}
\repeatit[10]{\myrepeat{150}{.}\\} %10 row, 150 dot per row.
\end{mymdf}

\end{questions}
\end{document}

enter image description here

6
  • Thanks for the solution! Is there a way to incorporate the \stretch{1} command used in the exam.cls so the box automatically fills the rest of the page? Also, how do you add empty space after a specified number of dotted lines (like in the attached image)?
    – muy_1047
    Commented May 12, 2018 at 14:17
  • @muy_1047, yourwelcome. I think the stretch option can be used with makeemptybox. The filling to whole page may be adjusted manually. I think, my code can be adapted for makeemptybox.
    – user31034
    Commented May 12, 2018 at 17:41
  • I don't know if you answered it or not, but could you please explain how to add some vertical space after a specific number of dotted lines?
    – Diaa
    Commented Aug 31, 2018 at 17:44
  • @Diaa, You may use \vspace{} command somewhere you want.
    – user31034
    Commented Aug 31, 2018 at 19:45
  • @ferahfeza If you don't mind, I would be grateful if you could help me with my relevant question.
    – Diaa
    Commented Aug 31, 2018 at 20:25
3

Here is a solution that redefines the macro \do@fillwithdottedlines. The macro now prints a bounding box around the dotted lines. Parts are reused from the \do@emptyboxmacro. The solution box first prints vertical white space, then prints dotted lines and then prints vertical white space again. The vertical white spacing is hard coded because \do@fillwithdottedlines takes only one argument (the vertical spacing with dotted lines). For some reason, unclear to me, the vertical white spacing must be a multiple of \dottedlinefillheight otherwise the vertical rules will show gaps. There is also a small negative vskip to get unbroken vertical lines (why is also unclear to me). I didn't test it with other solution boxes.

\documentclass[a4paper,addpoints]{exam}
% Use next line to show the solutions
%\documentclass[a4paper,addpoints,answers]{exam}

\makeatletter

\setlength\dottedlinefillheight{.25in}

\newlength{\mylength}
\setlength{\mylength}{\textwidth}

\def\do@fillwithdottedlines#1{%
  \hbox to \hsize{\hskip\@totalleftmargin \leaders\hrule\hfill}%
  \begingroup
  \advance \mylength by -\@totalleftmargin
  \advance \mylength by -4em
  \ifhmode
    \par
  \fi
  \hrule height \z@
  \nobreak
  \setbox0=\hbox to \hsize{\hskip \@totalleftmargin \vrule \hskip 2em
          \vrule height \dottedlinefillheight depth \z@ width \z@
          \leavevmode\hbox to \mylength {\dotfill\hfil} \vrule}%
  \setbox1=\hbox to \hsize{\hskip\@totalleftmargin
          \vrule height\dottedlinefillheight \hfill \vrule}%
  % We use \cleaders (rather than \leaders) so that a given
  % vertical space will always produce the same number of lines
  % no matter where on the page it happens to start:
  \cleaders \copy1 \vskip 1\dottedlinefillheight%
  \vskip -\minboxheight%
  \vskip -1.5pt % Dirty hack
  \cleaders \copy0 \vskip #1%
  \vskip -\minboxheight%
  \vskip -1.5pt % Dirty hack
  \cleaders \copy1 \vskip 4\dottedlinefillheight%
  \hbox to \hsize{\hskip\@totalleftmargin \leaders\hrule\hfill}%
  \endgroup
}% \do@fillwithdottedlines

\qformat{\textbf{Question \thequestion{}} (\totalpoints \@pointname) \hfill}

\makeatother

\begin{document}

\begin{questions}
\question[10]

Calculate $1+1$.

\begin{solutionordottedlines}[2in]
2
\end{solutionordottedlines}

\question[10] Please give the answer to the ultimate question of life, the universe, and everything. 
\begin{solutionordottedlines}[1.4in]
42
\end{solutionordottedlines}

\end{questions}

\end{document}
5
  • Thanks. I tried compiling your solution but wasn't able to get a box around the dotted lines.
    – muy_1047
    Commented May 12, 2018 at 14:21
  • That's strange? I use TexLive 2018 and it works for me. What distro are you using? Commented May 12, 2018 at 14:23
  • Ah I was on 2016...Thanks. It works now!
    – muy_1047
    Commented May 14, 2018 at 4:34
  • Could you please tell me how to make the blank spaces at the very top and bottom equal to each other?
    – Diaa
    Commented Dec 16, 2018 at 11:41
  • Sorry for the delay. Yes, you can fiddle with the line \cleaders \copy1 \vskip 1\dottedlinefillheight% and change the number in1\dottedlinefillheight to the size you need, There is a second line with 4\dottedlinefillheight and you can change the 4. Commented Dec 22, 2018 at 14:38

You must log in to answer this question.

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