在tikz
放置标签时 via\nodes
非常方便,并且经常使用。如何显示和定位外部图像而不是文本?
答案1
这里有一个方法。在运行此示例之前,请将 3 幅图像复制到与 .tex 文件相同的目录中,并将它们重命名为 img-1.X、img-2.X 和 img-3.X,其中X
表示图形格式png
,如jpg
等。
像往常一样放置并绘制一个节点,然后将\includegraphics[height=1cm]{img-1}
节点括号内的内容放在要放置文本的位置。
如果以后要连接节点,请命名它们。照常绘制连接,例如使用语句\graph
。
\documentclass[10pt, border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}% needed for the \graph - statement
% DO BEFOREHAND: copy and rename 3 images img-1 to img-3 to the same directory
\begin{document}
\tikz {
% --- that's all you need to place and show an image by a node ---
\node [draw=red] at (0,4) {\includegraphics[height=1cm]{img-1}};
% --- to connect nodes it's practical to name the nodes ---
\node (brd) [draw] at (0,0) {\includegraphics[height=2cm]{img-1}};
\node (stk) [draw] at (5,0) {\includegraphics[height=2cm]{img-3}};
\node (set) [draw] at (3.5, 3.5) {\includegraphics[height=2cm]{img-2}};
% --- let the sticks point towards two image-nodes ---
\graph {
(stk) -> [blue] {(brd), (set)}% reference aboves nodes by paranthesis ()
};
}
\end{document}
受到 David 的评论启发“\includegraphics
您可以在可以使用文本的任何地方使用“也可以将倾斜的图像附加到连接线上:
\documentclass[10pt, border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}% needed for the \graph - statement
% DO BEFOREHAND: copy and rename 3 images img-1 to img-3 to the same directory
\begin{document}
\tikz {
% --- to connect nodes it's practical to name the nodes ---
\node (brd) [draw] at (0,0) {\includegraphics[height=2cm]{img-3}};
\node (set) [draw] at (3.5, 3.5) {\includegraphics[height=2cm]{img-2}};
% --- connecting with a sloped text above ----
\draw (brd) -- node [sloped, above] {\small{label}} (set);
% --- connecting, with sloped image below and a curved connector ---
\draw [->] (brd) to [out=-30 , in=-100]
node [sloped, below] {\includegraphics[height=1cm]{img-1}} (set);
}
\end{document}
因为初学者tikz
有时会遇到困难,所以如果您愿意,可以反向应用 David 的建议:只采用经典的文本放置和样式,而不使用任何图像。
代码结构是从上一个示例中复制而来的,引入了文本的简写符号,提供了一些样式,放置了两个文本节点,提供了两个带有文本标签的连接器。
您的收获:tikz 中的结构仍然相同,只有处理的内容发生了变化(从图像到文本)。
\documentclass[10pt, border=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{graphs}% needed for the \graph - statement
% --- "text variables" to keep code cleaner -----------------
\def\stks{sticks}
\def\drst{Drummers set shown from Clyde Stubblefield and Yogi Horton}
\def\brd{drumset board}
\begin{document}
\tikz [dst/.style={text width=2cm, node font=\tiny},% styling the drums text
lbls/.style={node font=\small, color=blue}, sloped]% styling the label texts
{
% --- to connect by graphs it's practical to name the nodes ---
\node (stk) [draw] at (0,0) {\stks};% sticks, lower left
\node (set) [draw, dst] at (3.5, 3.5) {\drst};% upper right, styled
% --- connecting with a sloped and styled text above ----
\draw (stk) -- node [above, lbls] {label-text} (set);
% --- connecting, with sloped and styled text below and a curved connector ---
\draw [->] (stk) to [out=-30 , in=-100]% flexing the line
node [below, lbls] {\brd} (set);
}
\end{document}