1

When inserting space for answers by \fillwithdottedlines the resulting lines have the same width as textwidth. When using \fillwithgrid the resulting grid is smaller on both sides:

\documentclass[addpoints,a4paper,12pt,answers]{exam}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}     

\begin{document}

\begin{questions}
\question[4] Warum gibt es Luft?

\lipsum[1]

\fillwithdottedlines{2cm}

\bigskip

\fillwithgrid{2cm}

\end{questions}

\end{document}

How to get the exact same width?

The solution in an older post was to change \textwidth. But this only extends the grid on the right side.

0

2 Answers 2

1

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}

enter image description here

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}

enter image description here

3

The code of exam.cls uses \leaders for horizontal positioning, in a hbox taking up the full line width, and an initial whitespace of the total left margin, in a list environment. This feature of the class means that the grid squares will look vertically aligned independently of the actual left margin. But this left margin has no chance of being an exact multiple of the unit grid size, so the starting point of the squares looks a bit arbitrary and if you use a large value always systematically shifted the right because its absolute horizontal positioning must start at an integer multiple of gridsize from left border of text area.

In the code here I patch the \list to have a left margin of 2 gridsizes. And I adjust the gridsize so that an integer multiple fits in the whole width. This gives a modified gridsize of 5.13mm in place of 5mm.

\documentclass[addpoints,a4paper,12pt,answers]{exam}
\usepackage{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}     

\usepackage{etoolbox}
\patchcmd\questions{\settowidth{\leftmargin}{10.\hskip\labelsep}}
                   {\leftmargin=2\gridsize}
                   {\typeout{PATCH SUCCEEDED}}
                   {\PATCHFAILED}
\begin{document}

\makeatletter
\count@ \dimexpr\textwidth-\gridlinewidth\relax
\divide\count@ by \numexpr\dimexpr5mm\relax\relax
\gridsize\dimexpr(\textwidth-\gridlinewidth)/\count@\relax

\typeout{WE MODIFIED \string\gridsize\space TO \the\gridsize, i.e.
         \the\numexpr\gridsize*15875/29601792 mm/100
         SO WHEN USED INSIDE QUESTION THERE ARE \the\numexpr\count@-2\relax
         \space SQUARES IN HORIZONTAL LINES}
\makeatother

\begin{questions}
\question[4] Warum gibt es Luft?

blah blah blah (\number\gridsize)

\fillwithdottedlines{2cm}

\bigskip
\noindent X\dotfill X\par

\bigskip

\fillwithgrid{2cm}
\end{questions}

\end{document}

enter image description here

WE MODIFIED \gridsize TO 14.60864pt, i.e. 513mm/100 SO WHEN USED INSIDE QUESTION THERE ARE 29 SQUARES IN HORIZONTAL LINES
2
  • maybe real life will need 3\gridsize not 2\gridsize to accomodate question numbers with 2 digits.
    – user4686
    Jan 19, 2019 at 18:09
  • an alternative is to rewrite \fillwithgrid to add an \hbox layer containing the leaders after the initial \hskip\@totalleftmargin so the square will be correctly aligned whathever the unit width with the indentation (left margin) defined by the environment. But of course then the unit width has also to be adjusted if the available space is to be filled completely.
    – user4686
    Jan 19, 2019 at 22:04

You must log in to answer this question.

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