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 :)

7
  • 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. ctan.org/pkg/hyperref
    – MS-SPO
    Oct 8, 2023 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 8, 2023 at 18:19
  • 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 9, 2023 at 3: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 9, 2023 at 3: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) Oct 9, 2023 at 15:02

1 Answer 1

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.

6
  • 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 8, 2023 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 9, 2023 at 2:42
  • 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. Oct 9, 2023 at 6: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 9, 2023 at 6:11
  • 1
    @AndrewStacey But I've bowed to tikzmark pressure. :)
    – Alan Munn
    Oct 9, 2023 at 14:50

You must log in to answer this question.

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