Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Code:

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

Prints:

enter image description here

There can be arbitrary number of braces:

  1. 2 - 50% (in this example)
  2. 3 - 33%
  3. 4 - 25%

etc.

  1. How can I create a horizontal line 1mm to the right of the longer text, located in the center of the picture height:

enter image description here

2.As you can see, these commands look similar:

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

Is it possible to put the texts into an array and create a loop, that will create these braces?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Here is a suggestion.

You can use something like

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

to draw the arrow.

TikZ has built-in support for loops, see chapter 56 Repeating things in the manual. There are probably better ways of doing this than the code below, but it seems to work.

\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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