使用 tcolorbox 的练习框

使用 tcolorbox 的练习框

我想知道如何将文本“MyTitle”放入环境参数中并将其放在右侧?

\documentclass[12pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcounter{mycounter}

\setlength{\parindent}{0pt}

\begin{document}

\tcbset{exercisebox/.style={enhanced,colback=white,colframe=green!65!black,
enlarge top by=10mm,
overlay={%
\path[fill=blue!65,line width=.4mm] (frame.north west)--++(17mm,0)coordinate(n2)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
\node at ([shift={(5mm,4mm)}]frame.north west){\color{white}{\textbf{\sffamily Exercise}}};
\path[fill=green!65!blue] ([xshift=.4mm]n2)--++(0,8mm)--++(7mm,0)--++(0,-8mm)--cycle;
\node at ([shift={(4mm,4mm)}]n2){\color{white}{\textbf{\sffamily \themycounter}}};
\node at ([shift={(18mm,4mm)}]n2){\itshape\textbf{\sffamily MyTitle}};
}}}

Before... 

\begin{tcolorbox}[exercisebox,step=mycounter,label=one]
My exercise here...
\end{tcolorbox}

After...

\end{document}

谢谢 !

答案1

我猜,以下更接近预期的输出。可以使用以下选项按框调整标题文本title=<your title text here>

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcounter{mycounter}

\setlength{\parindent}{0pt}

\begin{document}

\tcbset{exercisebox/.style={
    enhanced,
    colback=white,
    colframe=green!65!black,
    coltitle=black,
    fonttitle =\itshape\bfseries\sffamily,
    boxed title style={
        colback=white,
        colframe=white,
        boxrule=1pt},
    attach boxed title to top right,
    overlay={%
        \path[fill=blue!65,line width=.4mm] (frame.north west)--++(17mm,0)coordinate(n2)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
        \node at ([shift={(5mm,4mm)}]frame.north west){\color{white}{\textbf{\sffamily Exercise}}};
        \path[fill=green!65!blue] ([xshift=.4mm]n2)--++(0,8mm)--++(7mm,0)--++(0,-8mm)--cycle;
    \node at ([shift={(4mm,4mm)}]n2){\color{white}{\textbf{\sffamily \themycounter}}};
    }}}

Before... 

\begin{tcolorbox}[exercisebox,step=mycounter,label=one, title=my title]
My exercise here...
\end{tcolorbox}

After...

\begin{tcolorbox}[exercisebox,step=mycounter,label=one, title=This box gets a different title]
My exercise here...
\end{tcolorbox}

After...

\end{document}

答案2

这类似于 leandris 的解决方案,但有一些变化。

已声明一个带有自动计数器的新exercise框。它有两个参数,一个可选参数可用于包含格式更改,另一个必填参数是标题。如果没有标题,则只需留下空括号即可。

\documentclass[12pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{skins}

\setlength{\parindent}{0pt}

\tcbset{exercisebox/.style={
    enhanced,
    colback=white,
    colframe=green!65!black,
    coltitle=black,
    fonttitle =\itshape\bfseries\sffamily,
    boxed title style={
        colback=white,
        colframe=white,
        boxrule=1pt},
    attach boxed title to top right,
    overlay={%
        \path[fill=blue!65,line width=.4mm] (frame.north west)--++(17mm,0)coordinate(n2)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
        \node at ([shift={(5mm,4mm)}]frame.north west){\color{white}{\textbf{\sffamily Exercise}}};
        \path[fill=green!65!blue] ([xshift=.4mm]n2)--++(0,8mm)--++(7mm,0)--++(0,-8mm)--cycle;
    \node at ([shift={(4mm,4mm)}]n2){\color{white}{\textbf{\sffamily \thetcbcounter}}};
    }}}

\newtcolorbox[auto counter]{exercise}[2][]{exercisebox,
title=#2,#1
}
\begin{document}

Before... 

\begin{exercise}{my title}
My exercise here...
\end{exercise}

After...

\begin{exercise}[colback=green!10]{This box gets a different title}
My exercise here...
\end{exercise}

After...

\begin{exercise}[colback=red!10]{}
An exercise without title
\end{exercise}

\end{document}

在此处输入图片描述

答案3

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcounter{mycounter}

\setlength{\parindent}{0pt}

\begin{document}
    
    \tcbset{exercisebox/.style={enhanced,colback=white,colframe=green!65!black,
            enlarge top by=10mm,
            overlay={%
                \path[fill=blue!65,line width=.4mm] (frame.north west)--++(17mm,0)coordinate(n2)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
                \node at ([shift={(5mm,4mm)}]frame.north west){\color{white}{\textbf{\sffamily Exercise}}};
                \path[fill=green!65!blue] ([xshift=.4mm]n2)--++(0,8mm)--++(7mm,0)--++(0,-8mm)--cycle;
                \node at ([shift={(4mm,4mm)}]n2){\color{white}{\textbf{\sffamily \themycounter}}};
                \node at ([shift={(-8mm,4mm)}]frame.north east){\itshape\textbf{\sffamily MyTitle}};
    }}}
    
    Before... 
    
    \begin{tcolorbox}[exercisebox,step=mycounter,label=one]
        My exercise here...
    \end{tcolorbox}
    
    After...
    
\end{document}

或者

在此处输入图片描述

\documentclass[12pt]{article}
\usepackage{lmodern}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\newcounter{mycounter}

\setlength{\parindent}{0pt}

\begin{document}
    
    \tcbset{exercisebox/.style={enhanced,colback=white,colframe=green!65!black,
            enlarge top by=10mm,
            overlay={%
                \path[fill=blue!65,line width=.4mm] (frame.north west)--++(17mm,0)coordinate(n2)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
                \node at ([shift={(5mm,4mm)}]frame.north west){\color{white}{\textbf{\sffamily Exercise}}};
                \path[fill=green!65!blue] ([xshift=.4mm]n2)--++(0,8mm)--++(7mm,0)--++(0,-8mm)--cycle;
                \node at ([shift={(4mm,4mm)}]n2){\color{white}{\textbf{\sffamily \themycounter}}};
                \path[fill=blue!65,line width=.4mm] (frame.north east)--++(6mm,0)coordinate(n3)--++(0,8mm)--++(-20mm,0) arc (-90:90:-4mm)--cycle;
                \node at ([shift={(-8mm,4mm)}]frame.north east){\color{white}{\itshape\textbf{\sffamily MyTitle}}};
    }}}
    
    Before... 
    
    \begin{tcolorbox}[exercisebox,step=mycounter,label=one]
        My exercise here...
    \end{tcolorbox}
    
    After...
    
\end{document}

相关内容