newtcolorbox 中的条件颜色

newtcolorbox 中的条件颜色

考虑下面显示的文件,它通过 定义了 3 个新的颜色框环境\newtcolorboxbluecolorboxredcolorboxgreencolorbox。除了当前的策略之外,我还想知道如何编写一个单一的策略,\newtcolorbox以便用户可以键入\begin{mybox}[blue]{...}或,\begin{mybox}{blue, ...}然后它会在正确的颜色集上分支。

% Preamble
\documentclass[letterpaper, 10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{MnSymbol}
\usepackage{libertine}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\usepackage[margin = 1.5cm]{geometry}
% List settings
\setlist{topsep = -0.75\parskip, partopsep = 0em, parsep = 0em, itemsep = 0.25\parskip}
\newlist{boxitemize}{itemize}{2}
% Common style
\tcbset{boxstyle/.style = {
    enhanced,
    breakable,
    sharp corners,
    center title,
    boxrule = 1.5pt,
}}
% Color styles
\newtcolorbox{bluecolorbox}[1]{
    boxstyle,
    code = {
        \setlist[boxitemize]{leftmargin = *}
        \setlist[boxitemize, 1]{label = {\textcolor{MaterialIndigo500}{$\filledsquare$}}}
        \setlist[boxitemize, 2]{label = {\textcolor{MaterialIndigo500}{$\bullet$}}}
    },
    colback = MaterialIndigo50,
    colframe = MaterialIndigo900,
    title = {#1}
}
\newtcolorbox{redcolorbox}[1]{
    boxstyle,
    code = {
        \setlist[boxitemize]{leftmargin = *}
        \setlist[boxitemize, 1]{label = {\textcolor{MaterialRed500}{$\filledsquare$}}}
        \setlist[boxitemize, 2]{label = {\textcolor{MaterialRed500}{$\bullet$}}}
    },
    colback = MaterialRed50,
    colframe = MaterialRed900,
    title = {#1}
}
\newtcolorbox{greencolorbox}[1]{
    boxstyle,
    code = {
        \setlist[boxitemize]{leftmargin = *}
        \setlist[boxitemize, 1]{label = {\textcolor{MaterialGreen500}{$\filledsquare$}}}
        \setlist[boxitemize, 2]{label = {\textcolor{MaterialGreen500}{$\bullet$}}}
    },
    colback = MaterialGreen50,
    colframe = MaterialGreen900,
    title = {#1}
}
% Document
\begin{document}
\lipsum[1]
\begin{bluecolorbox}{Blue box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{bluecolorbox}
\begin{redcolorbox}{Red box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{redcolorbox}
\begin{greencolorbox}{Green box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{greencolorbox}
\lipsum[1]
\end{document}

LaTeX 文档

答案1

您可以设置一个tcolorbox以颜色为参数的:

% Preamble
\documentclass[letterpaper, 10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{MnSymbol}
\usepackage{libertine}
\usepackage{xcolor-material}
\usepackage[most]{tcolorbox}
\usepackage[margin = 1.5cm]{geometry}
% command to set the color
\newcommand{\decidecolortest}[1]{%
    % default
    \definecolor{mylightcolor}{named}{MaterialIndigo50}%
    \definecolor{mycolor}{named}{MaterialIndigo500}%
    \definecolor{mydarkcolor}{named}{MaterialIndigo900}%
    \ifstrequal{#1}{blue}{% 
        \definecolor{mylightcolor}{named}{MaterialIndigo50}%
        \definecolor{mycolor}{named}{MaterialIndigo500}%
        \definecolor{mydarkcolor}{named}{MaterialIndigo900}%
        }{}%
    \ifstrequal{#1}{red}{% 
        \definecolor{mylightcolor}{named}{MaterialRed50}%
        \definecolor{mycolor}{named}{MaterialRed500}%
        \definecolor{mydarkcolor}{named}{MaterialRed900}%
        }{}%
    \ifstrequal{#1}{green}{% 
        \definecolor{mylightcolor}{named}{MaterialGreen50}%
        \definecolor{mycolor}{named}{MaterialGreen500}%
        \definecolor{mydarkcolor}{named}{MaterialGreen900}%
        }{}%
    }
% List settings
\setlist{topsep = -0.75\parskip, partopsep = 0em, parsep = 0em, itemsep = 0.25\parskip}
\newlist{boxitemize}{itemize}{2}
% Common style
\tcbset{boxstyle/.style = {
    enhanced,
    breakable,
    sharp corners,
    center title,
    boxrule = 1.5pt,
}}
% Color styles
\newtcolorbox{mybox}[2][]{
    boxstyle,
    code = {\decidecolortest{#1}%
        \setlist[boxitemize]{leftmargin = *}
        \setlist[boxitemize, 1]{label = {\textcolor{mycolor}{$\filledsquare$}}}
        \setlist[boxitemize, 2]{label = {\textcolor{mycolor}{$\bullet$}}}
    },
    colback = mylightcolor,
    colframe = mydarkcolor,
    title = {#2}
}
% Document
\begin{document}
\lipsum[1]
\begin{mybox}[blue]{Blue box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{mybox}
\begin{mybox}[red]{Red box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{mybox}
\begin{mybox}[green]{Green box}
\lipsum[1]
\begin{boxitemize}
    \item Item 1
    \begin{boxitemize}
        \item Item 1.1
        \item Item 1.2
    \end{boxitemize}
    \item Item 2
\end{boxitemize}
\end{mybox}
\lipsum[1]
\end{document}

在此处输入图片描述

相关内容