Tikz:如何在節點右側的圖片中心建立一條線?

Tikz:如何在節點右側的圖片中心建立一條線?

代碼:

\documentclass[12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, decorations.pathreplacing,}
\begin{document}
\newlength{\distance}
\setlength{\distance}{0.6cm}
\begin{tikzpicture}[node distance = 1cm, auto]
\tikzset{
    block/.style = {rectangle, draw, text centered},
    brace/.style = {decorate,decoration={brace,amplitude=4pt}},
    caption/.style = {black, midway, xshift = 1mm},
}
\count0=0
\node [block, minimum height=2\distance] (\the\count0) {100\%};

\count1=\count0
\advance\count0 by 1
\draw [brace] (\the\count1.north east) -- 
    ($(\the\count1.north east) + (0, -\distance)$) node [caption] (\the\count0) {50\%};

\advance\count0 by 1
\draw [brace] ($(\the\count1.north east) + (0, -\distance)$) --
    ($(\the\count1.north east) + (0, -2\distance)$) node [caption] (\the\count0) 
    {longer text 50\%};

\end{tikzpicture}
\end{document}

印刷:

在此輸入影像描述

可以有任意數量的大括號:

  1. 2 - 50%(在此範例中)
  2. 3 - 33%
  3. 4 - 25%

ETC。

  1. 如何在較長文字右側 1mm 處建立一條水平線,位於圖片高度的中心:

在此輸入影像描述

2.如您所見,這些命令看起來很相似:

%1
\advance\count0 by 1
\draw [brace] (\the\count1.north east) -- 
    ($(\the\count1.north east) + (0, -\distance)$) node [caption] (\the\count0) {50\%};

%2
\advance\count0 by 1
\draw [brace] ($(\the\count1.north east) + (0, -\distance)$) --
    ($(\the\count1.north east) + (0, -2\distance)$) node [caption] (\the\count0) 
    {longer text 50\%};

是否可以將文字放入數組並創建一個循環,以創建這些大括號?

答案1

這是一個建議。

你可以使用類似的東西

\draw [-latex] (current bounding box.east) ++(1mm,0) -- +(2cm,0);

繪製箭頭。

TikZ 內建了對循環的支持,請參閱章節56 重複事情 在手冊中。可能有比下面的程式碼更好的方法,但它似乎有效。

\documentclass[12pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{positioning, calc, decorations.pathreplacing}
\begin{document}
\newlength{\distance}%
\setlength{\distance}{.6cm}%
\begin{tikzpicture}[node distance = 1cm, auto]
\tikzset{
    block/.style = {rectangle, draw, text centered},
    brace/.style = {decorate,decoration={brace,amplitude=4pt}},
    caption/.style = {black, midway, xshift = 1mm},
}

\pgfmathsetmacro\Nbraces{3}
\pgfmathtruncatemacro\Npercent{1/\Nbraces*100}


\node [block, minimum height=\Nbraces\distance] (mybox) {100\%};


\foreach [count=\i] \x  in {\Npercent\%,longer text \Npercent\%,\Npercent\% something else}
  \draw [brace]
    ($(mybox.north east) + {(\i-1)}*(0,-\distance)$) -- 
    ($(mybox.north east) + \i*(0,-\distance)$) node [caption] {\x};


\draw [-latex] (current bounding box.east) ++(1mm,0) -- +(2cm,0);

\end{tikzpicture}
\end{document}

相關內容