0

I have created a NewDocumentEnvironment to print differently formatted sections for a problem and its solution (and with an optional comment on the title):

\documentclass[10pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{geometry}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 }
\usepackage{parskip}
\usepackage{tcolorbox}
\usepackage{lipsum}
\tcbuselibrary{skins,xparse,breakable}

\newcounter{problem}

\NewDocumentEnvironment{newprob}{o m +m}
{ % beginning code
    \refstepcounter{problem}
    \IfNoValueTF{#1}{ % start of true statement
    \begin{tcolorbox}[skin=bicolor,
    breakable,
    title=\textbf{Problem~\theproblem:},
    colframe=black!50!white,
    colback=black!20!white,
    colbacklower=black!5!white]
    {#2}    
    \tcblower\vskip-\baselineskip
    \tcbsubtitle[before skip=0pt]%
    {\textbf{Solution:}}
    {#3}
    \end{tcolorbox}
    } % end of true statement
    { % start of false statement
    \begin{tcolorbox}[skin=bicolor,
    breakable,
    title=\textbf{Problem~\theproblem:}~(#1),
    colframe=black!40!white,    
    colback=black!20!white,
    colbacklower=black!5!white]
    {#2}    
    \tcblower\vskip-\baselineskip
    \tcbsubtitle[before skip=0pt]%
    {\textbf{Solution:}}
    {#3}
    \end{tcolorbox} 
    } % end of false statement
}  %end code

\begin{document}
\begin{newprob}
    {
        This is one problem...
    }
    {
        ... and this is one answer.
    }
\end{newprob}

\begin{newprob}[special]
    {
        This is a speacial problem...
    }
    {
        ... and this is a special answer.
    }
\end{newprob}

\end{document}

The output is shown below.

enter image description here

Ideally I would like to achieve the same result using \NewTColorBox ; however I have not been able to understand how to incorporate the different formatting requirements for the upper and lower body of the tcolorbox and the conditionals. I wonder if someone with more experience with tcolorbox could provide a solution.

1 Answer 1

1

You don't want an environment, but a command. And you don't need to duplicate code: just conditionally typeset the optional argument if given, in the appropriate spot.

\documentclass[10pt]{article}
\usepackage{amsmath,amssymb}
\usepackage{geometry}
 \geometry{
 a4paper,
 total={170mm,257mm},
 left=20mm,
 top=20mm,
 }

\usepackage{tcolorbox}
\tcbuselibrary{skins,xparse,breakable}

\newcounter{problem}

\NewDocumentCommand{\newprob}{o +m +m}{%
  \refstepcounter{problem}
  \begin{tcolorbox}[
    skin=bicolor,
    breakable,
    title=\textbf{Problem~\theproblem:}\IfValueT{#1}{~(#1)},
    colframe=black!50!white,
    colback=black!20!white,
    colbacklower=black!5!white
  ]{#2}%
    \tcblower\vskip-\baselineskip
    \tcbsubtitle[before skip=0pt]%
    {\textbf{Solution:}}{#3}
  \end{tcolorbox}
}

\begin{document}

\newprob
    {
        This is one problem...
    }
    {
        ... and this is one answer.
    }

\newprob[special]
    {
        This is a speacial problem...
    }
    {
        ... and this is a special answer.
    }

\end{document}

Maybe it's possible also with \NewTColorBox, but why bother?

enter image description here

2
  • Thanks. Why is a command better than an environment?
    – Ted Black
    Commented Dec 14, 2023 at 22:02
  • @TedBlack Because you have just arguments and nothing else.
    – egreg
    Commented Dec 14, 2023 at 23:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .