[Tex/LaTex] How to fill vertical spaces with dotted horizontal lines

boxesspacing

I'm sure this must be a duplicate, but I can't find it.

I'm preparing a worksheet for students to use when starting to work on an assignment. Here is a (naturally sweetened) version of the code:

\documentclass{article}
\usepackage{kantlipsum}
\begin{document}
  \begin{flushleft}
    Toffee or caramel? \dotfill\medskip

    Fudge fantasy fiction: \dotfill\medskip

    Curious confectionery connections: \dotfill\medskip\par\mbox{}\dotfill\bigskip
  \end{flushleft}

  \kant[1]\bigskip

  \begin{flushleft}
    What do you like best about your preferred confection?\vfill

    What are the health implications?\vfill

    Does your preferred confection taste really good or just good?
    If the latter, how might it be improved?\vfill

  \end{flushleft}
\end{document}

This produces:

worksheet (sweetened)

I would like to replace the spaces created by the \vfill commands with dotted, horizontal lines. Is it possible to do this neatly/nicely and automatically?

I'm aware that packages for exams and tests offer something like this facility in some cases, but I cannot use a specialised exam class in this case because this is just one page in a packet of materials.

There's also little problem filling whole pages this way e.g. to create lined or squared paper of various kinds.

This answer addresses a similar question but I am not sure how to modify it, and I don't want to have to hard code the text width.

I can do this with TikZ if necessary, but I was hoping there might be an easier way.

Best Answer

Here is the code stolen from exam class and modified slightly.

\documentclass{article}
\usepackage{kantlipsum}

\makeatletter
\newlength\dottedlinefillheight
\setlength\dottedlinefillheight{.25in}

\def\fillwithdottedlines{%
  \begingroup
  \ifhmode
    \par
  \fi
  \hrule height \z@
  \nobreak
  \setbox0=\hbox to \hsize{\hskip \@totalleftmargin
          \vrule height \dottedlinefillheight depth \z@ width \z@
          \dotfill}%
  \cleaders \copy0 \vfill \hbox{}%
  \endgroup
}
\makeatother

\begin{document}

  \begin{flushleft}
    Toffee or caramel? \dotfill\medskip

    Fudge fantasy fiction: \dotfill\medskip

    Curious confectionery connections: \dotfill\medskip\par\mbox{}\dotfill\bigskip
  \end{flushleft}

  \kant[1]\bigskip

  \begin{flushleft}
    What do you like best about your preferred confection?\fillwithdottedlines

    What are the health implications?\fillwithdottedlines

    Does your preferred confection taste really good or just good?
    If the latter, how might it be improved?\fillwithdottedlines

  \end{flushleft}
\end{document}

enter image description here

Related Question