![如何使用 tcolorbox 创建警告框](https://linux22.com/image/410314/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20tcolorbox%20%E5%88%9B%E5%BB%BA%E8%AD%A6%E5%91%8A%E6%A1%86.png)
我尝试对齐感叹号,但无法使其垂直和水平居中,我的目标是使其看起来与此图像相同
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage[x11names,table]{xcolor}
%-------------------------
\definecolor{orang}{RGB}{255,155,0}
\newtcolorbox[auto counter,number within=section]{caja}[1][]{
enhanced jigsaw,colback=white,colframe=orang,coltitle=orang,
fonttitle=\bfseries\sffamily,
sharp corners,
detach title,
leftrule=22mm,
underlay unbroken and first={\node[below,text=white,font=\sffamily\bfseries,align=center]
at ([xshift=-11mm,yshift=-1mm]interior.north west) {\Huge \centering \textbf{!}};},
breakable,pad at break=1mm,
#1,
code={\ifdefempty{\tcbtitletext}{}{\tcbset{before upper={\tcbtitle\par\medskip}}}},
}
%----------------------
\begin{document}
\section{example}
\begin{caja}[title=warning]
The vertical alignment settings are only relevant for boxes which are larger than their
natural height, see Section 4.10 on page 53.
\end{caja}
\end{document}
你可能已经注意到了,这盒子有标题似乎对对齐有影响
答案1
调整锚点,使用anchor=east
和摆脱\centering
以及使用[xshift=-22.5pt]
产生所需的输出。
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
%-------------------------
\definecolor{orang}{RGB}{255,155,0}
\newtcolorbox[auto counter,number within=section]{caja}[1][]{
enhanced jigsaw,colback=white,colframe=orang,coltitle=orang,
fonttitle=\bfseries\sffamily,
sharp corners,
detach title,
leftrule=22mm,
% What you need %%%%%%%%%%%%
underlay unbroken and first={\node[below,text=black,anchor=east]
at ([xshift=-22.5pt]interior.base west) {\Huge \textbf{!}};},
%%%%%%%%%%%%%%%%%%%%%%%%
breakable,pad at break=1mm,
#1,
code={\ifdefempty{\tcbtitletext}{}{\tcbset{before upper={\tcbtitle\par\medskip}}}},
}
%----------------------
\begin{document}
\section{example}
\begin{caja}[title=warning]
One-line text.
\end{caja}
\begin{caja}[title=warning]
The vertical alignment settings are only relevant for boxes which are larger than their natural height, see Section 4.10 on page 53.
\end{caja}
\begin{caja}[title=warning]
The vertical alignment settings are only relevant for boxes which are larger than their natural height, see Section 4.10 on page 53. The vertical alignment settings are only relevant for boxes which are larger than their natural height, see Section 4.10 on page 53. The vertical alignment settings are only relevant for boxes which are larger than their natural height, see Section 4.10 on page 53.
\end{caja}
\end{document}