.sty 文件中的 newtcolorbox 标题定位和可选参数存在问题

.sty 文件中的 newtcolorbox 标题定位和可选参数存在问题

我正在尝试重新创造信息框,但是我在为框的标题和标题的位置制作可选参数时遇到了麻烦。是我目前所拥有的。从美学上讲,它几乎是完美的,只是标题没有与图标一起居中。虽然这不是主要问题,但只是“锦上添花”。以下是我为该框使用的代码:

\RequirePackage[most]{tcolorbox}
\RequirePackage{fontawesome}

\definecolor{tipsColor}{RGB}{142, 68, 173}

\makeatletter
\newtcolorbox{tipNote}[1][]{%
  enhanced jigsaw,
  borderline west={2pt}{0pt}{tipsColor},
  sharp corners,
  boxrule=0pt,
  fonttitle={\large\bfseries},
  coltitle={black},
  title={\textcolor{tipsColor}{\huge\faLightbulbO} Tip\\},
  attach title to upper,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip},
  #1
}
\makeatother

\endinput    

我是这样称呼它的:

\begin{tipNote}
\blindtext
\end{tipNote}

并且运行正常。但我希望能够有可选的自定义标题,以便我可以像上面那样调用它并获得默认标题,或者执行以下操作之一并获得自定义标题:

\begin{tipNote}{Example}
\blindtext
\end{tipNote}

\begin{tipNote}[Example]
\blindtext
\end{tipNote}

这是我获取自定义标题的尝试:

\makeatletter
\newtcolorbox{tipNote}[2][Tip]{%
  enhanced jigsaw,
  borderline west={2pt}{0pt}{tipsColor},
  sharp corners,
  boxrule=0pt,
  fonttitle={\large\bfseries},
  coltitle={black},
  title={\textcolor{tipsColor}{\huge\faLightbulbO} #2\\},
  attach title to upper,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip},
  #1
}
\makeatother

这是我得到的错误:

! Package pgfkeys Error: I do not know the key '/tcb/Tip' and I am going to ignore it. Perhaps you misspelled it.

See the pgfkeys package documentation for explanation.

我该如何解决这个问题?如果我做错了,正确的方法是什么?

编辑:这是正在使用的 .sty 文件的完整示例:

\documentclass{article}

\usepackage{notes} % name of package with note boxes
\usepackage{blindtext}

\begin{document}

\begin{tipNote}
\blindtext
\end{tipNote}

\end{document}

这里是完整的.sty 文件,供参考。

答案1

您的示例几乎实现了您的要求。

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{fontawesome,blindtext}

\definecolor{tipsColor}{RGB}{142, 68, 173}

\makeatletter
\newtcolorbox{tipNote}[1][Tip]{%
  enhanced jigsaw,
  borderline west={2pt}{0pt}{tipsColor},
  sharp corners,
  boxrule=0pt,
  fonttitle={\large\bfseries},
  coltitle={black},
  title={\textcolor{tipsColor}{\huge\faLightbulbO} #1\\},
  attach title to upper,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip}
}
\makeatother
\begin{document}
\begin{tipNote}[Example]
\blindtext
\end{tipNote}

\begin{tipNote}
Always be nice to marmots!
\end{tipNote}

\end{document}

在此处输入图片描述

不过,我建议使用更灵活的版本,你可以添加标题更多可选选项。

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{fontawesome,blindtext}

\definecolor{tipsColor}{RGB}{142, 68, 173}

\makeatletter
\tcbset{my title/.store in=\mytitle,my title={Tip}}
\newtcolorbox{tipNote}[1][]{%
  enhanced jigsaw,
  borderline west={2pt}{0pt}{tipsColor},
  sharp corners,
  boxrule=0pt,
  fonttitle={\large\bfseries},
  coltitle={black},
  title={$\vcenter{\hbox{\textcolor{tipsColor}{\huge\faLightbulbO}}}~\vcenter{\hbox{\mytitle}}$\\},
  attach title to upper,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip},
  #1
}
\makeatother
\begin{document}
\begin{tipNote}[my title=Example]
\blindtext
\end{tipNote}

\begin{tipNote}[borderline east={2pt}{0pt}{red!75!black},top=5pt,bottom=5pt]
Always be nice to marmots!
\end{tipNote}

\begin{tipNote}[borderline east={2pt}{0pt}{red!75!black},top=5pt,bottom=5pt,my
title=Another tip]
Also be friendly to ducks!
\end{tipNote}

\begin{tipNote}[borderline east={2pt}{0pt}{blue},top=5pt,bottom=5pt]
Always be nice to koala bears!
\end{tipNote}
\end{document}

在此处输入图片描述

相关内容