Tikz 包含图形和文本

Tikz 包含图形和文本

我有这些代码来绘制图形。但我没有找到另外添加文本的方法。当我写一些东西时,编译器总是给我图形。我想要一个解释性的文字,在这些文字下我想包含以下图画:

\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}

编辑: 我只想要这样的东西。使用简单的图像不会有问题,但我不知道如何包含 tikz 图形:

在此处输入图片描述

答案1

你的问题一点都不清楚。我怀疑你问的是,如何在文本中包含图片。除了提供的解决方案比基·特隆答案有一些好处:图像位于文本中插入的位置,但是,如果页面上插入的位置不够,就会出现漏洞。然后它会移到下一页,并在前一页上留下空白。为了解决这个问题,(La)TeX 有浮动功能,它试图通过图像浮动将图像放置在文本中的最佳位置。

例如,就你的情况而言:

\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}

生成:

在此处输入图片描述

使用上述任何一种解决方案都与包含图的复杂性无关。Tikz 代码可以直接在figure浮点环境中,也可以包含在内,例如

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

图表的唯一限制是其大小。生成的图像应小于一页的文本区域。

如果您想在图像中添加一些文字作为图例,您也可以通过以下方式进行:

...
\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}

这使:

在此处输入图片描述

一些文档类为这种情况提供了特殊环境,例如legend在包中memoir

笔记:

  • 我以更简洁的形式重写了给定的图表示例。在这里,我用 替换了过时的\tikzstyle符号tikzset

  • 我强烈建议你作为 LaTeX 新手阅读一些 LaTeX 介绍文本。例如LATEX 2ε 的简短介绍,或者其他一些。

答案2

\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}

在此处输入图片描述

相关内容