5

How to get Example and solution as in the style with boundaries of the attached jpg file.

enter image description here

  \documentclass{book}
\usepackage{graphicx,amssymb,amsmath}
\usepackage{exercise,chngcntr}
\counterwithin{Exercise}{chapter}
\counterwithin{Answer}{chapter}
\renewcounter{Exercise}[chapter]
\renewcommand{\theExercise}{\thechapter.\arabic{Exercise}}
\begin{document}
\begin{Exercise}
Prove that $    \frac{1}{2}\Delta(f_{ij}f^{ij}) =\nabla^{k}f^{ij}+f^{ij}f^{k}[2 \nabla_{i}R_{jk}-\nabla_{k}R_{ij}]$
\end{Exercise}
\begin{Answer}
Answer 2.2
        \end{Answer}
  \end{document}
cgnieder
  • 66,645
user1942348
  • 1,099

2 Answers2

7

I've created the new environment Example which puts the Exercise one within a tcolorbox.

\documentclass{book}
\usepackage{graphicx,amssymb,amsmath}
\usepackage[lastexercise]{exercise}
\usepackage{chngcntr}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbset{arc=0mm,boxrule=1pt,colback=white,colframe=cyan,leftrule=3mm}

\counterwithin{Exercise}{chapter}
\counterwithin{Answer}{chapter}
\renewcounter{Exercise}[chapter]
\renewcommand{\theExercise}{\thechapter.\arabic{Exercise}}
\renewcommand{\AnswerHeader}{\medskip\noindent\textcolor{red}{\textbf{SOLUTION}}\;}
\renewcommand{\ExerciseName}{EXAMPLE}
\renewcommand{\ExerciseHeader}{\noindent\textbf{\ExerciseName\;\ExerciseHeaderNB}}
\newenvironment{Example}{\begin{tcolorbox}\begin{Exercise}}{\end{Exercise}\end{tcolorbox}}
\setlength{\ExerciseSkipBefore}{0pt}
\setlength{\AnswerSkipAfter}{\baselineskip}
\begin{document}
    \chapter{My first set of exercises}
    \begin{Example}
        Prove that $\frac{1}{2}\Delta(f_{ij}f^{ij}) =\nabla^{k}f^{ij}+f^{ij}f^{k}[2\nabla_{i}R_{jk}-\nabla_{k}R_{ij}]$
    \end{Example}
    \begin{Answer}
        From \dots
    \end{Answer}

    \begin{Example}
        Prove that Paulinho is smart.
    \end{Example}
    \begin{Answer}
        All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
    \end{Answer}

    \begin{Example}
        Prove that Paulinho is smart.
    \end{Example}
    \begin{Answer}
        All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
    \end{Answer}

    \chapter{My second set of exercises}
    \begin{Example}
        Prove that Paulinho is smart.
    \end{Example}
    \begin{Answer}
        All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
    \end{Answer}

    \begin{Example}
        Prove that Paulinho is smart.
    \end{Example}
    \begin{Answer}
        All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
    \end{Answer}
\end{document}

I hope this is the output you are looking for:

enter image description here

enter image description here

CarLaTeX
  • 62,716
3

Using the xsim package with tcolorbox (questions and answers stolen from CarLaTeX's answer):

\documentclass{article}
\usepackage{xsim,tcolorbox,needspace}

\xsimsetup{
  exercise/within = section ,
  exercise/the-counter = \thesection.\arabic{exercise} ,
  exercise/template = cyan-box ,
  exercise/name = Example ,
  solution/template = red ,
  solution/print = true
}

\DeclareExerciseEnvironmentTemplate{cyan-box}
  {%
    \begin{tcolorbox}[arc=0mm,boxrule=1pt,colback=white,colframe=cyan,leftrule=3mm]
      \textsf{%
        \MakeUppercase{%
          \XSIMmixedcase{\GetExerciseName}~\GetExerciseProperty{counter}%
        }%
      }%
      \GetExercisePropertyT{subtitle}{ \textit{#1}}%
      \quad
  }
  {\end{tcolorbox}}

\DeclareExerciseEnvironmentTemplate{red}
  {%
    \par
    \Needspace*{2\baselineskip}
    \noindent
    \textsf{\textcolor{red}{\MakeUppercase{\XSIMmixedcase{\GetExerciseName}}}}%
    \quad
  }
  {\par\bigskip}

\begin{document}

\section{My first set of exercises}
\begin{exercise}
  Prove that $\frac{1}{2}\Delta(f_{ij}f^{ij})
  =\nabla^{k}f^{ij}+f^{ij}f^{k}[2\nabla_{i}R_{jk}-\nabla_{k}R_{ij}]$
\end{exercise}
\begin{solution}
  From \dots
\end{solution}

\begin{exercise}
  Prove that Paulinho is smart.
\end{exercise}
\begin{solution}
  All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
\end{solution}

\begin{exercise}
  Prove that Paulinho is smart.
\end{exercise}
\begin{solution}
  All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
\end{solution}

\section{My second set of exercises}
\begin{exercise}
  Prove that Paulinho is smart.
\end{exercise}
\begin{solution}
  All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
\end{solution}

\begin{exercise}
  Prove that Paulinho is smart.
\end{exercise}
\begin{solution}
  All ducks are smart. Paulinho is a duck. Therefore, Paulinho is smart.
\end{solution}

\end{document}

enter image description here

cgnieder
  • 66,645
  • FYI xsim throws an error in Miktex:

    ("C:\Program Files\MiKTeX 2.9\tex/latex/xsim\xsim.random.code.tex") ! Undefined control sequence. \xsim_declare_translation:nnn ...claretranslation {#1}{\c__xsim_keyword_pref... l.357 Happens even with the latest Miktex updates.

    – rhody Dec 30 '20 at 18:58
  • Problem solved, I had an old translation.sty file that was wasn't updated. – rhody Dec 30 '20 at 19:08