如何使用 tcolorbox 修改现有环境并保留名称?

如何使用 tcolorbox 修改现有环境并保留名称?

我想重新定义任务环境并将其与声明的彩色盒子。 到底任务 1 字幕应该有相同的文本颜色/背景颜色练习0.1

我也想\begin{aufgabe}...\end{aufgabe}在重新定义的环境中使用相同的命令。

\documentclass{article}

\usepackage[skins,hooks,xparse,breakable]{tcolorbox}
\usepackage{xcolor}
\usepackage{chngcntr}
\usepackage{blindtext}
\usepackage{tikz}

\newcounter{examplecounter}
\counterwithin{examplecounter}{section}

\usetikzlibrary{calc}


\definecolor{headercolourgreen}{rgb}{0,0.7019,0.7019}

\usepackage[
typ=ab,
fach=,
lerngruppe=,
farbig
% nummer=2A,
% datumAnzeigen,
% namensfeldAnzeigen,
]{schule}


\DeclareTColorBox{new}{O{}}{
    enhanced,
    breakable,
    arc=0pt,
    boxrule=0pt,
    colback=white,
    before skip={2em}, % increase spacing before the env
    overlay unbroken and first={
        % draw box on the left
        \node[fill=headercolourgreen!5,
        font=\color{headercolourgreen}\sffamily\bfseries\large,
        anchor=south west,
        xshift=2mm
        ] (titlebox) at (frame.north west) 
        {Exercise \refstepcounter{examplecounter}\theexamplecounter};
        
        
        \draw[draw=headercolourgreen, line width=2pt] 
        (titlebox.north west-|frame.north west)--+(0,-1.5);

    },
%   
}
\author{}
\title{}
\date{\today}



\begin{document}
            \begin{aufgabe}[subtitle=subtitle]
        \blindtext
    \end{aufgabe}
    \null
    \begin{aufgabe}[subtitle=subtitle]
        \blindtext
    \end{aufgabe}
    

    
    \null
    \begin{new}
        \blindtext
    \end{new}
    
    
    
    \null
    \begin{new}
        \blindtext
    \end{new}
    


\end{document}

答案1

如果我理解了你的评论,这里有一种方法可以实现所需的结果:首先,设置pgfkeys提取选项的值subtitle=

\pgfkeys{MyAufgabe/.cd,
    subtitle/.store in=\MyAufgabeSubtitle,
}

subtitle=这将存储的值\MyAufgabeSubtitle

接下来重新定义aufgabe环境以提取字幕并传递\MyAufgabeSubtitletcolobbox。我将其定义为aufgabeAux环境,以便更容易看出这与new环境之间的区别:

\renewenvironment{aufgabe}[1][]{%
    \pgfkeys{MyAufgabe/.cd, subtitle={}, #1}%
    \begin{aufgabeAux}[\MyAufgabeSubtitle]%
}{%
    \end{aufgabeAux}%
}%

参考:

结果:

在此处输入图片描述

代码:

\documentclass{article}

\usepackage[skins,hooks,xparse,breakable]{tcolorbox}
\usepackage{xcolor}
\usepackage{chngcntr}
\usepackage{blindtext}
\usepackage{tikz}

\newcounter{examplecounter}
\counterwithin{examplecounter}{section}

\usetikzlibrary{calc}
\definecolor{headercolourgreen}{rgb}{0,0.7019,0.7019}

\usepackage[
typ=ab,
fach=,
lerngruppe=,
farbig
% nummer=2A,
% datumAnzeigen,
% namensfeldAnzeigen,
]{schule}

\tcbset{MyStyle/.style={
    enhanced,
    breakable,
    arc=0pt,
    boxrule=0pt,
    colback=white,
    before skip={2em}, % increase spacing before the env
}}

\DeclareTColorBox{new}{O{}}{
    MyStyle,
    overlay unbroken and first={
        % draw box on the left
        \node[fill=headercolourgreen!5,
        font=\color{headercolourgreen}\sffamily\bfseries\large,
        anchor=south west,
        xshift=2mm
        ] (titlebox) at (frame.north west) 
        {Exercise \refstepcounter{examplecounter}\theexamplecounter};
        \draw[draw=headercolourgreen, line width=2pt] 
        (titlebox.north west-|frame.north west)--+(0,-1.5);
    },
}


\DeclareTColorBox{aufgabeAux}{O{}}{
    MyStyle,
    overlay unbroken and first={
        % draw box on the left
        \node[fill=headercolourgreen!5,
        font=\color{headercolourgreen}\sffamily\bfseries\large,
        anchor=south west,
        xshift=2mm
        ] (titlebox) at (frame.north west) 
        {Aufgabe \refstepcounter{aufgabe}\theaufgabe\ #1};
        \draw[draw=headercolourgreen, line width=2pt] 
        (titlebox.north west-|frame.north west)--+(0,-1.5);
    },
}

%% Save the original version in case it is still needed
\let\oldaufgabe\aufgabe
\let\endoldaufgabe\endaufgabe

\pgfkeys{MyAufgabe/.cd,
    subtitle/.store in=\MyAufgabeSubtitle,
}
\renewenvironment{aufgabe}[1][]{%
    \pgfkeys{MyAufgabe/.cd, subtitle={}, #1}%
    \begin{aufgabeAux}[\MyAufgabeSubtitle]%
}{%
    \end{aufgabeAux}%
}%

\author{}
\title{}
\date{\today}
\begin{document}
    \begin{oldaufgabe}[subtitle=Some Subtitle]
        This is an example of using the original \verb|aufgabe| environment.
        This should appear as the original did.
    \end{oldaufgabe}
    \bigskip\hrule\bigskip
    
    \begin{aufgabe}[subtitle=This is a subtitle]
        This is an example of using the new \verb|aufgabe| environment with a subtitle.
        This should have the title appear as the \verb|new| environment.
    \end{aufgabe}
    
    \begin{aufgabe}
        Another example of using the new \verb|aufgabe| environment, 
        but \textbf{without a subtitle}.
    \end{aufgabe}
    
    \begin{new}[subtitle This is a subtitle]
        This the sample format that is the desired format for the new 
        \verb|aufgabe| environment .
    \end{new}
\end{document}

相关内容