1

I have this code that I have obtained sometime ago from this site (sorry I don't know the exact source). It allows me to create a multiple choice answer sheet.

I have modified it to some extent and would like to know how do I draw an adjustable rectangle around the bubbles as shown below.

My MWE:

\documentclass{exam}
\usepackage{tikz}
\usepackage{multicol}

\newcommand\encircle[2][]{% 
    \indent\tikz[baseline=(X.base)]
    \node (X)    [draw, shape=circle, inner sep=1pt, #1] {\strut$#2$};
}

\ExplSyntaxOn

\newcommand{\answerspace}[1]{%
    
            \foreach \x in {1,...,#1}  {%
            \noindent\hspace*{10mm}\llap{\textbf{\x}}\hspace{5mm}\space
            \foreach \y in {1,2,3,4} {% 
                \l_encircle {\x}{\y} \ %
            }\newline\newline%
        }%

    }


\cs_new_protected:Npn \l_encircle #1#2 {    { \encircle{\int_to_Alph:n{#2}} }
}
\ExplSyntaxOff

\begin{document}
\begin{center}

\begin{multicols}{2}
\normalsize 
\answerspace{24}
\end{multicols}

\end{center}



\end{document}

Results

1 Answer 1

3

If you want to keep using your code, then this solution works.

Basically I renamed your command to something else, and put it inside of another \newcommand that will add the boxes later, which you can customize by changing the border color or width.

I added a counter to your \foreach commands and used those to rename your nodes as row-column for better reference. Then I used those to get the anchors needed for the boxes.

This solution assumes:

  • that you will keep using 4 answers, i.e. A, B, C, D
  • that you will keep using this format for the cheatsheet

So if you want further customisation, that's possible but I'd rather rebuild your code from scratch so that I'm sure it's robust and works for other use cases.

Note: If you change the number of questions, you need to compile twice!

Output

enter image description here

Code

\documentclass{exam}
\usepackage{tikz}
\usepackage{multicol}

\newcommand\encircle[2][]{% 
    \indent\tikz[baseline=(\nx-\ny.base), remember picture]{
    \node (\nx-\ny) [draw, shape=circle, inner sep=1pt, outer sep=10pt, #1] 
    {\strut$#2$};
    }
}

\ExplSyntaxOn

\newcommand{\createrows}[1]{%
            \foreach \x [count=\nx] in {1,...,#1}  {%
            \noindent\hspace*{10mm}\llap{\textbf{\x}}\hspace{5mm}\space
            \foreach \y [count=\ny] in {1,2,3,4} {% 
                \l_encircle {\x}{\y} \ %
            }\newline\newline%
        }%
    }
\cs_new_protected:Npn \l_encircle #1#2 {    { \encircle{\int_to_Alph:n{#2}} }
}
\ExplSyntaxOff

\newcommand\answerspace[2][]{%
\createrows{#2}%
\begin{tikzpicture}[overlay, remember picture]
    \pgfmathtruncatemacro\halfofrows{#2/2}
    \pgfmathtruncatemacro\secondhalfofrows{#2/2+1}
    \draw[line width=1pt,#1] (1-1.north west) rectangle (\halfofrows-4.south east);
    \draw[line width=1pt,#1] (\secondhalfofrows-1.north west) rectangle (#2-4.south east);
\end{tikzpicture}
}

\begin{document}
\begin{multicols}{2}
    \centering
\normalsize 
\answerspace{24}
\end{multicols}
\end{document}
4
  • @ Alenanno Thanks for that, I noticed you used two rectangles that overlap. Quick question, where can I learn how to create macros in LaTeX? Do you know a source/website?
    – Alan Jones
    Commented Nov 8, 2020 at 19:35
  • 1
    @AlanJones Where do they overlap? Well, I learned most of it on this website.
    – Alenanno
    Commented Nov 8, 2020 at 19:50
  • They over lap when I try to generate a \answerspace{10} without \multicols{}
    – Alan Jones
    Commented Nov 9, 2020 at 4:35
  • 1
    @AlanJones ah ok, that's why I said that it was based on your code. If you want it more dynamic I might need to rebuild the code from scratch. To fix it quickly though, you can delete the second rectangle, and give the second coordinates of the deleted rectangle to the one that stayed.
    – Alenanno
    Commented Nov 9, 2020 at 6:35

You must log in to answer this question.

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