修改了关于 tcolorbox 中不同选项的问题

修改了关于 tcolorbox 中不同选项的问题

我尝试使用以下代码在使用 \begin{con} 和 Theorem 与 \begin{thm} 时生成一个名为“Construction”的框?

\documentclass{article}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{pgffor}
\usepackage{thmtools}
\usepackage{lipsum}

\colorlet{colthm}{red!75!black}
\colorlet{colcon}{blue!75!black} % Color for construction box

\newcommand{\boxtypes}{
  {thm/colthm},
  {con/colcon}
}

\newcommand{\newtheorembox}[1]{%
  \expandafter\newcounter{#1} % Define a counter for the box type
  \expandafter\colorlet{col#1}{\csname cole#1\endcsname} % Define color for the box type
  \expandafter\newtcolorbox[use counter=#1]{#1}{%
    theorembase,
    drop fuzzy shadow,
    coltitle=black,
    borderline west={3pt}{-3pt}{\csname col#1\endcsname!50},
    attach boxed title to top left={xshift=-3mm, yshift*=-    \tcboxedtitleheight/2},
    boxed title style={right=3pt, bottom=3pt, overlay={
      \draw[draw=\csname col#1\endcsname!70, fill=\csname     col#1\endcsname!70, line join=round]
        (frame.south west) -- (frame.north west) --     (frame.north east) --
        (frame.south east) -- ++(-2pt, 0) -- ++(-2pt, -4pt) --
        ++(-2pt, 4pt) -- cycle;
    }},
    overlay unbroken={
      \scoped \shade[left color=\csname     col#1\endcsname!10!black, right color=\csname col#1\endcsname]
      ([yshift=-0.2pt]title.south west) -- ([xshift=-1.5pt,     yshift=-0.2pt]title.south-|frame.west) -- ++(0, -6pt) -- cycle;
    },
  }
}

\foreach \abbr/\color in \boxtypes {
  \expandafter\newtheorembox\expandafter{\abbr}
}

\begin{document}

    \begin{thm}

        \lipsum[1]
    
    \end{thm}

    \begin{con}

        \lipsum[1]
    
    \end{con}

 \end{document}

因此,以后我可以对任何类型的盒子使用相同的代码。但我最终得到的是

!软件包 xcolor 错误:未定义颜色“\colethm”。我能想到另一种定义颜色的方法。

答案1

类似这样的

在此处输入图片描述

在此处输入图片描述

你只需要修改title语法(请尽情尝试)

        title={\thechapter.\thesection.#2 \thetcbcounter},

平均能量损失

\documentclass{book}

\usepackage[most]{tcolorbox}
\usepackage{xcolor}

\newcounter{thm}[section] % Define a theorem counter tied to sections
\newcounter{con}[section] % Define a construction counter tied to sections
\newcounter{defin}[section] % Define a definition counter tied to sections

\renewcommand{\thethm}{\thesection.\arabic{thm}} % Include chapter and section numbers in theorem counter
\renewcommand{\thecon}{\thesection.\arabic{con}} % Include chapter and section numbers in construction counter
\renewcommand{\thedefin}{\thesection.\arabic{defin}} % Include chapter and section numbers in definition counter

\colorlet{colthm}{red!75!black}
\colorlet{colcon}{red!5!white}
\colorlet{coldefin}{green!50!black!50}

\newcommand{\newmybox}[4]{%
    \tcbset{
        #1base/.style={ 
            empty,
            frame engine=path,
            colframe=yellow!10,
            sharp corners,
            title={\thechapter.\thesection.#2 \thetcbcounter},
            attach boxed title to top left={yshift*=-\tcboxedtitleheight},
            boxed title style={size=minimal, top=4pt, left=4pt,      colback=#4}, % Use the provided color variable for title background
            coltitle=#4,fonttitle=\large\bfseries\sffamily,
        },
    }
    \newtcolorbox[use counter=#1]{#1}{%
        #1base, % Use renamed base style for theorem environment
        drop fuzzy shadow,
        coltitle=black,
        borderline west={3pt}{-3pt}{#4}, % Use the provided color      variable for the borderline
        attach boxed title to top left={xshift=-3mm, yshift*=-     \tcboxedtitleheight/2},
        boxed title style={right=3pt, bottom=3pt, overlay={
                \draw[draw=#4, fill=#4, line join=round] % Use the provided      color variable for the fill and draw
                (frame.south west) -- (frame.north west) -- (frame.north      east) --
                (frame.south east) -- ++(-2pt, 0) -- ++(-2pt, -4pt) --
                ++(-2pt, 4pt) -- cycle;
        }},
    }
}

% Define the new command for creating boxes
\newcommand{\newmythm}[3]{%
    \newmybox{#1}{#2}{#3}{col#1}%
}

\newmythm{thm}{Theorem}{thm}
\newmythm{con}{Construction}{con}
\newmythm{defin}{Definition}{defin}


\begin{document}
    
    \chapter{first chapter}
    
    \section{First Section}
    
    \begin{thm}
        This is a Theorem
    \end{thm}
    
    \begin{thm}
        This is another Theorem
    \end{thm}
    
    \begin{con}
        This is a construction
    \end{con}
    
        \chapter{Second chapter}
    
    \section{Second Section}
    
    \begin{thm}
        This is a Theorem
    \end{thm}
    
    \begin{thm}
        This is another Theorem
    \end{thm}
    
    \begin{con}
        This is a construction
    \end{con}
\end{document}

答案2

我认为我有,

 \documentclass{book}

 \usepackage[most]{tcolorbox}
 \usepackage{xcolor}

 \newcounter{thm}[section] % Define a theorem counter tied to sections
 \newcounter{con}[section] % Define a construction counter tied to sections
 \newcounter{defin}[section] % Define a definition counter tied to sections

 \renewcommand{\thethm}{\thesection.\arabic{thm}} % Include chapter and section numbers in theorem counter
 \renewcommand{\thecon}{\thesection.\arabic{con}} % Include chapter and section numbers in construction counter
 \renewcommand{\thedefin}{\thesection.\arabic{defin}} % Include chapter and section numbers in definition counter

 \colorlet{colthm}{red!75!black}
 \colorlet{colcon}{red!5!white}
 \colorlet{coldefin}{green!50!black!50}

 \newcommand{\newmybox}[4]{%
   \tcbset{
     #1base/.style={ 
       empty,
       frame engine=path,
       colframe=yellow!10,
       sharp corners,
       title={#2 \thetcbcounter},
       attach boxed title to top left={yshift*=-\tcboxedtitleheight},
       boxed title style={size=minimal, top=4pt, left=4pt,      colback=#4}, % Use the provided color variable for title background
       coltitle=#4,fonttitle=\large\bfseries\sffamily,
     },
   }
   \newtcolorbox[use counter=#1]{#1}{%
     #1base, % Use renamed base style for theorem environment
     drop fuzzy shadow,
     coltitle=black,
     borderline west={3pt}{-3pt}{#4}, % Use the provided color      variable for the borderline
      attach boxed title to top left={xshift=-3mm, yshift*=-     \tcboxedtitleheight/2},
      boxed title style={right=3pt, bottom=3pt, overlay={
       \draw[draw=#4, fill=#4, line join=round] % Use the provided      color variable for the fill and draw
         (frame.south west) -- (frame.north west) -- (frame.north      east) --
         (frame.south east) -- ++(-2pt, 0) -- ++(-2pt, -4pt) --
         ++(-2pt, 4pt) -- cycle;
     }},
   }
 }

 % Define the new command for creating boxes
 \newcommand{\newmythm}[3]{%
 \newmybox{#1}{#2}{#3}{col#1}%
 }

 \newmythm{thm}{Theorem}{thm}
 \newmythm{con}{Construction}{con}
 \newmythm{defin}{Definition}{defin}


 \begin{document}

 \chapter{first chapter}

 \section{First Section}

 \begin{thm}
   This is a Theorem
 \end{thm}

 \begin{thm}
   This is another Theorem
 \end{thm}

 \begin{con}
   This is a construction
 \end{con}

 \end{document}

但我现在只需要在标题中添加章节编号。

相关内容