5

I made this with 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}

Image

How to add text in, adjust the banlen value with the Boxname text and adjust the box size ? And how to make a environment with a color parameter ?

I have some difficult with the coordinates :s

Thanks.

LaRiFaRi
  • 43,807
iDavid
  • 53

2 Answers2

8

I would recommend to use nodes and their coordinates to draw the lines and not to do this manually. TikZ allows you to write \node \bgroup .. \egroup; instead of \node { .. }; and therefore you can wrap a node and the whole tikzpicture around the content of an environment. If you use font size specific units, i.e. ex and em, the whole box will scale nicely with the font size:

\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}

Image

Martin Scharrer
  • 262,582
  • I don't really know how to use \bgroup. I have the French TikZ doc. But I dont find anything for \bgroup. (Thanks for your quick reply) – iDavid Jul 05 '11 at 20:22
  • @iDavid: \bgroup is { as macro and \egroup is } as macro. You can't have a { on its own in a macro or environment definition, so you need to use \bgroup and then \egroup instead. However, you can only do this for macros like \hbox or (with Tikz) \node which process the content as box. It doesn't work for normal macro arguments, e.g. \texttt\bgroup..\egroup instead of \texttt{..} doesn't work. – Martin Scharrer Jul 05 '11 at 20:31
  • How I can change the width of the box with something like 0.5\textwidth ? I didn't yet very familiar with TikZ. Do you have any site or documentation, tuts for make some useful things like this box ? – iDavid Jul 05 '11 at 20:38
  • @iDavid: You can configure the width with the first argument, i.e. \begin{mybox}{.5\textwidth}{<title>}<content>\end{mybox}. This is internally done with the text width option. For documentation and examples you only know the pgfmanual and http://www.texample.net/tikz/examples/. – Martin Scharrer Jul 05 '11 at 20:42
  • Thanks. I thought it was just for the size of characters. – iDavid Jul 05 '11 at 20:55
4

tcolorbox is a good tool for this kind of boxes. It uses TiKZ as drawing engine and with its recent versions is easier to compound boxes with detached titles like this one. Some examples follow:

\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}

enter image description here

Ignasi
  • 136,588
  • @PeterGrill I'm using version 3.21, which is the most recent (10/10/2014). I'm not sure when these keys where introduced but I think within last two or three releases. – Ignasi Oct 23 '14 at 17:37
  • So sorry. I had switched to using TeXLive2013 last night and forgot to switch back... Works great with TeXLive2014!! – Peter Grill Oct 23 '14 at 17:42