TransWikia.com

Using TikZ to get correct framing

TeX - LaTeX Asked on June 10, 2021

I am currently trying to get this display using TikZ, and i managed to get the correct display but it is not exactly perfect. Let me explain.

This is the code that i wrote to get the following display

documentclass{article}
usepackage{tikz,tkz-tab,pgf} %pour TikZ

begin{document}

begin{center}shorthandoff{:}begin{tikzpicture}
draw[blue,fill=blue!20!white,rounded corners] (0, 0.5) rectangle (16, 2.5);
node at (8,1.5) {
    begin{minipage}{15cm}
        textcolor{blue}{Exercice 20.1:} Write a verb"punishement" marco that prints 100 lines containing the message og I must not talk in class.fg quad[textit{Hint:} Frist write a macro verb"mustnt" that prints the message once; then write a macro verb"five" that prints it five times.]
    end{minipage}
};
draw[green,fill=green!20!white,rounded corners] (0, -3) rectangle (16, 0);
node at (8,-1.5) {
    begin{minipage}{15cm}
        textcolor{blue}{Solution:}
        begin{verbatim}
            defmustnt{I will not talk in class.}
            deffive{mustntmustntmustntmustntmustnt}
            deftwenty{fivefivefivefive}
            defpunishement{twentytwentytwentytwentytwenty}
        end{verbatim}
    end{minipage}
};
end{tikzpicture}shorthandon{:}end{center}

end{document}

Which gave the display enter image description here

This is the display that i was looking for, but i am trying to define a macro or (environment maybe im not sure), that will give the correct display, but can also vary from size to size, so that if the text is shorter, the framing is a little smaller etc…

That way, i can type this display for example (notice the different sizes that before)

The way i got the last display is by changing the rectangle dimensions

Also, don’t mind the shorthandoff{:}, it’s just that i have to use the babel package because i write in french, and i don’t what it to interfere with the TikZ package.

enter image description here

One Answer

As @leandriis suggested, why not to use tcolorbox? It's a package designed to build colorful boxes and with capacity to manage .tex examples and exercices.

documentclass{article}
usepackage[T1]{fontenc}
usepackage{lmodern}
usepackage[most]{tcolorbox}

newtcolorbox[auto counter, number within=section] {exercice}[1][]{%
    colback=blue!20!white,
    colframe=blue,
    width=16cm,
    attach title to upper={: },
    coltitle=blue,
    title={Exercice~thetcbcounter},
    #1
}

newtcblisting{solution}[1][]{%
    colback=green!20!white,
    colframe=green,
    width=16cm,
    attach title to upper={: },
    coltitle=blue,
    title={Solution},
    #1
}

begin{document}

section{First section}

begin{exercice}
 Write a verb"punishement" marco that prints 100 lines containing the message << I must not talk in class.>> quad[textit{Hint:} Frist write a macro verb"mustnt" that prints the message once; then write a macro verb"five" that prints it five times.]
end{exercice}

begin{solution}[listing only]
defmustnt{I will not talk in class.}
deffive{mustntmustntmustntmustntmustnt}
deftwenty{fivefivefivefive}
defpunishement{twentytwentytwentytwentytwenty}
end{solution}

section{Second section}

begin{exercice}
Considering the verb+defrow#{(#1_1,ldots,#_n)}+ macro, what is the result of verb+$row{bf x}$+
end{exercice}

begin{solution}[text only]
The result will be $(bf{x_1,ldots,x_n})$. We can see that everything is in bold. To only get the $x$ in verb+bf+, we need to call verb+$row{{bf x}}$+
end{solution}

end{document}

enter image description here

Correct answer by Ignasi on June 10, 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