5

I am new to latex and tikz and wanted to draw a block diagram using it. But I am having some issues. Can somebody help me with the few questions I have?

  1. How to write 'this text'so that it's in the middle of the filled box and not have boxes for text0 and text 6?

  2. How to write text between the boxes or on top of the arrow?

  3. Is there a way to control the size of individual boxes and have different arrow shape?

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepackage{tikz}

\usetikzlibrary{shapes,arrows,positioning,fit,backgrounds}
\tikzstyle{block} = [draw, rectangle,  align=center, text width=2.4cm, text centered, minimum height=1.2cm, node distance=3.5cm,fill=white]
\tikzstyle{container} = [draw, rectangle, inner sep=0.3cm, fill=gray,minimum height=3cm]

\def\bottom#1#2{\hbox{\vbox to #1{\vfill\hbox{#2}}}}
\tikzset{
  mybackground/.style={execute at end picture={
      \begin{scope}[on background layer]
        \node[] at (current bounding box.north){\bottom{1cm} #1};
        \end{scope}
    }},
}
\begin{document}


\resizebox{14cm}{3cm}{%
\begin{tikzpicture}[>=latex',mybackground={This text}]
    \node [block, name= text0] (text0){text0};
    \node [block, right of=text0](text1) {text1};
    \node [block, right of=text1] (text2) {text2};
    \node [block, right of=text2] (text3) {text3};
    \node [block, right of=text3] (text4) {text4};
    \node [block, right of=text4] (text5) {text5};
     \node [block, right of=text5] (text6) {text6};
    \begin{scope}[on background layer]
    \node [container,fit= (text3) (text4)] (container) {};
     \end{scope}
    \draw [->] (text0) -- (text1);
    \draw [->] (text1) -- (text2);
    \draw [->] (text2) -- node {}(text3);
    \draw [->] (text3) -- node {}(text4);
    \draw [->] (text4) -- node {} (text5);
    \draw [->] (text5) -- node {} (text6);

\end{tikzpicture}

}


\end{document}

enter image description here

5

hopefully the example below fulfill all your requests:

enter image description here

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                backgrounds,
                chains,
                fit,
                quotes}

\begin{document}
    \begin{tikzpicture}[auto,
    node distance = 15mm,
        start chain = A going right,
     block/.style = {draw, fill=white,
                     text width=#1, minimum height=12mm, align=center,
                     outer sep=0pt, on chain},
     block/.default = 18mm,
 container/.style = {draw, fill=gray!50,
                     inner xsep=2mm, inner ysep=7mm},
                        ]
\node   [block] {text 0};    % block name is A-1
\node   [block] {text 1};
\node   [block] {text 2};
\node   [block=22mm] {text 3};    % block name is A-4
\node   [block=11mm] {text 4};    % block name is A-5
\node   [block] {text 5};
\node   [block] {text 6};    % block name is A-7
%
\scoped[on background layer]
    \node [container, label={[anchor=north]This text},
           fit= (A-4) (A-5)] (container) {};
\draw [-Stealth]
    (A-1) edge ["text 1"] (A-2)    % text on arrow is between " and "
    (A-2) edge ["text 2"] (A-3)
    (A-3) edge ["text 3"] (A-4)
    (A-4) edge ["text 4"] (A-5)
    (A-5) edge ["text 5"] (A-6)
    (A-6) edge ["text 5"] (A-7);
    \end{tikzpicture}
\end{document}
  • since your block diagram is simple, all block are in chain, i suggest to use chains library for their positioning (assumed, that distances between are equal, however, if some of the are different, this can be changed with local use right=of <name previous block>
  • for text on arrows between bloc is simple to write by help of library quotes as (<node name i>) edge [" your text"] (<node name i+1>)
  • i not understand your first question well, so see if my guessing is correct
  • don't use \resizebox! it lead to bad looking of image. rather resize block, font and distances between blocks
  • with use of arrows.meta you can simply adjust arrow head size (length, width, angle, etc). for details see "tikz & pgf manual, v 3.0.1a", section "16.5 Reference: Arrow Tips", page 201. in mwe below i use default values, however you can experimenting with for example (see page 209 of mentioned manual):

\draw[-{Stealth[length=2mm,width=3mm,inset=0.5mm]}] ...

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.