1

I desire to have the letter aligned to the top of the figure. I am unsure how to do this. I believe the choices environment is similar to that of an enumerate environment, but I am unsure how to modify that similar.

This is what it looks like:

What it looks like

This is what I desire:

what I desire

\documentclass[%
12pt, 
a4paper,
]{exam}

\newlength{\figsize}
\setlength{\columnsep}{0pt}

\usepackage{tikz}
\usepackage{multicol}

\newcommand{\diagram}{
\begin{tikzpicture}
\draw[rounded corners]  (-2.0cm, 5cm) rectangle (5cm,-2.0cm);
\end{tikzpicture}
}

\begin{document}
    \begin{questions}
        \pointsinmargin\pointformat{}      
        
        \setlength{\figsize}{0.35\textwidth}

        \question[1]%
        Here is a question that fills up some lines.
        
        \begin{multicols}{2}
        \begin{choices}
            \choice \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
        \end{choices}
        \end{multicols}
\end{questions}
\end{document}

p.s. If it's possibly to automatically set \figsize based on the columns - width of the letter A., that would be great, but I can't find any mention of something containing the column width in the multicol documentation.

2

2 Answers 2

2

You can use the baseline key to shift the baseline of a tikzpicture. In that key you can use current bounding box to reference the size of the current picture. And using the calc library we can shift that point downwards by the height of the \strutbox to get decent looking top alignment compared to the letters.

To set the \figsize automatically you can store M. inside a box (M is most likely the widest letter of your font), and set the \figsize to the current line width minus the width of that box (using \dimexpr such things are possible).

Full code:

\documentclass[%
12pt, 
a4paper,
]{exam}

\newlength{\figsize}
\setlength{\columnsep}{0pt}

\usepackage{tikz}
\usepackage{multicol}

\usetikzlibrary{calc}

\makeatletter
\newcommand{\diagram}
  {%
    \setfigsize
    \begin{tikzpicture}[baseline={($(current bounding box.north)-(0,\ht\strutbox)$)}]
      \draw[rounded corners]  (0,0) rectangle (\figsize,\figsize);
    \end{tikzpicture}%
  }
\newcommand*\setfigsize
  {%
    \sbox0{M.}% M should be the widest letter
    \setlength\figsize{\dimexpr\linewidth-\wd0\relax}%
  }
\makeatother

\begin{document}
    \begin{questions}
        \pointsinmargin\pointformat{}      
        
        \setlength{\figsize}{0.35\textwidth}

        \question[1]%
        Here is a question that fills up some lines.
        
        \begin{multicols}{2}
        \begin{choices}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
        \end{choices}
        \end{multicols}
\end{questions}
\end{document}

enter image description here

If you want to top align an image file you can use \adjustimage from the adjustbox package instead of \includegraphics (note that the \adjustimage doesn't use an optional argument but a mandatory one, it supports all the options of graphicx plus the adjustbox specific ones). If you want to top align something arbitrary (this would also work with a tikzpicture) you can use \adjustbox{valign=t}{<stuff>}.

Example:

\documentclass[%
12pt, 
a4paper,
]{exam}

\newlength{\figsize}
\setlength{\columnsep}{0pt}

\usepackage{adjustbox}
\usepackage{multicol}

\makeatletter
\newcommand{\diagram}
  {%
    \setfigsize
    \adjustimage{valign=t, width=\figsize}{example-image-duck}%
  }
\newcommand*\setfigsize
  {%
    \sbox0{M.}% M should be the widest letter
    \setlength\figsize{\dimexpr\linewidth-\wd0\relax}%
  }
\makeatother

\begin{document}
    \begin{questions}
        \pointsinmargin\pointformat{}      
        
        \setlength{\figsize}{0.35\textwidth}

        \question[1]%
        Here is a question that fills up some lines.
        
        \begin{multicols}{2}
        \begin{choices}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\diagram\end{center}
            \choice \begin{center}\adjustbox{valign=t}{\rule{10pt}{50pt}}\end{center}
        \end{choices}
        \end{multicols}
\end{questions}
\end{document}

Result:

enter image description here

4
  • Ah! Thanks. Is this possible if the exported tikz files have been saved as pdfs and included with \includegraphics? Since posting this (of course) I've reached Overleaf's compilation limit, and used this solution to improve compile times. This means I'm just importing pdfs, and it doesn't seem to work if I just paste the [baseline=...] part in the other project. Commented Sep 19, 2021 at 7:47
  • Alternatively - what if I don't have tikz pictures at all, and instead they are all pictures? I believe the same problem would arise. Commented Sep 19, 2021 at 7:48
  • @Modelmat \usepackage{adjustbox} then use \adjustimage{valign=t}{<file>} instead of \includegraphics{<file>}.
    – Skillmon
    Commented Sep 19, 2021 at 10:18
  • 1
    @Modelmat see the edit.
    – Skillmon
    Commented Sep 19, 2021 at 10:27
1

A solution with \vspace{-\baselineskip}

\documentclass[%
12pt, 
a4paper,
]{exam}

\newlength{\figsize}
\setlength{\columnsep}{0pt}

\usepackage{tikz}
\usepackage{multicol}

\newcommand{\diagram}{
\begin{tikzpicture}
\draw[rounded corners]  (-2.0cm, 5cm) rectangle (5cm,-2.0cm);
\end{tikzpicture}
}

\begin{document}
    \begin{questions}
        \pointsinmargin\pointformat{}      
        
        \setlength{\figsize}{0.35\textwidth}

        \question[1]%
        Here is a question that fills up some lines.
        
        \begin{multicols}{2}
        \begin{choices}
            \choice \mbox{}\vspace{-\baselineskip}
            \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \mbox{}\vspace{-\baselineskip}
            \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \mbox{}\vspace{-\baselineskip}
            \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
            \choice \mbox{}\vspace{-\baselineskip}
            \begin{center}\resizebox{\figsize}{!}{\diagram}\end{center}
        \end{choices}
        \end{multicols}
\end{questions}
\end{document}
2
  • This seems to break if I have \usepackage{enumitem}. They are not high enough. I need to add \vspace{-\labelsep} as well. Commented Sep 19, 2021 at 8:28
  • @Modelmat That's right! Commented Sep 19, 2021 at 8:44

You must log in to answer this question.

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