使用 varwidth 在 tcolorbox 内进行垂直对齐

使用 varwidth 在 tcolorbox 内进行垂直对齐

我以前tcolorbox有一个“警告”框。我希望“警告标志”位于其最顶部,但即使使用[t]环境选项也无法实现varwidth

这是 MWE:

\documentclass[a4paper, 11pt]{report}
\usepackage[T1]{fontenc} % encoding
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\usepackage{varwidth}
\usepackage{lipsum}

\newenvironment{attention}{
    \begin{varwidth}[t]{0.05\textwidth}
    \begin{tikzpicture}
        \draw [ultra thick, red, rounded corners, fill=white] (0,0) -- (1,0) -- (0.5,1)--cycle;
        \node [scale=2] at (0.5,0.4) {$ ! $};
        \pgfresetboundingbox;
        \clip (0.16,0.12) rectangle (0.83,0.80);
    \end{tikzpicture}
    \end{varwidth}
    %
    \begin{varwidth}{0.93\textwidth}
}{\end{varwidth}}

\tcbset{
    myattention_box/.style={
        blanker,
        %before skip=6pt,after skip=15pt,
        borderline west={1.5pt}{8.9pt}{red},
        grow to left by=17pt, % to have the borderline within the left margin
    },
}
\tcolorboxenvironment{attention}{myattention_box}

\begin{document}
    \begin{attention}
        \lipsum[1]
    \end{attention}
\end{document}

请注意,“警告标志”略微向上移动,但不是在最顶部: “警告标志”略微向上移动,但并非位于最顶部

答案1

请保留[t]两个varwidth框中的选项,

\documentclass[a4paper, 11pt]{report}
\usepackage[T1]{fontenc} % encoding
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\usepackage{varwidth}
\usepackage{lipsum}

\newenvironment{attention}{
    \begin{varwidth}[t]{0.05\textwidth}
    \begin{tikzpicture}
        \draw [ultra thick, red, rounded corners, fill=white] (0,0) -- (1,0) -- (0.5,1)--cycle;
        \node [scale=2] at (0.5,0.4) {$ ! $};
        \pgfresetboundingbox;
        \clip (0.16,0.12) rectangle (0.83,0.80);
    \end{tikzpicture}
    \end{varwidth}
    %
    \begin{varwidth}[t]{0.93\textwidth}
}{\end{varwidth}}

\tcbset{
    myattention_box/.style={
        blanker,
        %before skip=6pt,after skip=15pt,
        borderline west={1.5pt}{8.9pt}{red},
        grow to left by=17pt, % to have the borderline within the left margin
    },
}
\tcolorboxenvironment{attention}{myattention_box}

\begin{document}
    \begin{attention}
        \lipsum[1]
    \end{attention}
\end{document}

答案2

我修正了MadyYuvi回答的一些参数:

\documentclass[a4paper, 11pt]{report}
\usepackage[T1]{fontenc} % encoding
\usepackage{tikz}
\usepackage[many]{tcolorbox}
\usepackage{varwidth}
\usepackage{lipsum}

\newenvironment{attention}{
    \begin{varwidth}[t]{0.05\textwidth}
        \begin{tikzpicture}
            \draw [yshift=-7pt,ultra thick, red, rounded corners, fill=white] (0,0.1) -- (.9,0.1) -- (0.45,1.1)--cycle;
            \node [scale=2] at (0.45,.2) {$ ! $};
            \pgfresetboundingbox;
            \clip (0.16,0.12) rectangle (0.83,0.80);
        \end{tikzpicture}
    \end{varwidth}
    %
    \begin{varwidth}[t]{0.9\textwidth}
    }{\end{varwidth}}

\tcbset{
    myattention_box/.style={
        blanker,
        %before skip=6pt,after skip=15pt,
        borderline west={1.5pt}{8pt}{red},
        grow to left by=17pt, % to have the borderline within the left margin
    },
}
\tcolorboxenvironment{attention}{myattention_box}

\begin{document}
    \begin{attention}
        \lipsum[1]
    \end{attention}
\end{document}

因此输出是: 在此处输入图片描述

相关内容