8

I'd like to create a new style in TikZ for nodes with transparent background, rounded corners, with labels oriented vertically on their left on a opaque fill (similar to a tabbed paper). A sample sketch is given below. The "Label" will be given to the style as parameter.

tabbed box

  • 1
    You're pretty good at sketching. ;-) – Paul Gessler Jun 24 '15 at 21:47
  • @PaulGessler, I first sketched one by hand, it looked terrible though =P – Ali Jun 24 '15 at 21:51
  • I feel like this is a job better suited to mdframed or tcolorbox, both of which are based on TikZ (AFAIK). – Sean Allred Jun 24 '15 at 22:08
3

Here's the solution with Tikz since you were asking for it. I basically defined a new command with four arguments:

 \tabb{#1}{#2}{#3}{#4}
  1. Position of the node
  2. Color (the second color will automatically be lighter than the label)
  3. Label text
  4. Main text

I changed the x and y of the tikzpicture above for making it easier for me to place the examples.

Output

figure 1

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{
    rect/.style={%
        draw,%  
        line width=.4pt,
        rounded corners,                 
        rectangle split,
        rectangle split parts=2,
        rectangle split horizontal,
        rectangle split part fill={#1,none},
        every text node part/.style={text=white, font=\scriptsize\sffamily},
    } 
}

\newcommand{\tabb}[4]{
    \node[rect=#2] (a) at (#1) {\rotatebox{90}{#3}\nodepart{two}#4};
    \begin{pgfonlayer}{background}
    \fill[#2!30, rounded corners] (a.south west) rectangle (a.north east);
    \end{pgfonlayer}    
}

\begin{document}
\begin{tikzpicture}[x=2cm,y=1.5cm]

\tabb{1,1}{blue}{Label}{bla bla}
\tabb{1,0}{red}{Label}{bla bla}
\tabb{0,1}{green}{Label}{bla bla}
\tabb{0,0}{orange}{Label}{bla bla}

\end{tikzpicture}
\end{document}
10

You can do this with tcolorbox:

enter image description here

The code (adjust the settings according to your needs):

\documentclass{article}
\usepackage[many]{tcolorbox}

\definecolor{bluebg}{RGB}{201,218,248}
\definecolor{bluelabel}{RGB}{0,0,255}

\newtcbox{\labelbox}{
  enhanced,
  nobeforeafter,
  tcbox raise base,
  boxrule=0.4pt,
  top=0.4cm,
  bottom=0.4cm,
  right=0pt,
  left=4mm,
  arc=5pt,
  boxsep=2pt,
  before upper={\vphantom{dlg}},
  colframe=bluelabel!50!black,
  coltext=black,
  colback=bluebg,
  overlay={
    \begin{tcbclipinterior}
      \fill[bluelabel] (frame.south west)
      rectangle 
      node[
        text=white,
        font=\sffamily\bfseries\footnotesize,
        rotate=90] {LABEL} ([xshift=4mm]frame.north west);
    \end{tcbclipinterior}
  }
}

\begin{document}

\labelbox{some test text}

\end{document}

Update

Now the label is given as an argument, there are two optional arguments: the first one, controlling attributes for the main part of the box, and the second one, controlling the color for the label background. I illustrate the example of the remember as key to be used to reference the boxes in a tikzpicture:

\documentclass{article}
\usepackage[many]{tcolorbox}

\definecolor{bluebg}{RGB}{201,218,248}
\definecolor{bluelabel}{RGB}{0,0,255}

\DeclareTCBox{\mylib}{O{}mO{}}{
  enhanced,
  nobeforeafter,
  tcbox raise base,
  boxrule=0.4pt,
  top=0.4cm,
  bottom=0.4cm,
  right=0pt,
  left=4mm,
  arc=5pt,
  boxsep=2pt,
  before upper={\vphantom{dlg}},
  colframe=bluelabel!50!black,
  coltext=black,
  colback=bluebg,
  overlay={
    \begin{tcbclipinterior}
      \fill[bluelabel,#3] 
        (frame.south west)
        rectangle 
        node[
          text=white,
          font=\sffamily\bfseries\footnotesize,
          rotate=90
        ] {#2} 
        ([xshift=4mm]frame.north west);\end{tcbclipinterior}
  },
  #1
}

\begin{document}

\begin{center}
\mylib[remember as=box1]{Label1}{some test text for box1}\qquad
\mylib[remember as=box2,colback=orange!40]{Label2}[red]{text for box2}
\end{center}

\begin{tikzpicture}[
  remember picture,
  overlay,
  line width=3pt,
  shorten >= 3pt,
  shorten <= 3pt,
  ]
\draw[->,red!75!black] 
  (box1.north) to[bend left] (box2.north);
\draw[->,green!75!black] 
  (box2.south) to[bend left] (box1.south);
\end{tikzpicture}

\end{document}

enter image description here

  • Thanks, Gonzalo. Can I use this inside a TikZ picture with other elements in there? – Ali Jun 24 '15 at 22:00
  • @Ali Sure. I updated my answer with an example referencing boxes in a tikzpicture, but you could use them directly too. – Gonzalo Medina Jun 25 '15 at 2:38
  • @Ali But if you are planning to use this as kind of a node, the answer by John Kormylo might be a better option. – Gonzalo Medina Jun 25 '15 at 2:51
5

The \parbox isn't really needed, but I wasn't sure if one could specify the text widths for each part separately.

\documentclass[border=.1in]{standalone}
\usepackage{adjustbox}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}
\begin{document}
\begin{tikzpicture}
\node[rectangle split,rectangle split horizontal,rounded corners,rectangle split parts=2,
  draw,rectangle split part fill={blue,LightBlue}]
  {\rotatebox{90}{\color{white}Label}
  \nodepart{two}
  \parbox{1in}{text}};
\end{tikzpicture}
\end{document}

split node

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.