2
\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}

Basically,

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

Does what I want.

How do I change \newtcolorbox[ ...]{} so that it takes 3 arguments, with the last argument as an optional one for after title={\hfill\colorbox{red}{#3}}?

So if I do \begin{question}{How many van loads? }, it should not have the after title and \begin{question}{How many van loads? }{Easy} or \begin{question}[Easy]{How many van loads? } should have the after title input.

There is IfValueTF on page 446 in the package manual. But it wasn't quite clear how to use it.

3

You want to use the xparse library and \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}

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.