3

I am not sure how to create a axis with a labeling and some text above the line, aligned to my nodes. (for details see desired result below).

Desired result: (Ignore colors and size of nodes) enter image description here

Current approach: enter image description here

MWE:

\documentclass{scrreprt}
\usepackage{tikz}
    \usetikzlibrary{shapes,arrows,matrix,intersections,positioning}
    \tikzset{every picture/.style={/utils/exec={\sffamily}}}

\begin{document}
\begin{figure}[h]
\centering
\small
\begin{tikzpicture}[node distance = 0cm]
    % Style
    \tikzstyle{MP} = [rectangle, text width=1.5cm, minimum height=1.0cm, text centered, draw=black, fill=red!30]
    \tikzstyle{D} = [rectangle, text width=0.5cm, minimum height=1.0cm, text centered, draw=black, fill=orange!30]
    \tikzstyle{T} = [rectangle, text width=0.5cm, minimum height=1.0cm, text centered, draw=black, fill=blue!30]
    \tikzstyle{CYS} = [rectangle, text width=1.0cm, minimum height=1.0cm, text centered, draw=black, fill=green!30]
    \tikzstyle{S} = [rectangle, text width=1.5cm, minimum height=1.0cm, text centered, draw=black, fill=gray!30]
    \tikzstyle{CUB} = [rectangle, text width=0.8cm, minimum height=1.0cm, text centered, draw=black, fill=yellow!30]
    % Nodes
    \node (S1) [MP] {A};
    \node (S2) [D, right=of S1] {B};
    \node (S3) [T, right=of S2] {C}; 
    \node (S4) [CYS, right=of S3, xshift=0.2cm] {D}; 
    \node (S5) [S, right=of S4] {E}; 
    \node (S6) [T, right=of S5] {F1};  
    \node (S7) [T, right=of S6] {F2}; 
    \node (S8) [T, right=of S7] {F3}; 
    \node (S9) [T, right=of S8, xshift=0.4cm] {F4}; 
    \node (S10) [T, right=of S9] {F5}; 
    \node (S11) [T, right=of S10] {F6}; 
    \node (S12) [T, right=of S11] {F7}; 
    \node (S13) [CUB, right=of S12, xshift=0.6cm] {G}; 
    \node (S14) [CUB, right=of S13] {H}; 
    % Lines
    \draw [line width=0.1cm] (S3) -- (S4);
    \draw [line width=0.1cm] (S8) -- (S9);
    \draw [line width=0.1cm] (S12) -- (S13);
\end{tikzpicture}
\end{figure}
\end{document}
6

Here is one approach, using perpendicular coordinates (see TikZ: What EXACTLY does the the |- notation for arrows do?) to draw the ticks.

Note also that \tikzstyle is considered deprecated, so I moved the style definitions into the optional argument to the tikzpicture. I also defined a base style that all the other styles inherit, so that there is less repetition of code.

And just to show an alternative approach, I used a chain to position the nodes, but you don't have to use that of course.

(Some of the numbers were somewhat randomly chosen and placed, so you need to fix that yourself, or clarify what goes where. I did increase the gaps in the line in order to make the numbers fit next to each other, which in hindsight was perhaps a silly thing to do, but let me know and I'll change back.)

enter image description here

\documentclass{scrreprt}
\usepackage{tikz}
\usetikzlibrary{chains}
\tikzset{every picture/.style={/utils/exec={\sffamily}}}

\begin{document}
\begin{figure}
\centering
\small
\begin{tikzpicture}[
  node distance = 0cm,
  % Style
  base/.style={rectangle,minimum height=1cm,text centered,draw=black,on chain,fill=#1},
  MP/.style={text width=1.2cm, base=red!30},
  D/.style={text width=0.5cm, base=orange!30},
  T/.style={text width=0.5cm, base=blue!30},
  CYS/.style={text width=1.0cm, base=green!30},
  S/.style={text width=1.2cm, base=gray!30},
  CUB/.style={text width=0.8cm, base=yellow!30},
  connection/.style={line width=0.1cm}
]

\begin{scope}[start chain=S]
    % Nodes
    \node [MP] {A};
    \node [D] {B};
    \node [T] {C}; 
    \node [CYS, xshift=0.6cm] {D}; 
    \node [S] {E}; 
    \node [T] {F1};  
    \node [T] {F2}; 
    \node [T] {F3}; 
    \node [T, xshift=0.6cm] {F4}; 
    \node [T] {F5}; 
    \node [T] {F6}; 
    \node [T] {F7}; 
    \node [CUB, xshift=0.6cm] {G}; 
    \node [CUB] {H}; 
\end{scope}

    % Lines
    \draw [connection] (S-3) -- (S-4);
    \draw [connection] (S-8) -- (S-9);
    \draw [connection] (S-12) -- (S-13);
    % extensions
    \draw [connection] (S-1.west) -- ++(-0.5,0);
    \draw [connection] (S-14.east) -- ++(0.5,0);


% define coordinates for start and end point of axis
\path (S-1.north west) ++(-0.5,1) coordinate (start)
      (S-14.north east) ++(0.5,1) coordinate (end);

% draw axis
\draw (start) -- (end) node[above left] {Some text};

% draw ticks
\foreach \Anchor/\Number in {%
  start/0,
  S-1.north east/100,
  S-2.north east/200,
  S-3.north east/300,
  S-4.north west/320,
  S-8.north east/620,
  S-9.north west/640,
  S-12.north east/720,
  S-13.north west/740,
  end/860}
  \draw (start -| \Anchor) -- ++(0,-5pt) node[below] {\Number};

\end{tikzpicture}
\end{figure}
\end{document}

This has the same method for making the axis, it just demonstrates a variation for how to create the boxes in the first place, with a style that sets both the name and the node contents of the nodes, to the same thing. (Which in general is probably not a good plan, but in this case it works well.)

\documentclass{scrreprt}
\usepackage{tikz}
\usetikzlibrary{chains}
\tikzset{every picture/.style={/utils/exec={\sffamily}}}

\begin{document}
\begin{figure}
\centering
\small
\begin{tikzpicture}[
  node distance = 0cm,
  % Styles
  nameandcontent/.style={
   name=#1, % sets name, i.e. \node [name=foo] .. instead of \node [...] (foo) 
   node contents={#1} % the node text
  },
  base/.style={rectangle,minimum height=1cm,text centered,draw=black,on chain,fill=#1},
  MP/.style={text width=1.2cm, base=red!30},
  D/.style={text width=0.5cm, base=orange!30},
  T/.style={text width=0.5cm, base=blue!30},
  CYS/.style={text width=1.0cm, base=green!30},
  S/.style={text width=1.2cm, base=gray!30},
  CUB/.style={text width=0.8cm, base=yellow!30},
  connection/.style={line width=0.1cm}
]

\begin{scope}[start chain]
    % Nodes
    % due to the use of node contents (in nameandcontent)
    % we don't need (in fact can't use) the braces with the node text
    % the parsing of the node ends after the closing ] of the node options
    \node [MP,  nameandcontent=A];
    \node [D,   nameandcontent=B];
    \node [T,   nameandcontent=C]; 
    \node [CYS, nameandcontent=D, xshift=0.6cm]; 
    \node [S,   nameandcontent=E]; 
    \node [T,   nameandcontent=F1];  
    \node [T,   nameandcontent=F2]; 
    \node [T,   nameandcontent=F3]; 
    \node [T,   nameandcontent=F4, xshift=0.6cm]; 
    \node [T,   nameandcontent=F5]; 
    \node [T,   nameandcontent=F6]; 
    \node [T,   nameandcontent=F7]; 
    \node [CUB, nameandcontent=G, xshift=0.6cm]; 
    \node [CUB, nameandcontent=H]; 
\end{scope}

    % Lines
    \draw [connection] (C) -- (D);
    \draw [connection] (F3) -- (F4);
    \draw [connection] (F7) -- (G);
    % extensions
    \draw [connection] (A.west) -- ++(-0.5,0);
    \draw [connection] (H.east) -- ++(0.5,0);


% define coordinates for start and end point of axis
\path (A.north west) ++(-0.5,1) coordinate (start)
      (H.north east) ++(0.5,1) coordinate (end);

% draw axis
\draw (start) -- (end) node[above left] {Some text};

% draw ticks and values
\foreach \Anchor/\Number in {%
  start/0,
  A.north east/100,
  B.north east/200,
  C.north east/300,
  D.north west/320,
  F3.north east/620,
  F4.north west/640,
  F7.north east/720,
  G.north west/740,
  end/860}
  \draw (start -| \Anchor) -- ++(0,-5pt) node[below] {\Number};

\end{tikzpicture}
\end{figure}
\end{document}
  • Yes, node chain is much more elegant! (+1) – user121799 Mar 10 '18 at 19:10
  • @marmot Thanks. (By the way, why the \gettikzxy in your answer?) – Torbjørn T. Mar 10 '18 at 19:11
  • This is because I was trying to cook up something that computes the coordinates rather than putting them by hand. Then I saw the "(Ignore colors and size of nodes) " by the OP and switched gears, but I decided to keep it in case the OP changes her/his mind. – user121799 Mar 10 '18 at 19:14
  • @marmot OK, it seemed out of place, as you don't use it in the answer at all. – Torbjørn T. Mar 10 '18 at 19:16
  • Yes, you're right, thanks, I removed it. (It would not have worked anyway because I missed to put \makeatletter.) Good catch! Thanks! – user121799 Mar 10 '18 at 19:21
3

It is all pretty straightforward except that some of the nodes are too close to each other such that I need your feedback on where to put the coordinates.

\documentclass{scrreprt}
\usepackage{tikz}
    \usetikzlibrary{shapes,arrows,matrix,intersections,positioning}
    \tikzset{every picture/.style={/utils/exec={\sffamily}}}

\begin{document}
\begin{figure}[h]
\centering
\small
\begin{tikzpicture}[node distance = 0cm]
    % Style
    \tikzstyle{MP} = [rectangle, text width=1.5cm, minimum height=1.0cm, text centered, draw=black, fill=red!30]
    \tikzstyle{D} = [rectangle, text width=0.5cm, minimum height=1.0cm, text centered, draw=black, fill=orange!30]
    \tikzstyle{T} = [rectangle, text width=0.5cm, minimum height=1.0cm, text centered, draw=black, fill=blue!30]
    \tikzstyle{CYS} = [rectangle, text width=1.0cm, minimum height=1.0cm, text centered, draw=black, fill=green!30]
    \tikzstyle{S} = [rectangle, text width=1.5cm, minimum height=1.0cm, text centered, draw=black, fill=gray!30]
    \tikzstyle{CUB} = [rectangle, text width=0.8cm, minimum height=1.0cm, text centered, draw=black, fill=yellow!30]
    % Nodes
    \node (S1) [MP] {A};
    \node (S2) [D, right=of S1] {B};
    \node (S3) [T, right=of S2] {C}; 
    \node (S4) [CYS, right=of S3, xshift=0.2cm] {D}; 
    \node (S5) [S, right=of S4] {E}; 
    \node (S6) [T, right=of S5] {F1};  
    \node (S7) [T, right=of S6] {F2}; 
    \node (S8) [T, right=of S7] {F3}; 
    \node (S9) [T, right=of S8, xshift=0.4cm] {F4}; 
    \node (S10) [T, right=of S9] {F5}; 
    \node (S11) [T, right=of S10] {F6}; 
    \node (S12) [T, right=of S11] {F7}; 
    \node (S13) [CUB, right=of S12, xshift=0.6cm] {G}; 
    \node (S14) [CUB, right=of S13] {H}; 
    % Lines
    \draw [line width=0.1cm] (S3) -- (S4);
    \draw [line width=0.1cm] (S8) -- (S9);
    \draw [line width=0.1cm] (S12) -- (S13);
    \coordinate[above=1cm of S1] (X);
    \coordinate[above=0.5cm of X] (Y);
    \draw [line width=0.1cm] (S1.west) -- ++(-0.5cm,0) coordinate(S0);
    \node (C0) at (X-|S0.west){0};
    \draw (C0) -- (C0|-Y);  
    \foreach \i in {1,...,3}
        {
        \pgfmathtruncatemacro{\myX}{\i*100}
        \node (C\i) at (X-|S\i.east){\myX};
        \draw (C\i) -- (C\i|-Y);
        }
    \foreach \i in {13,14}
        {
        \pgfmathtruncatemacro{\myX}{\i*100-660}
        \node (C\i) at (X-|S\i.west){\myX};
        \draw (C\i) -- (C\i|-Y);
        }
    \draw [line width=0.1cm] (S14.east) -- ++(0.5cm,0) coordinate(S15);
    \node (C15) at (X-|S15.east){860};
    \draw (C15) -- (C15|-Y);    
    \draw (C0|-Y) -- (C15|-Y) node[pos=0,anchor=south west]{some text}
    node[pos=1,anchor=south east]{some text};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Where should I place the labels for D and F?

3

as exercises and for fun ... small variation of Torbjørn T. (actually combination of my first attempt to answer, but i was also few minutes to late and my solution was very similar but not so good; i'm very impressed with concept of nodes naming and writing their contains).

differences in comparison to Torbjørn T. answer are in style definitions, writing of numbers on axis (above nodes) and use positioning library. even entire code is shorter.

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{chains,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 12mm and 0mm,
  % Styles
NaC/.style = {% Name and Content
   name=#1,   % sets name, i.e. \node [name=foo] .. instead of \node [...] (foo)
   node contents={#1} % the node text
            },
base/.style args = {#1:#2}{rectangle, minimum height=1cm ,draw=black,
               minimum width=#1,
               fill=#2,
               font=\small\sffamily,
               on chain},
  MP/.style = {base=12mm:red!30},
   D/.style = {base= 5mm:orange!30},
   T/.style = {base= 5mm:blue!30},
 CYS/.style = {base=10mm:green!30},
   S/.style = {base=12mm:gray!30},
 CUB/.style = {base= 8mm:yellow!30},
  connection/.style={line width=1mm},
every pin/.append style = {pin distance=4mm, font=\footnotesize\sffamily}
                    ]

\begin{scope}[start chain]
    % Nodes
    % due to the use of node contents (in nameandcontent)
    % we don't need (in fact can't use) the braces with the node text
    % the parsing of the node ends after the closing ] of the node options
    \node [MP,  NaC=A];
    \node [D,   NaC=B];
    \node [T,   NaC=C];
    \node [CYS, NaC=D, right=5mm of C];
    \node [S,   NaC=E];
    \node [T,   NaC=F1];
    \node [T,   NaC=F2];
    \node [T,   NaC=F3];
    \node [T,   NaC=F4, right=5mm of F3];
    \node [T,   NaC=F5];
    \node [T,   NaC=F6];
    \node [T,   NaC=F7];
    \node [CUB, NaC=G, right=5mm of F7];
    \node [CUB, NaC=H];
\end{scope}
% Lines
\coordinate[left =5mm of A.west] (in);
\coordinate[right=5mm of H.east] (out);
\draw [connection]  (in) -- (A)
                    (C)  -- (D)
                    (F3) -- (F4)
                    (F7) -- (G)
                    (H)  -- (out);
% define coordinates for start and end point of axis
\coordinate[above=of in |- A.north] (start);
\coordinate[left =of start -| out ] (end);
% draw axis
\draw   (start) node[above right] {Some text} --
        (end)   node[above left] {Some text};
% draw ticks and values
\foreach \PIN/\Num in {%
  start/0,
  A.east/100,
  B.east/200,
  C.east/300,
  D.west/320,
  F3.east/620,
  F4.west/640,
  F7.east/720,
  G.west/740,
  end/860}
\coordinate[left=of start -| \PIN,
            pin=below:\Num] (aux);
    \end{tikzpicture}
\end{document}

enter image description here

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.