3

I use LaTeX for a lot of my classwork (worksheets, graphics, etc). One thing I have not transitioned is my exam writing. An example page of an exam might look like this. This is just a portion, doesn't have the header and such, I'm comfortable working with those.

example

I've not moved to LaTeX because of my need to be placing the answer boxes and blanks in specific spots on the page, and it's frankly pretty easy to drag little rounded text boxes around MS Word to achieve my layout. But there could be a fair bit of programmatic benefit in being able to make some of those blanks more consistently, layout chemical equations more simply, etc. I also have a page of multiple choice for each exam that I'd love to shuttle off a lot of that layout work to LaTeX.

Is achieving looks like this reasonable with enough flexibility to be able to shift around those blanks and such on a question-by-question basis? I've found this page about using a zero-sized box. Seems like it be applicable, but just want to check before I start down this path. Any resources or general code ideas for doing what I'd like would be appreciated. I'm sure in the future I'll come back with some MWE, but for now just gathering info on even executing this.

Also if the answer is "Probably more work than it's worth and keep using MS Word", I can accept that too :)

J M
  • 1,102
  • Hard to answer. If I had this situation, I'd do a few test documents to better understand how to do it, what underlying problems may or may not be, discover new opportunities via Latx/pdf etc. // Besides the exam-classes you may also want to have a look at hyperref, which provides those nice text-and checkboxes you may have seen on pdf-forms. https://ctan.org/pkg/hyperref – MS-SPO Oct 08 '23 at 17:23
  • 4
    Looking at the sample you've given, it doesn't look like you really need absolute positioning; you just think you do because that's how you've been doing it in Word. :) But even so, absolute positioning is quite simple to do with tikz and the tikzmark library. You can mark arbitrary points in the text and then place boxes at those points. – Alan Munn Oct 08 '23 at 18:19
  • Can Word randomise the order of questions? Can it print different versions of a multiple choice section with different orderings? Can it generate answer keys for each variant? But actually, I'm amazed you're not forced to do this in a virtual learning environment ... :(. – cfr Oct 09 '23 at 03:21
  • @cfr Haha sorry I'm not sure I follow :) I don't really need all those features because my exams are all given at once to the students, so there's no need for scrambling and such. I just write one version usually. A few assessments are given online depending on the class, and then the LMS quiz tools handle all those randomization aspects. – J M Oct 09 '23 at 03:28
  • 1
    You might want to randomise anyway. Having the facility to randomise makes it easier to avoid the effects of human bias when you write questions. (People tend, statistically, to put the correct choice at some positions rather than others.) Either you can have the code randomise the choices (rather than the questions) or you can order the questions by correct choice (all the As, then the Bs etc.) and randomise the questions. It's much easier to make yourself distribute correct choices equally when you can group them. There's more programmatic pluses than just placing boxes, is all I meant. – cfr Oct 09 '23 at 03:36
  • 1
    @cfr Oh yes, I understand now. For the multiple choice, I generate a random answer order list that I use for those few questions to avoid the issue described (it's only like 6 or so questions). Though ~80% of the exam is non-multiple choice as you can see in the above example :) Plenty of other gains to be made from the switch, the boxes were my tripping point though in making it happen! – J M Oct 09 '23 at 03:42
  • 2
    The textpos package is billed as ‘textpos – Place boxes at arbitrary positions on the LaTeX page’ and might be worth a look (it essentially wraps the ‘zero-sized box’ technique mentioned in passing in another comment) – Norman Gray Oct 09 '23 at 15:02

1 Answers1

7

Here's an expansion of my comment, showing proof of concept. We create a command to place an answer box a given horizontal distance from where the command is placed. You'll need to allow for vertical height yourself, although that could also be added to the command if it was required. This doesn't actually need the tikzmark library that I mentioned in the comment; it just places the box directly. You can pass explicit xshift and yshift parameters to the \tikz command to position the box pretty much anywhere you like if needed.

\documentclass{article}
\usepackage{tikz}
\tikzset{answer box/.style={draw,rounded corners,minimum size=1cm,text width=1cm}}
\NewDocumentCommand{\answerbox}{O{1cm}m}{\hspace*{#1}\tikz[overlay]{\node[answer box,#2,] {}}}
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\bfseries{Q}\arabic*,itemsep=2\baselineskip]
\item What is the velocity of an unladen swallow?\answerbox[\fill]{text width=3cm,label={Answer}}
\item Who expects the Spanish Inquisition?\answerbox[1cm]{label=Answer}
\item Calculate the answer to life, the universe, and everything.\\[2\baselineskip]\answerbox[2.5cm]{text width=5cm,label=Final Answer}
\item Do I really need to use \texttt{tikzmark}? See red box!\answerbox{text width=3cm,label={Arbitrary Position},xshift=3.5cm,yshift=3.5cm,red, align=center,node contents={No!}}
\end{enumerate}
\end{document}

output of code

But I want absolute positioning!

If you really do want absolute positioning, here's a version of the command that positions the node at a location set by the \tikzmark command. The syntax of this version of the command is the same as the previous one, but allows you to add an optional node location in parentheses.

\answerbox[hspace](coordinate){further tikz keys}

Here's a full example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
\tikzset{answer box/.style={draw,rounded corners,minimum size=1cm,text width=1cm}}
\NewDocumentCommand{\answerbox}{O{1cm}d()m}{
\IfNoValueTF{#2}
    {\hspace*{#1}\tikz[overlay]{\node[answer box,#3,]  {}}}
    {\hspace*{#1}\tikz[overlay,remember picture]{\node[answer box,#3] at (pic cs:#2) {}}}
    }
\usepackage{enumitem}
\begin{document}
\begin{enumerate}[label=\bfseries{Q}\arabic*,itemsep=2\baselineskip]
\item What is the velocity of an unladen swallow?\answerbox[\fill]{text width=3cm,label={Answer}}
\item Who expects the Spanish Inquisition?\answerbox[1cm]{label=Answer}
\item Calculate the answer to life, the universe, and everything.\\[2\baselineskip]\answerbox[2.5cm]{text width=5cm,label=Final Answer}\hfill\tikzmark{S}
\item Do I really need to use \texttt{tikzmark}? See red box!\answerbox{text width=3cm,label={Arbitrary Position},xshift=3.5cm,yshift=3.5cm,red, align=center,node contents={No!}}
\item But I want absolute positioning, not relative! See purple box!\answerbox(S){text width=3cm,label={Absolute},violet,align=center,font={OK!}}
\end{enumerate}
\end{document}

output of second example

Warning

In these code examples I've used node contents and font to place text inside the boxes. This is not how text should be placed in practice. If you want to supply boxes with content, you should make the content an argument of the command and pass it to the \node command {} argument directly.

Alan Munn
  • 218,180
  • Looks great! Would you possibly be willing to show a version with tikzmark? Just in cases where my alignment is not a straightforward horizontal adjustment from the line text or something? – J M Oct 08 '23 at 19:36
  • 1
    @JM I really don't think that tikzmark is necessary. I've added an example of how you can position a box in an arbitrary way without it. – Alan Munn Oct 09 '23 at 02:42
  • Amazing, wish I could give it a double-upvote :) That's even easier! – J M Oct 09 '23 at 02:47
  • 1
    Your "don't need no tikzmark" example is still relative positioning. For absolute you either need tikmark or the current page pseudonode, depending on how you want to identify the location. – Andrew Stacey Oct 09 '23 at 06:02
  • 1
    @AndrewStacey True, but for this application I couldn't see the value in using tikzmark as well. It was my first thought, but when I mocked up a version using it, it just added lines of code without much else. – Alan Munn Oct 09 '23 at 06:11
  • 1
    @AndrewStacey But I've bowed to tikzmark pressure. :) – Alan Munn Oct 09 '23 at 14:50