如何用 LaTeX 绘制平行六面体和立方体

如何用 LaTeX 绘制平行六面体和立方体

我怎样才能用 LaTeX 绘制这些立体图形?

在此处输入图片描述

\begin{tikzpicture}
\pgfmathsetmacro{\cubex}{5}
\pgfmathsetmacro{\cubey}{1}
\pgfmathsetmacro{\cubez}{3}
\draw[red,fill=yellow] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,-\cubey,0) -- ++(\cubex,0,0) -- cycle;
\draw[red,fill=yellow] (0,0,0) -- ++(0,0,-\cubez) -- ++(0,-\cubey,0) -- ++(0,0,\cubez) -- cycle;
\draw[red,fill=yellow] (0,0,0) -- ++(-\cubex,0,0) -- ++(0,0,-\cubez) -- ++(\cubex,0,0) -- cycle;
\end{tikzpicture}

我用过那个代码。我该如何绘制和写入引号和尺寸?

答案1

您可以使用多种方法来注释该图。以下是其中一种方法。

coordinate在构造长方体时放置 s。然后在后面的路径中将它们用作起点。其他坐标与原始坐标偏移(稍低、稍左、稍右……),然后用于绘制线条。使用操作绘制这些可以edge添加箭头提示作为每条线的起点和终点,而无需使用单独的路径,然后quotes使用库edge通过edge ["<label>"]语法标记这些 s。这些标签的选项设置auto为偏移它们。如果 TikZ 会“跳错方向”,则会'在带引号的标签后添加 以“交换”偏移量,例如向左而不是向右。

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{quotes,arrows.meta}
\begin{document}
\begin{tikzpicture}[every edge quotes/.append style={auto, text=blue}]
  \pgfmathsetmacro{\cubex}{5}
  \pgfmathsetmacro{\cubey}{1}
  \pgfmathsetmacro{\cubez}{3}
  \draw [draw=blue, every edge/.append style={draw=blue, densely dashed, opacity=.5}, fill=magenta]
    (0,0,0) coordinate (o) -- ++(-\cubex,0,0) coordinate (a) -- ++(0,-\cubey,0) coordinate (b) edge coordinate [pos=1] (g) ++(0,0,-\cubez)  -- ++(\cubex,0,0) coordinate (c) -- cycle
    (o) -- ++(0,0,-\cubez) coordinate (d) -- ++(0,-\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
    (o) -- (a) -- ++(0,0,-\cubez) coordinate (f) edge (g) -- (d) -- cycle;
  \path [every edge/.append style={draw=blue, |-|}]
    (b) +(0,-5pt) coordinate (b1) edge ["30cm"'] (b1 -| c)
    (b) +(-5pt,0) coordinate (b2) edge ["5cm"] (b2 |- a)
    (c) +(3.5pt,-3.5pt) coordinate (c2) edge ["10cm"'] ([xshift=3.5pt,yshift=-3.5pt]e)
    ;
\end{tikzpicture}
\end{document}

注释长方体

如果您需要在不同位置绘制一组或几组,定义一个pic可能是一个方便的解决方案。正如我所定义的,采用pic annotated cuboid一个可选参数,可以设置各种参数:width、、、(用于标签)和。要设置height颜色、填充等,您可以将depth可选参数传递给以指定例如。默认值是颜色等的当前默认值,一个 10x10x10 的长方体,缩放到自然大小的十分之一,单位标签以厘米为单位。unitsscale\picpic actions\pic [draw=red, fill=yellow, text=green] {annotated cuboid={width=10, height=45, depth=89.6, scale=.05, units=mm}};

例如:

\begin{tikzpicture}
  \pic {annotated cuboid};
  \pic [fill=magenta, text=blue, draw=blue] at (5,0) {annotated cuboid={width=30, height=5, depth=10}};
  \pic [fill=green, text=green!50!black, draw=green!25!black] at (5,-2) {annotated cuboid={width=6, height=20, depth=15, units=mm}};
  \pic at (1,-3) {annotated cuboid={width=150, height=200, depth=250, scale=.01, units=m}};
  \pic [fill=cyan, text=blue!75!cyan, draw=blue!75!cyan] at (-3,-2) {annotated cuboid={width=15, height=18, depth=13.5, units=}};
\end{tikzpicture}

生产

带注释的长方体

完整代码:

\documentclass[border=5pt, multi, tikz]{standalone}
\usetikzlibrary{quotes,arrows.meta}
\tikzset{
  annotated cuboid/.pic={
    \tikzset{%
      every edge quotes/.append style={midway, auto},
      /cuboid/.cd,
      #1
    }
    \draw [every edge/.append style={pic actions, densely dashed, opacity=.5}, pic actions]
    (0,0,0) coordinate (o) -- ++(-\cubescale*\cubex,0,0) coordinate (a) -- ++(0,-\cubescale*\cubey,0) coordinate (b) edge coordinate [pos=1] (g) ++(0,0,-\cubescale*\cubez)  -- ++(\cubescale*\cubex,0,0) coordinate (c) -- cycle
    (o) -- ++(0,0,-\cubescale*\cubez) coordinate (d) -- ++(0,-\cubescale*\cubey,0) coordinate (e) edge (g) -- (c) -- cycle
    (o) -- (a) -- ++(0,0,-\cubescale*\cubez) coordinate (f) edge (g) -- (d) -- cycle;
    \path [every edge/.append style={pic actions, |-|}]
    (b) +(0,-5pt) coordinate (b1) edge ["\cubex \cubeunits"'] (b1 -| c)
    (b) +(-5pt,0) coordinate (b2) edge ["\cubey \cubeunits"] (b2 |- a)
    (c) +(3.5pt,-3.5pt) coordinate (c2) edge ["\cubez \cubeunits"'] ([xshift=3.5pt,yshift=-3.5pt]e)
    ;
  },
  /cuboid/.search also={/tikz},
  /cuboid/.cd,
  width/.store in=\cubex,
  height/.store in=\cubey,
  depth/.store in=\cubez,
  units/.store in=\cubeunits,
  scale/.store in=\cubescale,
  width=10,
  height=10,
  depth=10,
  units=cm,
  scale=.1,
}
\begin{document}
\begin{tikzpicture}
  \pic {annotated cuboid};
  \pic [fill=magenta, text=blue, draw=blue] at (5,0) {annotated cuboid={width=30, height=5, depth=10}};
  \pic [fill=green, text=green!50!black, draw=green!25!black] at (5,-2) {annotated cuboid={width=6, height=20, depth=15, units=mm}};
  \pic at (1,-3) {annotated cuboid={width=150, height=200, depth=250, scale=.01, units=m}};
  \pic [fill=cyan, text=blue!75!cyan, draw=blue!75!cyan] at (-3,-2) {annotated cuboid={width=15, height=18, depth=13.5, units=}};
\end{tikzpicture}
\end{document}

相关内容