4

For the following, how to calculate the number of horizontal rules (i.e. 19 and 11) based on the remaining writable textheight for each page?

\documentclass{exam}
\usepackage[
a4paper, left = 10 mm, right = 10 mm, includehead, 
top = 10 mm, headheight = 60 mm, headsep = 0pt,
includefoot, foot = 0 mm, bottom = 25 mm 
]{geometry}
\usepackage{mdframed,xparse,blindtext,pgfplots}


\NewDocumentCommand{\AnswerRules}{ O{10mm} O{0.2pt} m O{nobreak} }{%
    \begin{mdframed}[linewidth = 0pt, nobreak = false]

        \setlength{\parskip}{0pt}
        \setlength{\baselineskip}{#1}
        \foreach \i in {1,...,#3} {\noindent\rule{\linewidth}{#2}\par}

    \end{mdframed}
}

\begin{document}
    \setlength{\parindent}{0cm}
    \begin{questions}

        \question short question\vspace{\baselineskip}
        \AnswerRules{19}

        \newpage
        \question \blindtext[3] \vspace{\baselineskip}
        \AnswerRules{11}

    \end{questions}
\end{document}

enter image description here

Diaa
  • 9,599
  • Perhaps related: https://tex.stackexchange.com/questions/308926/measure-remaining-space-on-page-and-use-it-on-another-page – Steven B. Segletes Jan 13 '20 at 16:23
  • @StevenB.Segletes I pretty need some rounded lengths division but it seems not clear to me how to do it. – Diaa Jan 13 '20 at 16:49

1 Answers1

4

You can use \leaders to fill the space with copies of a box.

\documentclass{exam}
\usepackage[
  a4paper,
  left = 10mm,
  right = 10mm,
  includehead, 
  top = 10mm,
  headheight = 60mm,
  headsep = 0pt,
  includefoot,
  foot = 0mm,
  bottom = 25mm 
]{geometry}

\usepackage{blindtext}

\newcommand{\answerspace}{%
  \par\leaders\hbox to \textwidth{\vrule width 0pt height 1cm\hrulefill}\vfill
  \clearpage
}

\begin{document}

\begin{questions}

\question short question\answerspace

\question \blindtext[3]\answerspace

\end{questions}

\end{document}

enter image description here

If you want to specify the thickness and the color, change into

\newcommand{\answerspace}[2]{% #1 = thickness, #2 = color
  \par\leaders\hbox to \textwidth{\color{#2}\vrule width 0pt height 1cm\leaders\hrule height #1\hfill}\vfill
  \clearpage
}

and call it with, for instance,

\answerspace{0.5pt}{red}

An easier way to specify indipendently the parameters is to use a key-value syntax: here I use d for the distance between lines, t for the thickness and c for the color.

\documentclass{exam}
\usepackage[
  a4paper,
  left = 10mm,
  right = 10mm,
  includehead, 
  top = 10mm,
  headheight = 60mm,
  headsep = 0pt,
  includefoot,
  foot = 0mm,
  bottom = 25mm 
]{geometry}
\usepackage{xparse,xcolor}

\usepackage{blindtext}

\newcommand{\answerspace}{%
  \par\leaders\hbox to \textwidth{\vrule width 0pt height 1cm\hrulefill}\vfill
  \clearpage
}

\ExplSyntaxOn
\keys_define:nn { diaa / answers }
 {
  d .dim_set:N = \l__diaa_answers_distance_dim,
  t .dim_set:N = \l__diaa_answers_thickness_dim,
  c .tl_set:N  = \l__diaa_answers_color_tl,
  d .initial:n = 10mm,
  t .initial:n = 0.2pt,
  c .initial:n = .,
 }

\NewDocumentCommand{\FillAnswerRules}{O{}}
 {
  \group_begin:
  \par
  \keys_set:nn { diaa / answers } { #1 }
  \leaders \hbox:n
   {
    \makebox[\textwidth][s]{
      \color{\l__diaa_answers_color_tl}
      \vrule width 0pt height \l__diaa_answers_distance_dim % the distance
      \leaders\hrule height \l__diaa_answers_thickness_dim\hfill
    }
   }\vfill
  \clearpage
  \group_end:
 }

\ExplSyntaxOff

\begin{document}

\begin{questions}

\question short question\FillAnswerRules[d=2cm]

\question \blindtext[3]\FillAnswerRules[t=1pt,c=red]

\end{questions}

\end{document}

enter image description here

egreg
  • 1,121,712