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.

I want to know how can I draw the line that appears in the right of the image below, and also how can I write this array that appears in the left of the picture.

tree

share|improve this question
3  
Can you add a minimal working example (MWE)? TikZ? PSTricks? What did you achieve so far? Where are your problems? –  Qrrbrbirlbel Dec 17 '12 at 20:04
    
edited @Qrrbrbirlbel –  Ahmed Mojaz Dec 17 '12 at 20:55
    
Welcome to TeX.sx! On this site, a question should typically revolve around an abstract issue (e.g. "How do I get a double horizontal line in a table?") rather than a concrete application (e.g. "How do I make this table?"). Questions that look like "Please do this complicated thing for me" tend to get closed because they are "too localized". Please try to make your question clear and simple by giving a minimal working example (MWE): you'll stand a greater chance of getting help. –  Martin Schröder Dec 17 '12 at 20:58
    
Related for the |-| papth: Vertical and horizontal lines in pgf-tikz –  Qrrbrbirlbel Apr 23 '13 at 1:04
add comment

1 Answer

up vote 8 down vote accepted

These are essentially two questions:

  1. The array.

    Use the array environment with a column specification of, for example, {c|c}.

    I wrapped this in a new environment sarray that adds the brackets (\left[ and \right]) and includes no additional \arraycolsep at the left and right side so that the brackets are tight. It takes one argument, the column specification, i.e. c|c.

    Another environment is Sarray that takes a count of columns, 6 becomes c|c|c|c|c|c. The optional argument can be used to change the columns from c to any column type (i.e. \begin{Sarray}[r]{5}.

    For the environments sarray and Sarray the column separator length is locally halved as this looks better in my eyes.

    Some examples can be found in the MWE below.

  2. The lines. I’d use Tik Z (→ ) for this.

    I provide the additional half- style for the to path operator. This style is a little bit optimized as it uses automatically .south for the start and .north for the target, as long as there’s neither . (node with anchor) or , (coordinate) in the start and target name.

Code

\documentclass[tikz]{standalone}% loads tikz automatically, 
                                % in any normal class you'll need \usepackage{tikz}
\usetikzlibrary{
    positioning,                % for left below=of
    shapes.arrows               % for the arrow
}
\newenvironment{sarray}[1]{     % #1 = column specifications, e.g. c|c|c
    \begingroup
    \setlength{\arraycolsep}{.5\arraycolsep}
    \left[
    \begin{array}{@{}#1@{}}
}{
    \end{array}
    \right]
    \endgroup
}
\newenvironment{Sarray}[2][c]{  % #1 = (optional, default = c) column specification
                                % #2 = number of columns
    \begin{sarray}{#1*{\numexpr#2-1\relax}{|#1}}
}{
    \end{sarray}
}
\makeatletter
\def\qrr@pgfutil@add@anchor#1#2{% ... if node without anchor
    \qrr@pgfutil@in@,{#1}% -> coordinate
    \ifpgfutil@in@\else
        \qrr@pgfutil@in@.{#1}% -> already node with anchor
        \ifpgfutil@in@\else
            \edef#1{#1#2}\fi\fi}
\def\qrr@pgfutil@in@#1#2{% to save \expandafters for #2
    \expandafter\pgfutil@in@\expandafter#1\expandafter{#2}}

\tikzset{
  half-/.style={% this style automatically uses .south (for the start)
                %                           and .north (for the target node)
                % if no anchor is specified
    to path={
      \pgfextra
        \qrr@pgfutil@add@anchor{\tikztostart}{.south}%
        \qrr@pgfutil@add@anchor{\tikztotarget}{.north}%
        \tikz@scan@one@point\pgfutil@firstofone(\tikztostart)\relax
        \pgf@xa\pgf@x\pgf@ya0.5\pgf@y
        \tikz@scan@one@point\pgfutil@firstofone(\tikztotarget)\relax
        \advance\pgf@ya0.5\pgf@y
      \endpgfextra
      (\tikztostart) -- (\pgf@xa, \pgf@ya) -| (\tikztotarget) \tikztonodes
    }
  }
}
\makeatother
\begin{document}
\begin{tikzpicture}
\node (nSA1) {$\begin{sarray}{c|c}
                   0, 1, 2, 4, 5, 6, 7 & 3 \\
                   0, 1, 2, 4, 5, 6, 7 & 3
               \end{sarray}$};

\node[
    below left=1cm and -1cm of nSA1
  ] (nSA2) {$\begin{Sarray}{4}% or \begin{sarray}{c|c|c|c}
                  0, 1, 2, 6 & 5, 7 & 4 & 3 \\
                  0, 1, 2, 6 & 5, 7 & 4 & 3
              \end{Sarray}$};

\node[
    below left=of nSA2
  ] (nSA3) {$\begin{Sarray}{6}% or \begin{sarray}{c*5{|c}}
                 0, 1, 2 & 6 & 7 & 5 & 4 & 3 \\
                 0, 1, 2 & 6 & 7 & 5 & 4 & 3
             \end{Sarray}$};

\node[% this is the arrow
    left=.5cm of nSA2,
    draw,
    single arrow,
    minimum height=1cm
  ] {};

\draw[very thick] (nSA1) to[half-] node[pos=.75,left] {$4 \to 4$} (nSA2)
                  (nSA2) to[half-] node[pos=.75,left] {$5 \to 5$} (nSA3);

\draw (-4,0) to[half-] +(-1,-1);% works with normal coordinates, too.
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
    
thanks a lot, well done –  Ahmed Mojaz Dec 18 '12 at 18:02
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.