如何使用 TikZ 创建选项卡框?

如何使用 TikZ 创建选项卡框?

我想在 TikZ 中为具有透明背景、圆角的节点创建一个新样式,标签垂直放置在左侧不透明填充上(类似于标签纸)。下面给出了一个示例草图。“标签”将作为参数提供给样式。

标签框

答案1

由于您要求,因此这是使用 Tikz 的解决方案。我基本上定义了一个带有四个参数的新命令:

 \tabb{#1}{#2}{#3}{#4}
  1. 节点的位置
  2. 颜色(第二种颜色将自动比标签浅)
  3. 标签文本
  4. 正文

我改变了上面的 tikzpicture 的xy,以便于我放置示例。

输出

图1

代码

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

答案2

你可以这样做tcolorbox

在此处输入图片描述

代码(根据需要调整设置):

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

更新

现在标签作为参数给出,有两个可选参数:第一个参数控制框主要部分的属性,第二个参数控制标签背景的颜色。我举例说明了remember as用于引用框的键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}

在此处输入图片描述

答案3

实际上\parbox并不是需要,但我不确定是否可以分别为每个部分指定文本宽度。

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

分裂节点

相关内容