我有这个代码,它允许我根据部分定义定理样式框的颜色和样式,并且我也有子部分代码,但我希望当部分改变颜色时,子部分会继续采用该配色方案和我所布置的样式,这可能吗?
\documentclass{book}
\usepackage{fancyhdr} % Customize headers and footers
\usepackage{setspace} % Adjust line spacing
\usepackage[many]{tcolorbox} % Boxes for headings or text
\usepackage[explicit]{titlesec} % Customize chapter and section titles
\usepackage{etoolbox}
\usepackage{libertine} % Use the Linux Libertine font (repeated)
\usepackage[style=authoryear, backend=biber]{biblatex}
%%%%%%%%%%%%%%%%%%%%%%%%%
% Counter definitions tied to section or chapter
\newcounter{thm}[section]
\newcounter{con}[section]
% Initialize current color with a default
\newcommand{\currentcolor}{colthm}
% Command to change the current color based on section
\newcommand{\changecolor}[1]{%
\renewcommand{\currentcolor}{#1}%
}
% Assuming you have a mechanism to distinguish section types,
% you could redefine the \section command or create a new one to include the color change.
% For simplicity, let's define custom commands for section types:
\newcommand{\thmsection}[1]{%
\changecolor{colthm}%
\section{#1}%
}
\newcommand{\consection}[1]{%
\changecolor{colcon}%
\section{#1}%
}
% Repeat for other types as needed
% Apply the current color to subsection titles
\titleformat{\subsection}[hang]
{\normalfont\large\bfseries\color{\currentcolor}} % Format
{\thesubsection}{0.5em}{} % Label and separation
% Custom formatting for counters
\renewcommand{\thethm}{\thechapter.\thesection.\arabic{thm}}
\renewcommand{\thecon}{\thechapter.\thesection.\arabic{con}}
% Color definitions for different box types
\colorlet{colthm}{red!90!black}
\colorlet{colcon}{blue!70!white}
\colorlet{coldefin}{green!50!black!50}
\colorlet{colpost}{green!80!white!50}
\colorlet{colcn}{orange!80!black!10}
% The main command to create a styled tcolorbox
\newcommand{\newmybox}[4]{%
\tcbset{
#1base/.style={
enhanced, % Use enhanced features for the box
breakable, % Allow box to break across pages
empty, % No background and frame by default
frame engine=path, % Use path-based rendering for the frame
colframe=yellow!10, % Frame color (not visible with empty style)
sharp corners, % Sharp corners, can be customized
title={#2 \thechapter.\thesection.\arabic{#3}}, % Box title format
attach boxed title to top left={yshift*=-\tcboxedtitleheight},
boxed title style={size=minimal, top=4pt, left=4pt, colback=#4},
coltitle=black, % Title text color
fonttitle=\large\bfseries\sffamily, % Title font style
drop fuzzy shadow=black!50!white, % Uncomment for shadow effect
borderline west={3pt}{-3pt}{#4}, % Border line on the left
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]
(frame.south west) -- (frame.north west) -- (frame.north east) --
(frame.south east) -- ++(-2pt, 0) -- ++(-2pt, -4pt) --
++(-2pt, 4pt) -- cycle;
}},
},
}
\newtcolorbox[use counter=#1]{#1}{%
#1base, % Apply the base style
% Additional styles or parameters can be added here
}
}
% Revised command to ensure proper text wrapping
\newtcolorbox{boxsubsect}[2][]{
enhanced,
before skip=2mm, after skip=2mm,
colframe=blue!50!black, colback=yellow!20!white,
colbacktitle=red!5!white,
breakable, % Ensures content can break across pages/lines
fonttitle=\bfseries\small,
coltitle=black,
width=\linewidth, % Explicitly set the width to the line width
left=1mm, right=1mm, % Adjust padding as necessary
attach boxed title to top left={
yshift=-0.25mm-\tcboxedtitleheight/2,
yshifttext=2mm-\tcboxedtitleheight/3
},
boxed title style={
boxrule=0.4mm,
frame code={
\path[tcb fill frame] ([xshift=-0mm]frame.west)--(frame.north west) -- (frame.north east) -- ([xshift=3mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle;
},
interior code={
\path[tcb fill interior] ([xshift=-0mm]interior.west)--(interior.north west) -- (interior.north east)
-- ([xshift=1.5mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;
},
},
title={\thesubsection #2}, #1,
valign=center, % This ensures vertical alignment; adjust as needed
}
\renewcommand{\thesubsection}{\Roman{subsection}.}
% Adjusted title format for subsection to ensure the box is used correctly
\titleformat{\subsection}
{} % Preceding the title body
{}{0em} % Space between label and title body, if any
{\begin{boxsubsect}{\hskip0.7em#1}\end{boxsubsect}} % Before the title
[] % After the title
% Command to easily create new theorem-like environments
\newcommand{\newmythm}[3]{%
\newmybox{#1}{#2}{#3}{col#1}%
}
% Creating specific environments
\newmythm{thm}{Theorem}{thm}
\newmythm{con}{Construction}{con}
%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{thm}
Theorem
\end{thm}
\subsection{Wrong colour}
\begin{con}
Construction
\end{con}
\subsection{wrong colour}
\end{document}
答案1
\documentclass{book}
% [Your existing preamble with packages]
\usepackage{fancyhdr} % Customize headers and footers
\usepackage{setspace} % Adjust line spacing
\usepackage[many]{tcolorbox} % Boxes for headings or text
\usepackage[explicit]{titlesec} % Customize chapter and section titles
\usepackage{etoolbox}
\usepackage{libertine} % Use the Linux Libertine font (repeated)
\usepackage[style=authoryear, backend=biber]{biblatex}
%%%%%%%%%%%%%%%%%%%%%%%%%
\newcounter{thm}[section]
\newcounter{con}[section]
% Initialize current color with a default
\newcommand{\currentcolor}{colthm}
% Command to change the current color based on section
\newcommand{\changecolor}[1]{%
\renewcommand{\currentcolor}{#1}%
}
\newcommand{\thmsection}[1]{%
\changecolor{colthm}%
\section{#1}%
}
\newcommand{\consection}[1]{%
\changecolor{colcon}%
\section{#1}%
}
% Apply the current color to subsection titles
\titleformat{\subsection}[hang]
{\normalfont\large\bfseries\color{\currentcolor}} % Format
{\thesubsection}{0.5em}{} % Label and separation
% Custom formatting for counters
\renewcommand{\thethm}{\thechapter.\thesection.\arabic{thm}}
\renewcommand{\thecon}{\thechapter.\thesection.\arabic{con}}
% Color definitions for different box types
\colorlet{colthm}{red!90!black}
\colorlet{colcon}{blue!70!white}
\colorlet{coldefin}{green!50!black!50}
\colorlet{colpost}{green!80!white!50}
\colorlet{colcn}{orange!80!black!10}
% The main command to create a styled tcolorbox
\newcommand{\newmybox}[4]{%
\tcbset{
#1base/.style={
enhanced, % Use enhanced features for the box
breakable, % Allow box to break across pages
empty, % No background and frame by default
frame engine=path, % Use path-based rendering for the frame
colframe=yellow!10, % Frame color (not visible with empty style)
sharp corners, % Sharp corners, can be customized
title={#2 \thechapter.\thesection.\arabic{#3}}, % Box title format
attach boxed title to top left={yshift*=-\tcboxedtitleheight},
boxed title style={size=minimal, top=4pt, left=4pt, colback=#4},
coltitle=black, % Title text color
fonttitle=\large\bfseries\sffamily, % Title font style
drop fuzzy shadow=black!50!white, % Uncomment for shadow effect
borderline west={3pt}{-3pt}{#4}, % Border line on the left
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]
(frame.south west) -- (frame.north west) -- (frame.north east) --
(frame.south east) -- ++(-2pt, 0) -- ++(-2pt, -4pt) --
++(-2pt, 4pt) -- cycle;
}},
},
}
\newtcolorbox[use counter=#1]{#1}{%
#1base, % Apply the base style
% Additional styles or parameters can be added here
}
}
\newtcolorbox{boxsubsect}[2][]{
enhanced,
before skip=2mm, after skip=2mm,
colframe=blue!50!black, colback=yellow!20!white,
colbacktitle=red!5!white,
breakable, % Ensures content can break across pages/lines
fonttitle=\bfseries\small,
coltitle=black,
width=\linewidth, % Explicitly set the width to the line width
left=1mm, right=1mm, % Adjust padding as necessary
attach boxed title to top left={
yshift=-0.25mm-\tcboxedtitleheight/2,
yshifttext=2mm-\tcboxedtitleheight/3
},
boxed title style={
boxrule=0.4mm,
frame code={
\path[tcb fill frame] ([xshift=-0mm]frame.west)--(frame.north west) -- (frame.north east) -- ([xshift=3mm]frame.east)
-- (frame.south east) -- (frame.south west) -- cycle;
},
interior code={
\path[tcb fill interior] ([xshift=-0mm]interior.west)--(interior.north west) -- (interior.north east)
-- ([xshift=1.5mm]interior.east) -- (interior.south east) -- (interior.south west)
-- cycle;
},
},
title={\thesubsection #2}, #1,
valign=center, % This ensures vertical alignment; adjust as needed
}
\renewcommand{\thesubsection}{\Roman{subsection}.}
\titleformat{\subsection}
{} % Preceding the title body
{}{0em} % Space between label and title body, if any
{\begin{boxsubsect}{\hskip0.7em#1}\end{boxsubsect}} % Before the title
[] % After the title
% theorem-like environments
\newcommand{\newmythm}[3]{%
\newmybox{#1}{#2}{#3}{col#1}%
}
% Creating specific environments
\newmythm{thm}{Theorem}{thm}
\newmythm{con}{Construction}{con}
%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{chngcntr}
\newcounter{subthm}[thm]
\counterwithin{subthm}{thm} % Makes subthm subordinate to thm
color scheme
\newtcolorbox{subthmbox}[1][]{
enhanced,
breakable,
colback=\currentcolor!5, % Lighter background color; adjust as necessary
colframe=\currentcolor, % Frame color uses the current color
coltitle=\currentcolor, % Title color
title=Subtheorem~\thethm.\arabic{subthm}:,
#1
}
\newenvironment{subthm}[1][]{%
\refstepcounter{subthm}%
\begin{subthmbox}[#1]%
}{%
\end{subthmbox}%
}
% [Your document body]
\begin{document}
\thmsection{Sample Section with Theorems}
\begin{thm}
This is a theorem.
\end{thm}
\begin{subthm}
This is a subtheorem, styled differently but using the theorem color.
\end{subthm}
\begin{con}
This is a construction.
\end{con}
\consection{Sample Section with Constructions}
\begin{thm}
Another theorem, but in a construction section.
\end{thm}
\begin{subthm}
Another subtheorem
\end{subthm}
\end{document}