Tikzset 多张图片

Tikzset 多张图片

尝试在一个 tikzset 中绘制两张图片。为什么它无法绘制 myBalance?

\documentclass[a4paper]{article}
\usepackage[margin=10mm]{geometry}
\usepackage {array, tikz, amssymb}

\def \myScale {0.3}

\tikzset{ 
myBag/.pic={
    \draw (-1,0) arc (180:360:1);
    \draw (1,0) arc (0:180:1 and 2);
    \draw [rounded corners=1 ] (0,2) -- (0.5,2.5) -- (-0.5,2.5) -- cycle;
 }
  
  myBalance/.pic={
    \draw (-3,0) -- (3,0);
    \draw[rounded corners=1] (0,0) -- (-0.2,-0.2) -- (0.2,-0.2) -- cycle;
  }
}

\centering

\begin{document}
\begin{center}
\begin{tabular}{ m{8cm}  m{0.5cm}  m{8cm} }
%\hline
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\

1) & & 2) \\

\begin{tikzpicture}
  \centering
  \pic [scale=\myScale] at (2,1) {myBag};
  \pic [scale=\myScale] at (-3,0) {myBag};
  \pic at (0,0) {myBalance};
\end{tikzpicture}

& & \\

\end{tabular}
\end{center}
\end{document}

答案1

另请参阅这里

\documentclass[a4paper]{article}
\usepackage[margin=10mm]{geometry}
\usepackage {array, tikz, amssymb}

\def \myScale {0.3}

\tikzset{ 
myBag/.pic={
    \draw (-1,0) arc (180:360:1);
    \draw (1,0) arc (0:180:1 and 2);
    \draw [rounded corners=1 ] (0,2) -- (0.5,2.5) -- (-0.5,2.5) -- cycle;} }
\tikzset{ 
myBalance/.pic={
    \draw (-3,0) -- (3,0);
    \draw[rounded corners=1] (0,0) -- (-0.2,-0.2) -- (0.2,-0.2) -- cycle;
  }
}

\centering

\begin{document}
\begin{center}
\begin{tabular}{ m{8cm}  m{0.5cm}  m{8cm} }
%\hline
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\

1) & & 2) \\

\begin{tikzpicture}
  \centering
  \pic [scale=\myScale] at (2,1) {myBag};
  \pic [scale=\myScale] at (-3,0) {myBag};
  \pic at (0,0) {myBalance};
\end{tikzpicture}

& & \\

\end{tabular}
\end{center}
\end{document}

在此处输入图片描述

答案2

您应该考虑在两个定义之间添加逗号pics

\documentclass[a4paper]{article}
% https://tex.stackexchange.com/questions/633850/tikzset-multiple-pic
\usepackage[margin=10mm]{geometry}
\usepackage {array, tikz, amssymb}

\def \myScale {0.3}

\tikzset{ 
myBag/.pic={
    \draw (-1,0) arc (180:360:1);
    \draw (1,0) arc (0:180:1 and 2);
    \draw [rounded corners=1 ] (0,2) -- (0.5,2.5) -- (-0.5,2.5) -- cycle;
 },   % <------------ Add a comma here!
  myBalance/.pic={
    \draw (-3,0) -- (3,0);
    \draw[rounded corners=1] (0,0) -- (-0.2,-0.2) -- (0.2,-0.2) -- cycle;
  }
}

\centering

\begin{document}
\begin{center}
\begin{tabular}{ m{8cm}  m{0.5cm}  m{8cm} }
%\hline
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\
 & & \\

1) & & 2) \\

\begin{tikzpicture}
  \centering
  \pic [scale=\myScale] at (2,1) {myBag};
  \pic [scale=\myScale] at (-3,0) {myBag};
  \pic at (0,0) {myBalance};
\end{tikzpicture}

& & \\

\end{tabular}
\end{center}
\end{document}

相关内容