tcolorbox 带框架标题的精美文本框 - 标题调整

tcolorbox 带框架标题的精美文本框 - 标题调整

我正在使用带标题的文本框,这是@Alenanno 在 灵感来自 Indesign 的精美盒子

此标题设计为右对齐。有人可以将其改为左对齐吗?

\documentclass[a4paper]{article}
\usepackage{amsmath}
\usepackage{amsmath,amssymb}%      pour les maths
\usepackage{enumitem}


\usepackage[svgnames]{xcolor}
\usepackage[most]{tcolorbox}
\usepackage{tikz}

\tcbset{
    lemmastyle/.style={enhanced, colback=white, colframe=blue!20, arc=0pt, 
                       fonttitle=\bfseries, description color=Maroon,  
                       colbacktitle=white, coltitle=DarkOliveGreen,    
                       top=\tcboxedtitleheight,
                       boxed title style={arc=0pt},
                       attach boxed title to top left={yshift=-\tcboxedtitleheight/2,
                                                       xshift=4mm}%
                      },
}

\newtcbtheorem{myLemma}{Long text here without counter }{lemmastyle}{thm}

\usetikzlibrary{calc, fit}



\newcommand{\mybox}[4][8cm]{
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\node[line width=0.5mm, rounded corners, text width=#1, draw=#2] (one) {\vspace{25pt}\\ #4};
\node[text=white,anchor=north east,align=center, minimum height=20pt] (two) at (one.north east) {#3};
\path[fill=#2] 
    (one.north west|-two.west) --
    ($(two.west)+(-1.5cm,0)$) 
    to[out=0,in=180] (two.south west) --
    (two.south east) [rounded corners] --
    (one.north east) -- 
    (one.north west) [sharp corners] -- cycle;
\node[text=white,anchor=north east,align=center, minimum height=25pt, text height=2ex] (three) at (one.north east) {#3 \hspace*{.5mm}};
\end{tikzpicture} 
\end{figure}
}

\usepackage{pifont}

\begin{document}


\mybox[6cm]{green!70!black}{Long Fancy Title}{
\begin{enumerate}
\item Show that 
${\displaystyle D_2f(x,y) = \frac{\partial {}}{\partial{y}} \left ( \int_0^xg_1 (t,0) \ dt + \int_0^y g_2(x,s) \ ds \right)}$
\item prove that 
${ \displaystyle \left(\forall x\in\mathbb{R}  \right)\left(\forall y \in \mathbb{R} \right) x\neq y\, \text{and} \, x+y \neq 2 \implies x^{2}-2x \neq y^2-2y }$ 
\end{enumerate}
}

\begin{myLemma}{}{}
\begin{enumerate}
\item Show that 
${\displaystyle D_2f(x,y) = \frac{\partial {}}{\partial{y}} \left ( \int_0^xg_1 (t,0) \ dt + \int_0^y g_2(x,s) \ ds \right)}$
\item prove that 
${ \displaystyle \left(\forall x\in\mathbb{R}  \right)\left(\forall y \in \mathbb{R} \right) x\neq y\, \text{and} \, x+y \neq 2 \implies x^{2}-2x \neq y^2-2y }$ 
\end{enumerate}
\end{myLemma}

\mybox[6cm]{blue!70!black}{Very Very Long Fancy Title}{Duis id dolor et ligula eleifend imperdiet. Mauris luctus, quam vitae viverra sagittis, dolor nibh imperdiet augue, eu venenatis eros augue et nisl. Vivamus nec fermentum est.}

Nullam libero augue, luctus et est vitae, fermentum aliquet libero. Maecenas dictum placerat eros, eu fermentum sem fermentum dapibus. Quisque non tellus nec magna feugiat luctus. 

\end{document}

答案1

阿莱南诺mybox用命令定义TiKZ。可以使用tcolorbox允许页面间可破坏的框来定义。

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\makeatletter
\newtcolorbox{mytcbox}[2][]{%
enhanced, 
breakable,
colback=white,
colframe=blue!30!black,
attach boxed title to top left={yshift=-2pt}, title={#2},
boxed title size=standard,
boxrule=0pt,
boxed title style={%
    sharp corners, 
    rounded corners=northwest, 
    colback=tcbcolframe, 
    boxrule=0pt},
sharp corners=north,
overlay unbroken={%
    \path[fill=tcbcolback] 
        ([xshift=-2pt]title.south east) 
        to[out=0, in=180] ([xshift=1.5cm]title.east)--
        (title.east-|frame.east) |- 
        ([xshift=-2pt]title.south east)--cycle;
    \path[fill=tcbcolframe] (title.south east) 
        to[out=0, in=180] ([xshift=1.5cm]title.east)--
        (title.east-|frame.east)
        [rounded corners=\kvtcb@arc] |- 
        (title.north-|frame.north) 
        [sharp corners] -| (title.south east);
    \draw[line width=.5mm, rounded corners=\kvtcb@arc, 
        tcbcolframe] 
        (title.north west) rectangle 
        (frame.south east);
}, 
overlay first={%
    \path[fill=tcbcolback] 
        ([xshift=-2pt]title.south east) 
        to[out=0, in=180] ([xshift=1.5cm]title.east)--
        (title.east-|frame.east) |- 
        ([xshift=-2pt]title.south east)--cycle;
    \path[fill=tcbcolframe] (title.south east) 
        to[out=0, in=180] ([xshift=1.5cm]title.east)--
        (title.east-|frame.east)
        [rounded corners=\kvtcb@arc] |- 
        (title.north-|frame.north) 
        [sharp corners] -| (title.south east);
    \draw[line width=.5mm, rounded corners=\kvtcb@arc, 
        tcbcolframe] 
        (frame.south west) |- (title.north) -| 
        (frame.south east);
}, 
overlay middle={%
    \draw[line width=.5mm, tcbcolframe] 
    (frame.north west)--(frame.south west) 
    (frame.north east)--(frame.south east);
}, 
overlay last={%
    \draw[line width=.5mm, rounded corners=\kvtcb@arc, 
        tcbcolframe] 
        (frame.north west) |- (frame.south) -|
        (frame.north east);
}, 
#1
}
\makeatother

\begin{document}

\begin{mytcbox}{Long fancy title}
\lipsum[1-12]
\end{mytcbox}

\begin{mytcbox}[colframe=blue!70!black, colback=red!20, width=.75\linewidth]{Even Longer fancy title}
\lipsum[1]
\end{mytcbox}

\begin{mytcbox}[colback=white]{Title}
\lipsum[1]
\end{mytcbox}

\end{document}

笔记以前的代码已更新以兼容tcolorbox v4.20。如果您使用的是以前的版本,请将颜色形式重命名tcbcol...为旧形式 tcbcol@...

答案2

您需要重建\mybox命令里面完成的路径:

\usetikzlibrary{calc, fit, backgrounds}
\usepackage{varwidth}
\newcommand{\mybox}[4][\textwidth-\pgfkeysvalueof{/pgf/inner xsep}-2mm]{%
\begin{figure}[!h]
\centering
\begin{tikzpicture}
\node[line width=0.5mm, inner ysep=10pt, text width=#1, outer sep=0] (text) {\begin{varwidth}{\textwidth}#4\end{varwidth}};
\node[text=white, above right, align=left] (title) at (text.north west) {#3};
\node[fit=(title) (text), line width=0.5mm, rounded corners, draw=#2, inner sep=0pt] (box){};
\begin{scope}[on background layer]
\fill[#2] (title.south west) -- (title.south east) to[out=0, in=180] ([xshift=1.5cm]title.east) -- (title.east-|box.north east) [rounded corners] -- (box.north east) -- (box.north west) [sharp corners] -- cycle;
\end{scope}
\end{tikzpicture} 
\end{figure}
}

还用到了backgrounds库,避免了白色节点的重复,以及fit库+vardiwdth包使得盒子更加灵活。

在此处输入图片描述

相关内容