我有一个命令\kw
,它只是用某种颜色将文本格式化为粗体。它用于关键字,既可用于普通文本(默认为某种预定义颜色),也可用于框(我希望它采用框的颜色(或者更确切地说是其标题的颜色)。
\documentclass[a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\definecolor{green}{rgb}{0.08,0.61,0.32}
\definecolor{bggreen}{rgb}{0.953,0.98,0.949}
\definecolor{red}{rgb}{1,0.24,0.24}
\definecolor{bgred}{rgb}{0.988,0.937,0.937}
% Etc.
% Set the colour of the default for various boxes
\newcommand*{\setaccentcolour}[1]{
\colorlet{accentcolour}{#1}
\tcbset{
accent/.style={
colback=bg#1,
colbacktitle=bg#1,
coltitle=#1,
}
}
}
\setaccentcolour{green}
% Sets of colours for manually changing
\tcbset{
red/.style={
colback=bgred,
colbacktitle=bgred,
coltitle=red,
},
% Etc.
}
\newcommand*{\kw}[1]{\textbf{\textcolor{accentcolour}{#1}}}
\NewTcbTheorem{definition}{Definition}{accent}{def}
\NewTColorBox{callout}{ O{accent} }{#1}
\begin{document}
\begin{callout}[red]
% I'd like this to use the title colour (even though there's no title here) which is `red'
This is \kw{coloured} text!
\end{callout}
% This is green, as expected
And some more \kw{coloured} text.
\begin{definition}{Test}{}
% This is also green
This is also \kw{coloured}.
\end{definition}
\end{document}
我不会考虑在每个框中使用不同的命令,或者明确地改变颜色,因为这会破坏命令的语义。
我尝试使用tcbcoltitle
tcolorbox 定义的颜色,效果很好,但我仍然需要一种方法来判断命令是否在颜色框内调用。即
\begin{definition}{Test}{}
This is \textcolor{tcbcoltitle}{coloured} text!
\end{definition}
提供与框标题颜色匹配的文本,但要将其放入命令中需要知道何时应用tcbcoltitle
(在框中时)或何时默认为某些基本颜色kwcolour
(不在框中时)。
答案1
您可以定义\kw
选择文本的自定义颜色,然后可以tcolorbox
使用以下code
选项在相关内容中(重新)定义该颜色:
\documentclass{article}
\usepackage{tcolorbox}
\newtcolorbox{myredbox}{
colback=red!5!white,
colframe=red!75!black
}
\newtcolorbox{mybluebox}{
colback=blue!5!white,
colframe=blue!75!black,
code={\colorlet{kwcolor}{red}}
}
\colorlet{kwcolor}{blue}
\NewDocumentCommand{\kw}{ m }{%
\textcolor{kwcolor}{#1}%
}
\begin{document}
\begin{myredbox}
Foo \kw{bar} baz
\end{myredbox}
\begin{mybluebox}
Foo \kw{bar} baz
\end{mybluebox}
\begin{myredbox}
Foo \kw{bar} baz
\end{myredbox}
\end{document}
与 es 相同的方法(和相同的输出)\NewTotalTColorBox
:
\documentclass{article}
\usepackage{tcolorbox}
\NewTotalTColorBox{\myredbox}{ m }{
colback=red!5!white,
colframe=red!75!black
}{ #1 }
\NewTotalTColorBox{\mybluebox}{ m }{
colback=blue!5!white,
colframe=blue!75!black,
code={\colorlet{kwcolor}{red}}
}{ #1 }
\colorlet{kwcolor}{blue}
\NewDocumentCommand{\kw}{ m }{%
\textcolor{kwcolor}{#1}%
}
\begin{document}
\myredbox{Foo \kw{bar} baz}
\mybluebox{Foo \kw{bar} baz}
\myredbox{Foo \kw{bar} baz}
\end{document}
我将您在最近编辑的问题中添加的片段拼接在一起,希望这些片段是正确的。据我所知,您仍然可以使用上面描述的方法。
通过颜色重新定义的code
作用域是当前框。因此,如果颜色red
在框外,而您将其重新定义为框内blue
(或),则如果稍后在框外使用,颜色的定义仍将为:tcbcoltitle
red
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\usepackage{cleveref}
\tcbset{
accent/.style={
colback=blue!25,
colbacktitle=blue!25,
coltitle=blue,
code={\colorlet{kwcolour}{tcbcoltitle}}
},
}
\NewTcbTheorem[
Crefname={Definition}{Definitions}
]{definition}{Definition}{accent}{def}
\colorlet{kwcolour}{red}
\newcommand*{\kw}[1]{\textbf{\textcolor{kwcolour}{#1}}}
\begin{document}
Foo \kw{bar} baz
\begin{definition}{Foo}{foo}
Foo \kw{bar} baz
\end{definition}
Foo \kw{bar} baz
\end{document}
此外,如果您使用上面描述的选项,此解决方案也会按预期工作,并且使用 MWE code
。您只需code={\colorlet{accentcolour}{tcbcoltitle}
在设置coltitle=red
课程后进行设置:
\documentclass[a4paper]{article}
\usepackage{tcolorbox}
\tcbuselibrary{theorems}
\definecolor{green}{rgb}{0.08,0.61,0.32}
\definecolor{bggreen}{rgb}{0.953,0.98,0.949}
\definecolor{red}{rgb}{1,0.24,0.24}
\definecolor{bgred}{rgb}{0.988,0.937,0.937}
% Etc.
% Set the colour of the default for various boxes
\newcommand*{\setaccentcolour}[1]{
\colorlet{accentcolour}{#1}
\tcbset{
accent/.style={
colback=bg#1,
colbacktitle=bg#1,
coltitle=#1,
}
}
}
\setaccentcolour{green}
% Sets of colours for manually changing
\tcbset{
red/.style={
colback=bgred,
colbacktitle=bgred,
coltitle=red,
code={\colorlet{accentcolour}{tcbcoltitle}}
},
% Etc.
}
\newcommand*{\kw}[1]{\textbf{\textcolor{accentcolour}{#1}}}
\NewTcbTheorem{definition}{Definition}{accent}{def}
\NewTColorBox{callout}{ O{accent} }{#1}
\begin{document}
Some \kw{coloured} text.
\begin{callout}[red]
% I'd like this to use the title colour (even though there's no title here) which is `red'
This is \kw{coloured} text!
\end{callout}
% This is green, as expected
And some more \kw{coloured} text.
\begin{definition}{Test}{}
% This is also green
This is also \kw{coloured}.
\end{definition}
\end{document}