TransWikia.com

Drawing an adjustable rectangle in Tikz

TeX - LaTeX Asked on June 1, 2021

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}

newcommandencircle[2][]{% 
    indenttikz[baseline=(X.base)]
    node (X)    [draw, shape=circle, inner sep=1pt, #1] {strut$#2$};
}

ExplSyntaxOn

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

    }


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

One Answer

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}

newcommandencircle[2][]{% 
    indenttikz[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}  {%
            noindenthspace*{10mm}llap{textbf{x}}hspace{5mm}space
            foreach y [count=ny] in {1,2,3,4} {% 
                l_encircle {x}{y}  %
            }newlinenewline%
        }%
    }
cs_new_protected:Npn l_encircle #1#2 {    { encircle{int_to_Alph:n{#2}} }
}
ExplSyntaxOff

newcommandanswerspace[2][]{%
createrows{#2}%
begin{tikzpicture}[overlay, remember picture]
    pgfmathtruncatemacrohalfofrows{#2/2}
    pgfmathtruncatemacrosecondhalfofrows{#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}

Correct answer by Alenanno on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP