我是 LaTeX 新手。我想自定义 tcolorbox 以自定义标题,并根据我的选择将背景颜色设为浅红色。我还想更改标题的字体颜色以及内部文本内容,根据我的选择将其设为深红色/蓝色。我该怎么做?请帮忙。
基本 MWE 由 feculededentier 在以下链接页面中提供
我找到了这个页面tcolorbox 标题中与章节相关的示例数字
答案1
最开始的近似值我只会取回答你提到的,去除chapter
东西并添加颜色。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter,
number freestyle={\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
enhanced,
breakable,
fonttitle=\bfseries,
mytitle={},
#1
}
\begin{document}
\begin{myexample}[colback=red!5!white,colframe=red!75!black,mytitle={First example}, label=exfirst]
\lipsum[4]
\end{myexample}
\begin{myexample}[colback=red!5!white,colframe=blue!75!black]
\lipsum[4]
\end{myexample}
\begin{myexample}[colback=red!5!white,colframe=red!75!black,mytitle={Third example}, label=exthird]
\lipsum[4]
\end{myexample}
I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}
编辑:解决颜色问题的一种方法是引入新的宏。
\documentclass{article}
\usepackage{etoolbox}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\tcbset{mytitle/.style={title={Example~\thetcbcounter\ifstrempty{#1}{}{: #1}}}}
\newtcolorbox[auto counter, number within=chapter,
number freestyle={\noexpand\arabic{\tcbcounter}}]{myexample}[1][]{%
enhanced,
breakable,
fonttitle=\bfseries,
mytitle={},
#1
}
\newcommand{\BlueBox}[3][]{
\begin{myexample}[colback=blue!5!white,colframe=blue!75!black,mytitle={#2},#1]
#3
\end{myexample}
}
\newcommand{\RedBox}[3][]{
\begin{myexample}[colback=blue!5!white,colframe=red!75!black,mytitle={#2},#1]
#3
\end{myexample}
}
\begin{document}
\RedBox[label=exfirst]{First Example}{\lipsum[1]}
\BlueBox[label=exsecond]{}{\lipsum[2]}
\RedBox[label=exthird]{Third Example}{\lipsum[3]}
I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}
答案2
欢迎:)
\documentclass{book}
\usepackage{tcolorbox}
\tcbuselibrary{skins,breakable}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexample}[2][]{%
enhanced,
breakable,
fonttitle=\bfseries,
title=\textcolor{red}{Example}~\textcolor{red}{\thetcbcounter:} \textcolor{red}{#2},
#1
}
\newtcolorbox[auto counter, number within=chapter, number freestyle={\noexpand\thechapter.\noexpand\arabic{\tcbcounter}}]{myexampleblue}[2][]{%
enhanced,
breakable,
fonttitle=\bfseries,
title=\textcolor{blue}{Example}~\textcolor{blue}{\thetcbcounter:} \textcolor{blue}{#2},
#1
}
\begin{document}
\chapter{First chapter}
\begin{myexample}[label=exfirst]{First example}
\lipsum[4]
\end{myexample}
\begin{myexampleblue}{Second example}
\lipsum[4]
\end{myexampleblue}
I can now refer to example~\ref{exfirst} and example~\ref{exthird}.
\end{document}