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 have the following code which draws a tikzpicture.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}

\usepgflibrary{shapes.arrows}

\tikzstyle{every node}=[align=center,text=black, ultra thick]


% All the Labels on the Edges
\draw (11,-0.65) node {$\frac{1}{4}.x$};
\draw (11,-1.35) node {(1)};

\draw (2.25,-0.65) node {4.x};
\draw (2.25,-1.35) node {(8)};


\draw (6.45,-7.75) node {1};
\draw (6.45,-8.35) node {(2)};


\draw (10.15,-5.5) node {$\frac{1}{4}.x$};
\draw (10.85,-5.5) node {(4)};

\draw (12.15,-4.75) node {$\frac{1}{x}$};
\draw (12.85,-4.75) node {(4)};

% All the Nodes
\draw (0,0) node[minimum height=3.5 cm,minimum width=2.4cm,draw,ultra thick, text width=2cm,font=\large] {A \\300.x};
\draw (4.5,0.5) node[minimum height=2cm,minimum width=1 cm,draw, ultra thick, text width=2cm,font=\large] {$Fetch_L$\\150};
\draw (4.5,-2.5) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$CINT_L$\\130};
\draw (8.5,-1.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$DINT_L$\\100};
\draw (0,-5.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$BINTS-Split$\\40};
\draw (8.5,-5.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$Md_C$\\200};
\draw (4.5,-8.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$IDg_C$\\260};
\draw (8.5,-8.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$USCA$\\40};
\draw (12.5,-6.5) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$C-Join$\\30};
\draw (12.5,-3.0) node[minimum height=2cm,minimum width=1 cm,draw,ultra thick, text width=2cm,font=\large] {$CMix$\\680.x};

% All the Arrows
\draw [->, ultra thick] (1.25,0.5)  -- (3.35,0.5);
\draw [->, ultra thick] (1.25,-0.95)  -- (4.35,-0.95) -- (4.35,-1.45);
\draw [->, ultra thick] (5.65,0.50)  -- (8.50,0.5) -- (8.5,0.05);
\draw [->, ultra thick] (5.65,-2.50)  -- (8.50,-2.5) -- (8.5,-2.05);
\draw [->, ultra thick] (9.65,-1)  -- (12.50,-1) -- (12.5,-2.0);
\draw [->, ultra thick] (0.0,-1.75)  -- (0.0,-4);
\draw [->, ultra thick] (1.15,-5)  -- (7.35,-5);
\draw [->, ultra thick] (5.65,-8)  -- (7.35,-8);
\draw [->, ultra thick] (0,-6)  -- (0,-8) -- (3.35,-8);
\draw [->, ultra thick] (8.65,-7)  -- (8.65,-6);
\draw [->, ultra thick] (9.65,-5)  -- (10.5,-5) -- (10.5,-6.5) -- (11.35, -6.5);
\draw [->, ultra thick] (12.5,-5.5)  -- (12.5,-4);

\end{tikzpicture}
\end{document}

The output looks like this:

output

My problem is that I don't want to hard code the values in order to make it easier to change some entries if desired. I tried to refer to the examples given in the gallery. However, the examples do not give me accurate placement of nodes like the one given in the output above. My question is thus: is it possible to not use the hard coded values and still obtain a output like this?

share|improve this question
    
You can place named nodes (\draw[<opt>] (<name>) at (<coordinate>) {<text>};) and then use (<name>) to refer to the nodes. You can even use that name for those lines. Take a look at the -|/|- path-operators. Additionally, you can use custom styles to have a short-cut for often-used styles (text width, minimum sizes, etc.) For placement, take a look at the positioning and the matrix library/styles. –  Qrrbrbirlbel Jan 3 '13 at 17:33
    
You may also look up relative coordinates (+ and ++) and to path. On that node: This is a great question as it delves into many (basic) TikZ things, but it is a bad MWE as everyone who’s not you does not get a general idea what is connected with what and how. Can you shorten your code to a few simple examples? –  Qrrbrbirlbel Jan 3 '13 at 17:42
    
should i remove the bottom part of the graph to make less code? Actually I use QTikz to edit example which shows me immediately which part is what. –  Raj Jan 3 '13 at 17:45
add comment

1 Answer

up vote 1 down vote accepted

Styles

  • every node: you already used that
  • normal: for the majority of nodes
  • big: for the one big node
  • -|- is a to path that has to be used on like to[-|-] or edge[-|-].

Nodes

I have given a name to all big nodes that are referenced later in the lines.

It is also possible to specify nodes on the line (whether --, -|, to or edge). I have provided a few examples how that should work.

I have not changed the placement of the nodes, albeit this should be done in a less hard-coded way. Again, I refer to the positioning library. A few examples:

\node (A) {A};
\node[left=of A] {B};
\node[right=of A.north,anchor=north, minimum height=3cm] (C) {C};
\node[below=5cm of C] (D) {far away};

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\tikzset{
    every node/.style={
        align=center,
        text=black,
        ultra thick,
    },
    normal/.style={
        every node,
        minimum height=2cm,
        minimum width=1cm,
        draw,
        text width=2cm,
        font=\large,
    },
    big/.style={
        normal,
        minimum height=3.5cm,
        minimum width=2.4cm,
    },
    -|-/.style={
        to path={
            let \p1=(\tikztostart),
                \p2=(\tikztotarget),
                \n1={(\x1+\x2)/2}
             in
            (\tikztostart) -- (\n1,\y1) -- (\n1,\y2) \tikztonodes -- (\tikztotarget)
        }
    },
}

\begin{document}
\begin{tikzpicture}
\node[big]    (A)      at (0,0)      {A \\300.x};
\node[normal] (Fetch)  at (4.5,0.5)  {$Fetch_L$\\150};
\node[normal] (CINT)   at (4.5,-2.5) {$CINT_L$\\130};
\node[normal] (DINT)   at (8.5,-1.0) {$DINT_L$\\100};
\node[normal] (BINTS)  at (0,-5.0)   {$BINTS-Split$\\40};
\node[normal] (FStar)  at (4.5,-5.0) {$FStar_C$\\300};
\node[normal] (Md)     at (8.5,-5.0) {$Md_C$\\200};
\node[normal] (IDg)    at (4.5,-8.0) {$IDg_C$\\260};
\node[normal] (USCA)   at (8.5,-8.0) {$USCA$\\40};
\node[normal] (C-Join) at (12.5,-6.5){$C-Join$\\30};
\node[normal] (CMix)   at (12.5,-3.0){$CMix$\\680.x};

\begin{scope}[every path/.style={ultra thick, ->}]
\draw (A.east |- Fetch.west) -- node[above]{4.x} node[below] {(8)} (Fetch.west);
\draw (A.320) -| (CINT);
\draw (Fetch) -| (DINT);
\draw (CINT) -| (DINT);
\draw[pos=.25] (DINT) -| node[above]{$\frac{1}{4}.x$} node[below] {(1)} (CMix);
\draw (A)  -- (BINTS);
\draw (BINTS) edge (FStar)
      (FStar) edge (Md); % two on one
\draw (BINTS) |- (IDg);
\draw (IDg)  edge (USCA)
      (USCA) edge (Md);
\draw[] (Md) to[-|-] (C-Join);
\draw[] (C-Join)  -- (CMix);
\end{scope}
\end{tikzpicture}
\end{document}

Output

enter image description here

share|improve this answer
    
This is just great !! –  Raj Jan 3 '13 at 18:13
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.