0

I tried to use the following code to produce a box titled Construction when using \begin{con} and Theorem with \begin{thm}?

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

and therefore use the same code for any type of box I want in the future. But I end up with

! Package xcolor Error: Undefined color `\colethm '. I can think of another way to define the colors.

2 Answers 2

1

something like this

enter image description here

enter image description here

you only need to amend the title syntax (please experiment to your hearts content)

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

MWE

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

I think I have it with,

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

but I just need to get it to add the chapter and section number in the titles now.

You must log in to answer this question.

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