Keeping a diagram + question on the same page/column with the exam class

I use the exam class to write multiple choice questions, and I frequently use tikz to make a diagram that precedes a question. How can I make sure that each diagram+question doesn't break across a column or page break?

Wrapping the tikzpicture and question in a minipage works for the 2nd question onward, but not for the very first question after \begin{questions}. Is there a way I can do something similar for the first question?

This works:

\documentclass[twocolumn]{exam}
\usepackage{tikz}

\begin{document}
\twocolumn

\begin{questions}

\question What's the length of the line above?
\begin{choices}
\choice 1
\choice 8
\end{choices}

\begin{minipage}{\linewidth}
\begin{tikzpicture}
\draw (0,0) -- (2,0);
\end{tikzpicture}

\question What's the length of the line above?
\begin{choices}
\choice 2
\choice 1
\end{choices}
\end{minipage}

\end{questions}

\end{document}


But this doesn't compile (presumably since \begin{questions} expects \question right away):

\documentclass[twocolumn]{exam}
\usepackage{tikz}

\begin{document}
\twocolumn

\begin{questions}

\begin{minipage}{\linewidth}
\begin{tikzpicture}
\draw (0,0) -- (2,0);
\end{tikzpicture}

\question What's the length of the line above?
\begin{choices}
\choice 2
\choice 1
\end{choices}
\end{minipage}

\question What's the length of the line above?
\begin{choices}
\choice 1
\choice 8
\end{choices}

\end{questions}

\end{document}


Welcome to TeX.SE! I am not sure I understand the purpose of the minipage here. Regardless of whether or not you want to use it, here is a simple cheat that allows you to add a picture above a question. It adds an item, but does not print the number and makes sure the counter does not get increased.

\documentclass[twocolumn]{exam}
\usepackage{tikz}
\begin{document}
\twocolumn

\begin{questions}

\begin{pictureabovequestion}
\begin{tikzpicture}
\draw (0,0) -- (2,0);
\end{tikzpicture}
\end{pictureabovequestion}
\question What's the length of the line above?
\begin{choices}
\choice 1
\choice 8
\end{choices}

\begin{pictureabovequestion}
\begin{tikzpicture}
\draw[blue] (0,0) -- (1,0);
\end{tikzpicture}
\end{pictureabovequestion}
\question What's the length of the line above?
\begin{choices}
\choice 2
\choice 1
\end{choices}

\end{questions}

\end{document}


• Thanks! If I wasn't clear, I was trying to use minipage to prevent page/column breaks between either the picture and the question or the question and its answers. minipage works around your "pictureabovequestion" environment, so your answer helps. – lwad Oct 2 '18 at 3:25
• @creakyshrimp You're welcome! (A single tikzpicture won't get broken up, but yes, if you have a whole bunch of things, a minipage does make sense.) – user121799 Oct 2 '18 at 3:28