Bedingte \NewTColorBox mit verschiedenen Optionen für tcblower

Bedingte \NewTColorBox mit verschiedenen Optionen für tcblower

Ich habe eine erstellt, NewDocumentEnvironmentum unterschiedlich formatierte Abschnitte für ein Problem und seine Lösung (und mit einem optionalen Kommentar zum Titel) auszudrucken:

\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}

Die Ausgabe wird unten angezeigt.

Bildbeschreibung hier eingeben

Idealerweise würde ich dasselbe Ergebnis gerne mit erreichen \NewTColorBox; ich habe jedoch nicht verstanden, wie ich die unterschiedlichen Formatierungsanforderungen für den oberen und unteren Text tcolorboxund die Konditionale integrieren kann. Ich frage mich, ob jemand mit mehr Erfahrung mit tcolorboxeine Lösung anbieten könnte.

Antwort1

Sie möchten keine Umgebung, sondern einen Befehl. Und Sie müssen den Code nicht duplizieren: Setzen Sie das optionale Argument, falls angegeben, einfach bedingt an die entsprechende Stelle.

\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}

Vielleicht ist es auch mit möglich \NewTColorBox, aber wozu die Mühe?

Bildbeschreibung hier eingeben

verwandte Informationen