# Tikz include graphic and text

I have these code to draw a graph. But I did not find a way to include text additionally. When I write something the compiler always gives me the graphic. I want an explaining text and under these text I want to include the following drawing:

\documentclass[11pt]{article}
\usepackage[pdftex,active,tightpage]{preview}
\setlength\PreviewBorder{2mm}

\usepackage{tikz}
\usetikzlibrary{arrows,positioning, calc,lindenmayersystems,decorations.pathmorphing,intersections}
\tikzstyle{resource}= [draw,minimum size=16pt,inner sep=0pt]
\tikzstyle{process} = [draw,minimum size=16pt,inner sep=0pt,circle]
\tikzstyle{allocated} = [->,thick,arrows={-latex}]
\tikzstyle{requested} = [<-,thick,arrows={latex-}, dashed]

\begin{document}
\begin{preview}
\begin{tikzpicture}[scale=2,auto,swap]
\node (p1)[resource] at (1,0) {$P_1$};
\node (p2)[resource] at (1,1) {$P_2$};
\node (p3)[resource] at (1,2) {$P_3$};
\node (a)[process]  at (0,2) {$A$};
\node (b)[process]  at (2,2) {$B$};
\node (c)[process]  at (2,0) {$C$};
\node (d)[process] at (2,1) {$D$};

\draw[allocated] (c) -- (p1);
\draw[allocated] (a) -- (p3);
\draw[allocated] (p1) -- (a);
\draw[allocated] (p2) -- (c);
\draw[allocated] (p3) -- (b);
\draw[allocated] (d) -- (p2);

\end{tikzpicture}
\end{preview}

\end{document}


Edit: I just want something like this. With a simply image it would not be a problem but I don't know how to include tikz Graphics:

• You need to show what you have done - graphics and text, and then describe how the result differs from what you expect. – hpekristiansen Jun 29 '17 at 2:18
• Can you please show us where you want to print your explaining text, by simple drawing.@user1299292 – Biki Teron Jun 29 '17 at 3:26
• @BikiTeron He mentioned it in the question: above the drawing. – TeXnician Jun 29 '17 at 5:59
• I don't know what type of document you are writing, but the normal way would be to put the picture in a figure environment with a caption text (under the picture). Then in the normal text you write the description together with a cross reference to the caption number. – StefanH Jun 29 '17 at 6:28
• If you are trying to add text inside the tikzpicture environment, it will be ignored unless it is placed in a node, which I don't think it is what you want. Simply write your explanations outside of the tikzpicture (but put the picture and the explanation inside another environment to be "exported" by preview package) – JLDiaz Jun 29 '17 at 15:36

Your question is not clear at all. I suspect that you asking, how to include images in your text. Beside solution provided in Biki Teron answer which has some befits: image is is in place where is inserted in text, however, weeknes arise if on page, where is inserted, is not enough place. Then it is shifted to the next page and left empty space on previous one. To overcome this problem, (La)TeX has floats, by which it try to place image on the best way in the text with floating of image.

For example, in your case:

\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\tikzset{
base/.style = {draw, minimum size=16pt, inner sep=0pt},
resource/.style = {rectangle, base},
process/.style = {circle, base},
allocated/.style = {-latex, thick},
%requested/.style = {latex-, thick, dashed}
}% end of tikzset

\setlength\parindent{0pt}

\usepackage{lipsum}% for dummy text filler

\begin{document}
\lipsum[11]
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=2]
\node (p1) [resource] at (1,0) {$P_1$};
\node (p2) [resource] at (1,1) {$P_2$};
\node (p3) [resource] at (1,2) {$P_3$};
%
\node (a) [process]   at (0,2) {$A$};
\node (b) [process]   at (2,2) {$B$};
\node (c) [process]   at (2,0) {$C$};
\node (d) [process]   at (2,1) {$D$};
%
\draw[allocated]
(d)  edge (p2)
(p2) edge (c)
(c)  edge (p1)
(p1) edge (a)
(a)  edge (p3)
(p3) edge (b);
\end{tikzpicture}
\caption{my graph}
\label{fig:my graph}
\end{figure}
\lipsum[2]
\end{document}


which generate:

Using any of mentioned solution is independent from complexity included graph. Tikz code can be directly in figure float environment or can be included for example as

\input{path/<code-file-name>}


Only limitation of graph is its size. Generated image should be smaller than text area of one page.

In case, that you like to add some text as legend to image, this you can do also on the following way:

...
\begin{document}
\lipsum[1]
\begin{figure}[htb]
\centering
\begin{tikzpicture}[scale=2]
\node (p1) [resource] at (1,0) {$P_1$};
\node (p2) [resource] at (1,1) {$P_2$};
\node (p3) [resource] at (1,2) {$P_3$};
%
\node (a) [process]   at (0,2) {$A$};
\node (b) [process]   at (2,2) {$B$};
\node (c) [process]   at (2,0) {$C$};
\node (d) [process]   at (2,1) {$D$};
%
\draw[allocated]
(d)  edge (p2)
(p2) edge (c)
(c)  edge (p1)
(p1) edge (a)
(a)  edge (p3)
(p3) edge (b);
\end{tikzpicture}

\medskip
\begin{minipage}{\linewidth}% text in figure environment
\textbf{Legend:} \lipsum*[11]
\end{minipage}
\caption{my graph}
\label{fig:my graph}
\end{figure}
\lipsum[2]
\end{document}


which gives:

Some document classes provide for such cases special environments as legend in package memoir.

Note:

• I rewrote given example of graph in more concise form. In this I replace obsolete notation \tikzstyle with tikzset.

• I strongly encourage you as novice to LaTeX to read some of LaTeX introduction text. For example The Not So Short Introduction to LATEX 2ε, or some others.

\documentclass[11pt]{article}
\usepackage[pdftex,active,tightpage]{preview}
\usepackage{lipsum}
\setlength\PreviewBorder{2mm}

\usepackage{tikz}
\usetikzlibrary{arrows,positioning, calc,lindenmayersystems,decorations.pathmorphing,intersections,decorations.text}
\tikzstyle{resource}= [draw,minimum size=16pt,inner sep=0pt]
\tikzstyle{process} = [draw,minimum size=16pt,inner sep=0pt,circle]
\tikzstyle{allocated} = [->,thick,arrows={-latex}]
\tikzstyle{requested} = [<-,thick,arrows={latex-}, dashed]

\begin{document}
\begin{preview}
Random Text:\\
Letter wooded direct two men indeed income sister. Impression up admiration he by partiality is. Instantly immediate his saw one day perceive.

\begin{center}
\begin{tikzpicture}[scale=2,auto,swap]
\node (p1)[resource] at (1,0) {$P_1$};
\node (p2)[resource] at (1,1) {$P_2$};
\node (p3)[resource] at (1,2) {$P_3$};
\node (a) [process]  at (0,2) {$A$};
\node (b) [process]  at (2,2) {$B$};
\node (c) [process]  at (2,0) {$C$};
\node (d) [process]  at (2,1) {$D$};

\draw[allocated] (c)  --node[above]{text 1} (p1);
\draw[allocated] (a)  --node[above]{text 2} (p3);
\draw[allocated] (p1) --node[midway, above, sloped]{text 3} (a);
\draw[allocated] (p2) --node[midway, above, sloped]{text 4} (c);
\draw[allocated] (p3) --node[above]{text 5} (b);
\draw[allocated] (d)  --node[above]{text 6} (p2);

\end{tikzpicture}
\end{center}
Random Text:\\
Letter wooded direct two men indeed income sister. Impression up admiration he by partiality is. Instantly immediate his saw one day perceive.
\end{preview}

\end{document}


• I just want something like this. With a simply image it would not be a problem but I don't know how to include tkiz Graphics: [![enter image description here][1]][1] [1]: i.stack.imgur.com/r5O8m.png – user1299292 Jun 29 '17 at 10:54