4

I am trying to do a array diagram with Tikz. So far, my results looks like this:

As you can see, I have two problems: The boxes are proportional to the content of the node and I do not have a overall box size.(Take the last three boxes as example) Furthermore, the text "k-elements" is between the array diagram and brace. It should be below brace.

Code:

\begin{tikzpicture}
  [
  -{Stealth[length=2.5pt]},
MyStyle/.style={draw, text width=25pt, text height=10pt, text centered},
my arrow/.style={shape=single arrow, rotate=90, inner sep=5pt, outer sep=0pt, single arrow head extend=0pt, minimum height=7.5pt, text width=0pt, draw=blue!50, fill=blue!25}
  ]
\begin{scope} [start chain, node distance=0pt]
\node [MyStyle,on chain] (1) {$1$};
\node [MyStyle,on chain] (2) {$2$};
\node [MyStyle,on chain] (3) {$3$};
\node [MyStyle,on chain] (4) {$4$};
\node [MyStyle,on chain] (5) {$\cdots$};
\node [MyStyle,on chain] (6) {$k$};
\node [MyStyle,on chain] (7) {$\cdots$};
\node [MyStyle,on chain] (8) {$n-2$};
\node [MyStyle,on chain] (9) {$n-1$};
\node [MyStyle,on chain] (10) {$n$};
\end{scope}
  \draw (1.north) [out=25, in=155] to (2.north);
  \draw (1.north) [out=30, in=155] to (3.north);
  \draw (1.north) [out=35, in=155] to (4.north);
  \draw (1.north) [out=40, in=155] to (6.north);
  \draw [decorate,decoration={brace,amplitude=10pt,mirror},xshift=-4pt,yshift=0pt]
  (0.7,-0.27) -- (6.6,-0.27)node[black,midway,xshift=9pt] {$k$-elements} ;
\end{tikzpicture}

(code is based on Array with arrows and a cell outside and Draw Curly Braces in TikZ )

And a other questions: Is there a good introduction to Tikz? Or is the Documentation the best starting point?

Thank you guys for the help!

3

Testing new editor for LaTeX I was to late with my answer. It is almost the same as from @ferahfeza. The tiny differences are in definition of arrows and brace:

\documentclass[tikz,margin=10pt]{standalone}
    \usetikzlibrary{arrows.meta,chains,%
                    decorations.pathreplacing}

    \begin{document}
\begin{tikzpicture}[
%  -{Stealth[length = 2.5pt]},
       start chain = going right,
     node distance = 0pt,
MyStyle/.style={draw, minimum width=2em, minimum height=2em, 
                outer sep=0pt, on chain},
  ]
\node [MyStyle] (1) {$1$};
\node [MyStyle] (2) {$2$};
\node [MyStyle] (3) {$3$};
\node [MyStyle] (4) {$4$};
\node [MyStyle] (5) {$\cdots$};
\node [MyStyle] (6) {$k$};
\node [MyStyle] (7) {$\cdots$};
\node [MyStyle] (8) {$n-2$};
\node [MyStyle] (9) {$n-1$};
\node [MyStyle] (10) {$n$};
\begin{scope}[-{Stealth[length = 2.5pt]}]
  \draw (1.north) [out=25, in=155] to (2.north);
  \draw (1.north) [out=30, in=155] to (3.north);
  \draw (1.north) [out=35, in=155] to (4.north);
  \draw (1.north) [out=40, in=155] to (6.north);
\end{scope}
\draw[decorate,decoration={brace, amplitude=10pt, raise=5pt, mirror}]
  (2.south west) to node[black,midway,below= 15pt] {$k$-elements} (7.south east);%
\end{tikzpicture}
    \end{document}

A naming of nodes with numbers is not good idea, in some circumstances can cause unexpected errors.

enter image description here

For learning TikZ read chapter "TikZ is kein Zeichenprogramm" in TikZ manual. There is some sort summary of (very lengthy) TikZ manual. Informative are also examples provided in http://www.texample.net/

  • Just with minimum height=2em, the base lines of the nodes are not aligned : try a node with $n^2$ or $n_1$. You may add text height=1em, text depth=.2em. – Paul Gaborit May 16 '15 at 13:47
  • @Paul Gaborit, you have right. I consider only to obtain of equal height of shapes, not position of text in them. – Zarko May 16 '15 at 14:33
4

For this kind of schemes a matrix can help. At least it saves you some typing.

\documentclass[tikz,margin=10pt]{standalone}
\usetikzlibrary{arrows.meta,matrix,%
                    decorations.pathreplacing}

\begin{document}
\begin{tikzpicture}[
    MyStyle/.style={draw, minimum width=2em, minimum height=2em, 
                outer sep=0pt},
  ]

\matrix (A) [matrix of math nodes, nodes={MyStyle, anchor=center}, column sep=-\pgflinewidth]
{1 & 2 & 3 & 4 & \cdots & k & \cdots & n-2 & n-1 & n\\};
\begin{scope}[-{Stealth[length = 2.5pt]}]
  \draw (A-1-1.north) [out=25, in=155] to (A-1-2.north);
  \draw (A-1-1.north) [out=30, in=155] to (A-1-3.north);
  \draw (A-1-1.north) [out=35, in=155] to (A-1-4.north);
  \draw (A-1-1.north) [out=40, in=155] to (A-1-5.north);
\end{scope}
\draw[decorate,decoration={brace, amplitude=10pt, raise=5pt, mirror}]
  (A-1-1.south west) to node[black,midway,below= 15pt] {$k$-elements} (A-1-6.south east);%
\end{tikzpicture}
\end{document}

enter image description here

1

Adding a slightly different solution here. Instead of setting the nodes size manually, you can align them to the nodes with the biggest content. You do this by adding the following to your node style.

minimum height=\heightof{$n-2$}+2*2*1mm)

You can also reduce your code using a foreach command. You can learn about it by reading the Tikz Manual (v 3.0.0).

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{graphics}

\usetikzlibrary{arrows.meta,decorations,decorations.pathreplacing,calc,bending,positioning, chains}


\tikzset{
    MyStyle/.style={draw, text width=25pt, text height=10pt, text centered,minimum height=\heightof{$n-2$}+2*2*1mm)},
    myarrow/.style={shape=single arrow, rotate=90, inner sep=5pt, outer sep=0pt, 
                    single arrow head extend=0pt, minimum height=7.5pt, text width=0pt, draw=blue!50, fill=blue!25}

}

\begin{document}
    \begin{tikzpicture}[
        -{Stealth[length=2.5pt]}
      ]
    \begin{scope} [start chain, node distance=-.5pt]  
      \foreach \name [count=\xi] in {1,2,3,4,\cdots,k,\cdots,n-2,n-1,n}{
        \node[MyStyle, on chain] (n\xi) {$\name$};
      }
     \end{scope}
    \draw (n1.north) [out=25, in=155] to (n2.north);
    \draw (n1.north) [out=30, in=155] to (n3.north);
    \draw (n1.north) [out=35, in=155] to (n4.north);
    \draw (n1.north) [out=40, in=155] to (n6.north);
    \draw [decorate,decoration={brace,amplitude=10pt,mirror}]
      (n1.south east) -- (n7.south west) node[black,midway,below=8pt] {$k$-elements};
    \end{tikzpicture}   
\end{document}

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.