我目前正在尝试使用 来获取此显示TikZ
,我设法获得了正确的显示,但它并不完美。让我解释一下。
这是我编写的代码,用于获取以下显示
\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}
\def\mustnt{I will not talk in class.}
\def\five{\mustnt\mustnt\mustnt\mustnt\mustnt}
\def\twenty{\five\five\five\five}
\def\punishement{\twenty\twenty\twenty\twenty\twenty}
\end{verbatim}
\end{minipage}
};
\end{tikzpicture}\shorthandon{:}\end{center}
\end{document}
这是我一直在寻找的显示,但我试图定义一个宏或(环境可能我不确定),它将提供正确的显示,但也可以因大小而异,这样如果文本较短,框架就会小一些等等...
这样,我可以输入这个显示内容(请注意之前的不同大小)
我得到最后一个显示的方式是通过改变
\rectangle
尺寸
另外,不要介意
\shorthandoff{:}
,只是我必须使用 babel 包,因为我用法语写作,而且我不希望它干扰包TikZ
。
答案1
正如@leandriis 所建议的,为什么不使用tcolorbox
?它是一个旨在构建彩色盒子并具有管理.tex
示例和练习能力的包。
\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]
\def\mustnt{I will not talk in class.}
\def\five{\mustnt\mustnt\mustnt\mustnt\mustnt}
\def\twenty{\five\five\five\five}
\def\punishement{\twenty\twenty\twenty\twenty\twenty}
\end{solution}
\section{Second section}
\begin{exercice}
Considering the \verb+\def\row#{(#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}