![我可以使用图片填充 Tikz 元素吗?怎样做?](https://linux22.com/image/453946/%E6%88%91%E5%8F%AF%E4%BB%A5%E4%BD%BF%E7%94%A8%E5%9B%BE%E7%89%87%E5%A1%AB%E5%85%85%20Tikz%20%E5%85%83%E7%B4%A0%E5%90%97%EF%BC%9F%E6%80%8E%E6%A0%B7%E5%81%9A%EF%BC%9F.png)
我想使用来自互联网或我的电脑的图片来填充立方体区域。我需要它具有木质纹理。我读过一些关于 tcolorbox 和 tikz 库的文章,但这对我没有帮助。
这是立方体:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\draw[fill=brown]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[fill=brown]
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[fill=brown]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}
答案1
您可以使用该fill.image
库:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fill.image}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\draw[fill stretch image=Ikcfv.png]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[fill stretch image=Ikcfv.png]
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[fill stretch image=Ikcfv.png]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}
答案2
将图片以某个名称(我使用了名称wood.jpg
)保存在某个文件夹中并复制路径。然后尝试此代码:
\documentclass{article}
\usepackage{tikz}
% paste the path to the picture e.g
\graphicspath{ {C:/images/tikz} }
\begin{document}
\begin{tikzpicture}[scale=2]
\tikzset{
wood/.style={%
draw=black!10,
path picture={%
\node at (path picture bounding box.center) {
\includegraphics[width=0.4\linewidth]{wood.jpg}};}}}
\pgfmathsetmacro{\cubex}{2}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{1}
\fill[wood]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\fill[wood]
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\fill[wood]
(0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw
(0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}
\end{document}
输出: