I think this is a combination of problems. First \fillwithgrid
only draws complete squares with a specific size (5 mm per default), this makes it very unlikely that the resulting grid will be exactly the same width as \linewidth
(\textwidth
does not take into account that the line is shorter due to the enumeration of the question)
I thought that using a fraction of the \linewidth
as size of the squares should avoid this problem, e.g.
\setlength{\gridsize}{\dimexpr.1\linewidth-11\gridlinewidth}
\fillwithgrid{2cm}
but for reasons unknown to me, this draws only 9 squares instead of the 10 ones I was expecting....
Resorting to the heavy tikz
weapons instead
\documentclass[addpoints,a4paper,12pt,answers]{exam}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{tikz}
\begin{document}
\begin{questions}
\question[4] Warum gibt es Luft?
\lipsum[1]
\fillwithdottedlines{2cm}
\bigskip
\begin{tikzpicture}%
\draw[step=0.5,black,thin] (0,0) grid (\linewidth,2cm);
\end{tikzpicture}%
\end{questions}
\end{document}
With this approach als calculating the size of the squares as a fraction of the \linewidth
works:
\documentclass[addpoints,a4paper,12pt,answers]{exam}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{tikz}
\begin{document}
\begin{questions}
\question[4] Warum gibt es Luft?
\lipsum[1]
\fillwithdottedlines{2cm}
\bigskip
\begin{tikzpicture}%
\draw[step=0.05\linewidth-0.01pt,black,thin] (0,0) grid (\linewidth,0.2\linewidth);
\end{tikzpicture}%
\end{questions}
\end{document}