如何使用此 TikZ 代码创建文本框?

如何使用此 TikZ 代码创建文本框?

我用 TikZ 做了这个:

\usetikzlibrary{calc}
\def\banlen{3}
\xdefinecolor{mycolor}{RGB}{62,96,111} % Neutral Blue
\def\bancolor{mycolor}
\begin{tikzpicture}
\draw (0.25,0.25) rectangle (5,-2); %
\fill[\bancolor](0,0) -- (0.25,-0.25) -- (0.25,0); % Coin bas-gauche
\fill[\bancolor](\banlen,0.5) -- (\banlen-0.25,0.75) -- (\banlen-0.25,0.5); %Coin haut-droit
\fill[\bancolor!75] (0,0) rectangle (\banlen,0.5); % Rectangle (banner)
\draw (\banlen/2,0.25) node {Box name};
\end{tikzpicture}

图像

如何添加文本,使用 Boxname 文本调整 banlen 值并调整框大小?以及如何创建带有颜色参数的环境?

我对坐标有些困惑 :s

谢谢。

答案1

我建议使用节点及其坐标来绘制线条,而不是手动执行此操作。TikZ 允许您写入\node \bgroup .. \egroup;而不是\node { .. };,因此您可以将节点和整体包裹tikzpicture在环境的内容周围。如果您使用字体大小特定的单位,即exem,整个框将随着字体大小很好地缩放:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\xdefinecolor{mycolor}{RGB}{62,96,111} % Neutral Blue
\colorlet{bancolor}{mycolor}

\def\bancolor{mycolor}
\newenvironment{mybox}[3][]{%
    \begin{tikzpicture}[#1]%
        \def\myboxname{#3}%
        \node [draw,inner sep=1.5ex,text width=#2]% good options: minimum height, minimum width
            (BOXCONTENT) \bgroup\rule{0pt}{3ex}\ignorespaces
}{%
        \egroup;
        \node [right,inner xsep=1em,fill=bancolor!75,outer sep=0pt,text height=2ex,text depth=.5ex] (BOXNAME) 
            at ([shift={(-1em,0pt)}]BOXCONTENT.north west) {\myboxname};
        \fill[bancolor] (BOXNAME.north east) -- +(-1em,1em) -- +(-1em,0) -- cycle;
        \fill[bancolor] (BOXNAME.south west) -- +(1em,-1em) -- +(1em,0) -- cycle;
    \end{tikzpicture}
}

\begin{document}

\begin{mybox}{10em}{Test}
    This is the content
\end{mybox}

\begin{mybox}{15em}{Test it really good}
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
\end{mybox}

\huge

\begin{mybox}{10em}{Test}
    This is the content
\end{mybox}

\begin{mybox}{15em}{Test it really good}
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
\end{mybox}
\tiny

\begin{mybox}{10em}{Test}
    This is the content
\end{mybox}

\begin{mybox}{15em}{Test it really good}
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
    This is the longer content
\end{mybox}


\end{document}

图像

答案2

tcolorbox是处理此类框的好工具。它使用TiKZ绘图引擎,并且其最新版本更容易组合带有独立标题的框,例如此框。以下是一些示例:

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

\definecolor{bancolor}{RGB}{62,96,111}

\tcbset{myboxstyle/.style={enhanced,skin=enhanced jigsaw,
attach boxed title to top left={xshift=-3mm,yshift=-\tcboxedtitleheight/2},
coltitle=black,varwidth boxed title=0.7\linewidth,
colbacktitle=#1!75, colback=white,sharp corners,
top=2ex,
boxed title style={sharp corners, boxrule=0pt},
underlay boxed title={
\fill[#1] (title.south west) -- (title.south-|frame.west)--++(0,-.5*\tcboxedtitleheight)--cycle;
\fill[#1] (title.north east) --++(-.5*\tcboxedtitleheight,0)--++(0,.5*\tcboxedtitleheight)--cycle;}},
    myboxstyle/.default=bancolor}


\newtcolorbox{mybox}[2][]{myboxstyle,
title={\hspace*{.5cm}#2\hspace*{.5cm}},#1}

\begin{document}
\begin{mybox}{My Title}
\lipsum[1]
\end{mybox}

\begin{mybox}[width=.5\linewidth]{My Title}
A short text
\end{mybox}

\tcbox[myboxstyle=red,title=My title]{A short box}
\end{document}

在此处输入图片描述

相关内容