定义 `\newtcolorbox` 的可选参数

定义 `\newtcolorbox` 的可选参数
\documentclass[12pt,a4paper]{ctexart}  
\ctexset{
    section = {
        name = {Week},
        number = {~\arabic{section}}
    }
}
\usepackage{amsmath,enumerate}
\usepackage[top=2cm,left=1cm,right=1.5cm,bottom=2cm]{geometry}


\usepackage{tcolorbox}
\tcbset{%
    colback=white,
    colframe=black,
    title filled=false
}
\newtcolorbox[
    auto counter,
    number freestyle={ W\noexpand\thesection/Q\noexpand\arabic{\tcbcounter} },
    ]{question}[3][]{%
    lowerbox=ignored, %invisible/ignored
    fonttitle=\bfseries,
    title=\thetcbcounter: #2,
    after title={\hfill\colorbox{red}{#3}}, %optional input
    % IfValueTF={#3}{ after title={\hfill\colorbox{red}{#3}} }{},
    #1
}%


%% Document begins
\begin{document}
\pagestyle{empty}


\section{}

%% long way
\begin{question}[after title = {\hfill\colorbox{red}{Easy}}]{How many van loads? }
A transport company's vans each carry a maximum load of 12 tonnes. A firm needs to deliver 24 crates each weighing 5 tonnes. How many van loads will be needed to do this? 
\end{question}


%% Default with two arguments - to be changed
\begin{question}{How many van loads? }
A transport company's vans each carry a maximum load of 12 tonnes. A firm needs to deliver 24 crates each weighing 5 tonnes. How many van loads will be needed to do this? 
\end{question}


%% OK
\begin{question}{How many van loads? }{Easy}
A transport company's vans each carry a maximum load of 12 tonnes. A firm needs to deliver 24 crates each weighing 5 tonnes. How many van loads will be needed to do this? 
\end{question}


%% Don't really want the "blank" red square
\begin{question}{How many van loads? }{}
A transport company's vans each carry a maximum load of 12 tonnes. A firm needs to deliver 24 crates each weighing 5 tonnes. How many van loads will be needed to do this? 
\end{question}




\end{document}

基本上,

\begin{question}[after title = {\hfill\colorbox{red}{Easy}}]{How many van loads? }
\begin{question}{How many van loads? }{Easy}

做我想做的事。

我如何进行更改\newtcolorbox[ ...]{},以便它需要 3 个参数,其中该last参数是可选参数after title={\hfill\colorbox{red}{#3}}

所以如果我这样做\begin{question}{How many van loads? },它就不应该有after title\begin{question}{How many van loads? }{Easy}或者\begin{question}[Easy]{How many van loads? }应该有 after title输入。

软件包手册第 446 页有IfValueTF。但如何使用它并不十分清楚。

答案1

您想使用该xparse库并且\NewTColorBox

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{xparse}

\tcbset{
    colback=white,
    colframe=black,
    title filled=false
}

\NewTColorBox[
    auto counter,
    number freestyle={W\noexpand\thesection/Q\noexpand\arabic{\tcbcounter}},
    ]{question}{O{}mo}{
    lowerbox=ignored, %invisible/ignored
    fonttitle=\bfseries,
    title=\thetcbcounter: #2,
    IfValueTF={#3}{after title={\hfill\colorbox{red}{#3}}}{},
    #1
}

\begin{document}

\begin{question}[after title = {\hfill\colorbox{red}{Easy}}]{How many van loads? }
A transport company's vans each carry a maximum load of 12 tonnes. 
A firm needs to deliver 24 crates each weighing 5 tonnes. How many 
van loads will be needed to do this? 
\end{question}

\begin{question}{How many van loads?}
A transport company's vans each carry a maximum load of 12 tonnes. 
A firm needs to deliver 24 crates each weighing 5 tonnes. How many 
van loads will be needed to do this? 
\end{question}

\begin{question}{How many van loads?}[Easy]
A transport company's vans each carry a maximum load of 12 tonnes. 
A firm needs to deliver 24 crates each weighing 5 tonnes. How many 
van loads will be needed to do this? 
\end{question}

\end{document}

在此处输入图片描述

相关内容