如何用 tikz 创建特定图片

如何用 tikz 创建特定图片

我想使用 TikZ 或任何其他矢量图形包创建所附图片。但是,我不知道如何绘制箭头和对齐文本。

非常感谢您的帮助:)

我想用 Latex 创建的图片

答案1

这应该可以帮助你入门。

我得到了意想不到的结果[text height][minimum height]因此我将两个字段都放入了保存箱中。

需要注意的是,对于矩形,可以简单地使用\node[draw]。此外,计算多个节点的宽度时不要忘记考虑inner sep=.333em(默认)。

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\newsavebox{\boxA}
\newsavebox{\boxB}

\begin{document}

\savebox{\boxA}{\begin{tabular}{@{}lr@{}}
\multicolumn{2}{c}{Recycling}\\
\multicolumn{2}{c}{25.0 Mil Mg}\\
Bio waste & 3.8\\
Garden Residues & 3.9\\
Glass & 3.6\\
Paper & 7.9\\
Plastics & 4.6
\end{tabular}}%

\savebox{\boxB}{\parbox[c][\dimexpr \ht\boxA+\dp\boxA][t]{1.5cm}%
{\centering\strut Landfill 4.0\\ Mil Mg}}%

\begin{tikzpicture}
\node (A) {\usebox{\boxA}};
\draw (A.north west) -- (A.south west) -- ($(A.south)+(0,-1)$) -- (A.south east) -- (A.north east) -- cycle;
\node (B) [right] at (A.east) {\usebox{\boxB}};
\draw (B.north west) -- (B.south west) -- ($(B.south)+(0,-1)$) -- (B.south east) -- (B.north east) -- cycle;
\end{tikzpicture}

\end{document}

演示

答案2

没有什么好的理由使用这种方法。我发布它是为了满足好奇心。也就是说,这是一个用棺材做的实验。

代码之所以这么长,是因为据我所知,没有内置的方法可以在棺材周围或内部绘制规则,利用它们的手柄、对齐方式等等。

我认为结果还不错,但是代码过多了。

棺材废料

\documentclass{article}
\usepackage{xcoffins,tikz}
\usetikzlibrary{tikzmark,calc}
\ExplSyntaxOn
\dim_new:N \l_waste_rule_dim
\dim_set:Nn \l_waste_rule_dim { .4pt }
\tl_new:N \l_waste_colour_tl
\tl_set:Nn \l_waste_colour_tl { black }
\tl_new:N \l_waste_tmpa_tl
\tl_new:N \l_waste_tmpb_tl
\tl_new:N \l_waste_tmpc_tl
\tl_new:N \l_waste_tmpd_tl
\cs_new_protected:Nn \coffin_attach_rule:Nnnnnnnnn
{
  \vcoffin_set:Nnn \l_tmpa_coffin { #8 }
  {
    \dim_zero:N \parindent
    \color { \l_waste_colour_tl }
    \rule { #8 } { #9 }
  }
  \coffin_attach:NnnNnnnn #1 { #2 } { #3 } \l_tmpa_coffin { #4 } { #5 } { #6 } { #7 }
}
\cs_new_protected:Nn \coffin_join_rule:Nnnnnnnnn
{
  \vcoffin_set:Nnn \l_tmpa_coffin { #8 }
  {
    \dim_zero:N \parindent
    \color { \l_waste_colour_tl }
    \rule { #8 } { #9 }
  }
  \coffin_join:NnnNnnnn #1 { #2 } { #3 } \l_tmpa_coffin { #4 } { #5 } { #6 } { #7 }
}
\cs_generate_variant:Nn \coffin_attach_rule:Nnnnnnnnn { NVVVVVVVn , NVVVVVVnV }
\cs_generate_variant:Nn \coffin_join_rule:Nnnnnnnnn { NVVVVVVVn , NVVVVVVnV }
\NewDocumentCommand \CoffinRuleH { s m O { b , l } o D () { 0pt, 0pt } o O { \l_waste_rule_dim } o }
{
  \IfValueT { #8 }
  {
    \tl_set:Nn \l_waste_colour_tl { #8 }
  }
  \IfNoValueTF { #6 }
  {
    \dim_set:Nn \l_tmpa_dim { \coffin_wd:N #2 }
  }
  {
    \dim_set:Nn \l_tmpa_dim { #6 }
  }
  \clist_set:Nn \l_tmpa_clist { #3 }
  \clist_pop:NN \l_tmpa_clist \l_waste_tmpa_tl
  \clist_pop:NN \l_tmpa_clist \l_waste_tmpb_tl
  \IfNoValueTF { #4 }
  {
    \tl_set:NV \l_waste_tmpc_tl \l_waste_tmpa_tl
    \tl_set:NV \l_waste_tmpd_tl \l_waste_tmpb_tl
  }
  {
    \clist_set:Nn \l_tmpa_clist { #3 }
    \clist_pop:NN \l_tmpa_clist \l_waste_tmpc_tl
    \clist_pop:NN \l_tmpa_clist \l_waste_tmpd_tl
  }
  \clist_set:Nn \l_tmpa_clist { #5 }
  \clist_pop:NN \l_tmpa_clist \l_tmpb_dim
  \clist_pop:NN \l_tmpa_clist \l_tmpc_dim
  \IfBooleanTF { #1 }
  {
    \coffin_join_rule:NVVVVVVVn #2 \l_waste_tmpa_tl \l_waste_tmpb_tl \l_waste_tmpc_tl \l_waste_tmpd_tl \l_tmpb_dim \l_tmpc_dim \l_tmpa_dim { #7 }
  }
  {
    \coffin_attach_rule:NVVVVVVVn #2 \l_waste_tmpa_tl \l_waste_tmpb_tl \l_waste_tmpc_tl \l_waste_tmpd_tl \l_tmpb_dim \l_tmpc_dim \l_tmpa_dim { #7 }
  }
}
\NewDocumentCommand \CoffinRuleV { s m O { b , l } o D () { 0pt, 0pt } o O { \l_waste_rule_dim } o }
{
  \IfValueT { #8 }
  {
    \tl_set:Nn \l_waste_colour_tl { #8 }
  }
  \IfNoValueTF { #6 }
  {
    \dim_set:Nn \l_tmpa_dim { \coffin_ht:N #2 + \coffin_dp:N #2 }
  }
  {
    \dim_set:Nn \l_tmpa_dim { #6 }
  }
  \clist_set:Nn \l_tmpa_clist { #3 }
  \clist_pop:NN \l_tmpa_clist \l_waste_tmpa_tl
  \clist_pop:NN \l_tmpa_clist \l_waste_tmpb_tl
  \IfNoValueTF { #4 }
  {
    \tl_set:NV \l_waste_tmpc_tl \l_waste_tmpa_tl
    \tl_set:NV \l_waste_tmpd_tl \l_waste_tmpb_tl
  }
  {
    \clist_set:Nn \l_tmpa_clist { #3 }
    \clist_pop:NN \l_tmpa_clist \l_waste_tmpc_tl
    \clist_pop:NN \l_tmpa_clist \l_waste_tmpd_tl
  }
  \clist_set:Nn \l_tmpa_clist { #5 }
  \clist_pop:NN \l_tmpa_clist \l_tmpb_dim
  \clist_pop:NN \l_tmpa_clist \l_tmpc_dim
  \IfBooleanTF { #1 }
  {
    \coffin_join_rule:NVVVVVVnV #2 \l_waste_tmpa_tl \l_waste_tmpb_tl \l_waste_tmpc_tl \l_waste_tmpd_tl \l_tmpb_dim \l_tmpc_dim { #7 } \l_tmpa_dim
  }
  {
    \coffin_attach_rule:NVVVVVVnV #2 \l_waste_tmpa_tl \l_waste_tmpb_tl \l_waste_tmpc_tl \l_waste_tmpd_tl \l_tmpb_dim \l_tmpc_dim { #7 } \l_tmpa_dim
  }
}
\ExplSyntaxOff
\newlength\mytotalwidth
\setlength\mytotalwidth{\linewidth}
\NewCoffin\maincoffin
\NewCoffin\nextcoffin
\NewCoffin\holdcoffin
\begin{document}
\SetVerticalCoffin\maincoffin{.6\mytotalwidth}
{%
  \centering Municipal Solid Waste\par 46.5 Mil.\ Mg
}
\CoffinRuleH*\maincoffin[t,hc](.1\mytotalwidth,\baselineskip)[\mytotalwidth]
\CoffinRuleH*\maincoffin[b,hc](0pt,-\baselineskip)[\mytotalwidth]
\SetVerticalCoffin\holdcoffin{.4\mytotalwidth}
{%
  \centering Household waste separate collection
}
\SetVerticalCoffin\nextcoffin{.6\mytotalwidth}
{%
  \centering Mixed household waste,\\household waste similar\\to commercial waste,\\bulky waste\\21.2 Mil.\ Mg
}
\CoffinRuleV\nextcoffin[b,l](-.2pt,-\baselineskip)[\CoffinTotalHeight\nextcoffin+2\baselineskip]
\JoinCoffins\holdcoffin [vc,r] \nextcoffin [vc,l]
\JoinCoffins\maincoffin[B,hc]\holdcoffin[t,hc](0pt,-\baselineskip)
\CoffinRuleH*\maincoffin[b,hc](0pt,-\baselineskip)
\SetVerticalCoffin\holdcoffin{.4\mytotalwidth}
{%
  \centering Recycling \par 25.0 Mil.\ Mg \par
  \begin{tabular}{lr}
    Bio waste & 3.8\\
    Garden Residues & 3.9\\
    Glass & 3.6\\
    Paper & 7.9\\
    Plastics & 4.6
  \end{tabular}%
}
\CoffinRuleV\holdcoffin[b,r](.2pt,-\baselineskip)[\CoffinTotalHeight\holdcoffin+2\baselineskip]
\SetVerticalCoffin\nextcoffin{.175\mytotalwidth}
{%
  \centering Landfill \par 4.0 Mil.\ Mg
}
\JoinCoffins \holdcoffin [t,r] \nextcoffin [t,l]
\CoffinRuleV\holdcoffin[b,r](.2pt,-\baselineskip)[\CoffinTotalHeight\holdcoffin+2\baselineskip]
\SetVerticalCoffin\nextcoffin{.175\mytotalwidth}
{%
  \centering Treatment \par 4.2 Mil.\ Mg
}
\JoinCoffins \holdcoffin [t,r] \nextcoffin [t,l]
\CoffinRuleV\holdcoffin[b,r](.2pt,-\baselineskip)[\CoffinTotalHeight\holdcoffin+2\baselineskip]
\SetVerticalCoffin\nextcoffin{.25\mytotalwidth}
{%
  \centering Waste incineration\par 12.8 Mil.\ Mg\
}
\JoinCoffins \holdcoffin [t,r] \nextcoffin [t,l]

\JoinCoffins \maincoffin [b,l] \holdcoffin [t,l](0pt,-\baselineskip)

\CoffinRuleH*\maincoffin[b,hc](0pt,-\baselineskip)[\mytotalwidth][0pt]
\CoffinRuleV\maincoffin[b,l]
\CoffinRuleV\maincoffin[b,r]

\noindent\TypesetCoffin\maincoffin

\noindent\tikzmark{a}

\begin{tikzpicture}[remember picture,overlay]
  \coordinate (a) at ({pic cs:a});
  \path (a) ++(.4\mytotalwidth,0) coordinate (b) ++(.175\mytotalwidth,0) coordinate (c)  ++(.175\mytotalwidth,0) coordinate (d)  ++(.25\mytotalwidth,0) coordinate (e);
  \foreach \i/\j/\k in {a/b/.2,b/c/.0875,c/d/.0875,d/e/.125}
  \draw (\i) --  ($(\j)!.5!(\i) + (0,-\k*\mytotalwidth)$) -- (\j);
\end{tikzpicture}

\end{document}

相关内容