代码

代码

我想制作定制tcolorbox盒子,但在标题背景方面遇到了障碍。

我知道如何绘制框架,但我不知道如何在标题后面应用红色背景。我想tcolorbox在开始使用 tikz 方式之前先“按这种方式”应用它。

注意定制版的字体颜色已经是红色的,必须改为白色。

这个问题最好从代码本身来理解,因此代码如下:

旁注:我刚刚注意到第二个盒子也稍微宽了一点。嗯,这不太好。

代码

\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{lipsum}

% Basic Version
\NewDocumentCommand{\ithhint}{ O{} m }{%
\tcbset{colback=white,colframe=red!55!black,boxrule=0.5pt,title={#1},fonttitle=\bfseries}
\begin{tcolorbox}[]
#2
\end{tcolorbox}
}%

% Customized Version
\NewDocumentCommand{\ithnote}{ O{} m }
{
\colorlet{colornote}{red!55!black}
\newtcolorbox[]{ithnotebox}{%
    % Example Frame Start
    empty,% Empty previously set parameters
    title={#1},% use \thetcbcounter to access the ithexample counter text
    % Attaching a box requires an overlay
    attach boxed title to top left,
       % Ensures proper line breaking in longer titles
       minipage boxed title,
    % (boxed title style requires an overlay)
    boxed title style={empty,colframe=red,size=normal,boxsep=0pt,toprule=0pt,top=4pt,left=3mm,right=3mm,overlay={}},
    coltitle=colornote,fonttitle=\bfseries,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=3mm,top=2pt,breakable,pad at break=0mm,
       before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
    % Handles box when it exists on one page only
    overlay unbroken={\draw[colornote,rounded corners,line width=.5pt] 
    ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west) --
    ([xshift=-0pt]frame.south west) -- ([xshift=-0pt]frame.south east) --
    ([xshift=-0pt]frame.south east) -- ([xshift=-0pt]title.north east) --
    cycle ; },
    % Handles multipage box: first page
    overlay first={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
    % Handles multipage box: middle page
    overlay middle={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
    % Handles multipage box: last page
    overlay last={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
    }
\begin{ithnotebox}
  #2
\end{ithnotebox}\endlist
}

\begin{document}
\ithhint[Hint]{Check this out. How can I customize the hell out of this?}
\ithnote[Note]{I would like to make this box look like the hint box. I will give it a shot with some Latin. \lipsum[1]}
\end{document}

输出

在此处输入图片描述

答案1

我不确定我是否正确理解了这个问题。

我已应用该skin=enhancedfirst jigsaw键和interior style={fill=colornote}盒装标题样式。

不同的宽度是由于不同的boxrule设置造成的。

\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{lipsum}

% Basic Version
\NewDocumentCommand{\ithhint}{ O{} m }{%
\tcbset{colback=white,colframe=red!55!black,boxrule=0.5pt,title={#1},fonttitle=\bfseries}%
\begin{tcolorbox}
  #2
\end{tcolorbox}
}%

\newtcolorbox{ithnotebox}[1][]{%
    % Example Frame Start
    empty,% Empty previously set parameters
    % Attaching a box requires an overlay
    title={#2},% use \thetcbcounter to access the ithexample counter text,
    attach boxed title to top left,
       % Ensures proper line breaking in longer titles
    minipage boxed title,
       % (boxed title style requires an overlay)
    boxed title style={empty,skin=enhancedfirst jigsaw,colframe=red,size=normal,boxrule=0.5pt,boxsep=0pt,toprule=0pt,top=4pt,left=3mm,right=3mm,overlay={},
      interior style={fill=colornote},
    },
    fonttitle=\bfseries,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=3mm,top=2pt,breakable,pad at break=0mm,
    before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
    % Handles box when it exists on one page only
    overlay unbroken={\draw[colornote,rounded corners,line width=.5pt] 
      ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west) --
      ([xshift=-0pt]frame.south west) -- ([xshift=-0pt]frame.south east) --
      ([xshift=-0pt]frame.south east) -- ([xshift=-0pt]title.north east) --
      cycle ; },
       % Handles multipage box: first page
       overlay first={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
       % Handles multipage box: middle page
       overlay middle={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
       % Handles multipage box: last page
       overlay last={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
    #1
    }


% Customized Version
\NewDocumentCommand{\ithnote}{ O{} m }
{
\colorlet{colornote}{red!55!black}
\begin{ithnotebox}[title={#1}]
  #2
\end{ithnotebox}\endlist
}

\begin{document}
\ithhint[Hint]{Check this out. How can I customize the hell out of this?}
\ithnote[Note]{I would like to make this box look like the hint box. I will give it a shot with some Latin. \lipsum[1]}
\end{document}

在此处输入图片描述

更新

\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{showframe}
\usepackage{lipsum}

% Basic Version
\NewDocumentCommand{\ithhint}{ O{} m }{%
\tcbset{colback=white,colframe=red!55!black,boxrule=0.5pt,title={#1},fonttitle=\bfseries,equal height group=hintnote}%
\begin{tcolorbox}
  #2
\end{tcolorbox}
}%

\newtcolorbox{ithnotebox}[1][]{%
    % Example Frame Start
    empty,% Empty previously set parameters
    % Attaching a box requires an overlay
    %title={#2},% use \thetcbcounter to access the ithexample counter text,
    attach boxed title to top left,
       % Ensures proper line breaking in longer titles
    minipage boxed title,
       % (boxed title style requires an overlay)
    boxed title style={empty,skin=enhancedfirst jigsaw,colframe=colornote,size=normal,boxrule=0.5pt,boxsep=0pt,toprule=0pt,top=4pt,left=3mm,right=3mm,overlay={},
      interior style={fill=colornote},
    },
    boxrule=0.5pt,
    fonttitle=\bfseries,
    before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=3mm,right=3mm,top=2pt,breakable,pad at break=0mm,
    before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
    % Handles box when it exists on one page only
    overlay unbroken={\draw[colornote,rounded corners,line width=.5pt] 
      ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west) --
      ([xshift=-0pt]frame.south west) -- ([xshift=-0pt]frame.south east) --
      ([xshift=-0pt]frame.south east) -- ([xshift=-0pt]title.north east) --
      cycle ; },
       % Handles multipage box: first page
       overlay first={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]title.north west) -- ([xshift=-0pt]frame.south west); },
       % Handles multipage box: middle page
       overlay middle={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },
       % Handles multipage box: last page
       overlay last={\draw[colornote,rounded corners,line width=.5pt] ([xshift=-0pt]frame.north west) -- ([xshift=-0pt]frame.south west); },%
       equal height group=hintnote,
    #1
    }


% Customized Version
\NewDocumentCommand{\ithnote}{ O{} m }
{
\colorlet{colornote}{red!55!black}
\begin{ithnotebox}[title={#1}]
  #2
\end{ithnotebox}\endlist
}

\begin{document}
\ithhint[Hint]{Check this out. How can I customize the hell out of this?}
\ithnote[Note]{I would like to make this box look like the hint box. I will give it a shot with some Latin. \lipsum[1]}
\end{document}

相关内容