框架答案环境

框架答案环境

我正在尝试为我给出的评估解决方案编写一个简单的环境。下面的代码定义了这样一个环境:

 \newenvironment{answerenv}[1][Answer]{% Sets the default "Answer" but can be changed to be reused.
   \vskip1.5\baselineskip
   \MakeFramed {\FrameRestore}
   \noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white,
          anchor=west, overlay] at (0em, 2em) {\normalcolor\sffamily#1};}%
 {\endMakeFramed}

 \newcommand{\answer}[1]{% This is to avoid typing then environment over and over
        {\color{red}%
        \begin{answerenv}
        {#1}
        \end{answerenv}
        }}

这是我实现上述代码的方式:

 \documentclass[letterpaper]{article}
 \usepackage[top=1.5cm,bottom=1.5cm,left=1.5cm,right=1.5cm]{geometry}
 \usepackage{amsmath,amssymb,enumitem}
 \usepackage[dvipsnames]{xcolor}
 \usepackage{framed,tikz}
 \parindent0pt
 \newenvironment{answerenv}[1][Answer]{%
 \vskip1.5\baselineskip
\MakeFramed {\FrameRestore}
\noindent\tikz\node[inner sep=1ex, draw=black!20,fill=white, anchor=west, overlay] at (0em, 2em) {\color{black}\sffamily#1};}%
 {\endMakeFramed}
 \newcommand{\answer}[1]{%
        {\color{red}%
        \begin{answerenv}
        {#1}
        \end{answerenv}
        }}
 \begin{document}
    \begin{enumerate}
    \item Is $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ equal to $5y^2z^6$?
          \answer{Let us simplify $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ and verify whether or not it is equal to $5y^2z^6$. Thus,
          \begin{align*}
          5x\left(x^{-\frac{1}{2}}yz^3\right)^2&=5x\left(x^{-1}y^2z^6\right)\\
                                               &=5y^2z^6
          \end{align*}
          Hence, $5x\left(x^{-\frac{1}{2}}yz^3\right)^2=5y^2z^6$
          }
    \item Solve $2x+3=-2(4-7x)-2$.
          \answer{
          \begin{align*}
          2x+3&=-2(4-7x)-2\\
          2x+3&=-8+14x-2\\
          14x-2x&=3+8+2\\
          12x&=13\\
          x&=\frac{13}{12}
          \end{align*}}
    \item Is division commutative? Support your conclusion with an example.
          \answer{If division is commutative then this means that \[a\div b=b\div a\] is true. It will suffice to say that \[4=8\div 2\neq 2\div 8=0.25\].}
    \item Is 1 prime or composite?
          \answer{1 is neither prime nor composite. If it were prime then it would have only two distinct factors, one and itself, which does not have. To be composite would mean to have more than two factors and thus does not satisfy any of the above. }
    \end{enumerate}
 \end{document} 

这是输出:在此处输入图片描述

如您所见,存在几个问题:

  1. 首先,item单词“Answer”大致位于我想要的位置,但您仍然可以看到红色框。我希望包含单词“Answer”的框与框线垂直居中。

  2. 红色框没有使用枚举环境缩进。注意我希望环境中的内容缩进,如下所示。

  3. 环境不一致。

以下是我想要实现的目标:

在此处输入图片描述

答案1

这是一个快速而肮脏的解决方案mdframed

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}


\global\mdfdefinestyle{redbox}{%
linewidth=2pt,
linecolor=red,
innertopmargin=1.2\baselineskip,
skipabove={\dimexpr0.5\baselineskip+\topskip\relax},
needspace=2\baselineskip,
frametitlefont=\sffamily\bfseries,
frametitlefontcolor=black,
fontcolor=red,
innerleftmargin=1em,
leftmargin=-1em,
innerrightmargin=1em,
rightmargin=-1em,
innerbottommargin=1em
}
\makeatletter
\renewrobustcmd\mdfcreateextratikz{\node[black,fill=white,xshift=1cm] at (P-|O) {\mdf@frametitlefont{Answer}};}
\makeatother

\begin{document}

\lipsum[3]
\begin{mdframed}[style=redbox]
\lipsum[2]
\end{mdframed}

\end{document}

在此处输入图片描述

答案2

使用的示例tcolorbox适用于框架盒。

\documentclass{article}
\usepackage{amsmath}
\usepackage[most]{tcolorbox}

\newtcolorbox{myanswer}[1][]{%
    enhanced, title=Answer, colframe=red, 
    colback=white, sharp corners, colupper=red,
    fonttitle=\ttfamily,coltitle=black,
    attach boxed title to top left = {xshift=5mm,yshift=-2.5mm} ,
    boxed title style={size=small, colframe=gray, sharp corners,colback=white},
    #1}

\begin{document}

\begin{enumerate}
\item Is $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ equal to $5y^2z^6$?

\begin{myanswer}
Let us simplify $5x\left(x^{-\frac{1}{2}}yz^3\right)^2$ and verify whether or not is equal to $5y^2z^6$. Thus,
\[\begin{split}5x\left(x^{-\frac{1}{2}}yz^3\right)^2 & = 5x(x^{-1}y^2z^6)\\
& = 5y^2z^6\end{split}\]
Hence, $5x\left(x^{-\frac{1}{2}}yz^3\right)^2 = 5y^2z^6$
\end{myanswer}
\end{enumerate}

\end{document}

在此处输入图片描述

相关内容