Tikz 在一个环境中提供多个图形

Tikz 在一个环境中提供多个图形

我尝试在文档中放置多个图形。我尝试创建一个范围并移动图形,有没有更好的方法?

\documentclass{article}
\usepackage{scrtime, dsfont}
\usepackage{listings}
\usepackage{amsmath, amssymb}

\usepackage{tikz}
\usetikzlibrary{backgrounds,calc, decorations.pathreplacing,snakes}
\begin{document}  
\begin{tikzpicture}[scale=.5]
\pgfmathsetmacro{\A}{27}
\pgfmathsetmacro{\xn}{10}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-5cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-10cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-15cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-20cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-25cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-30cm]
\draw[red,fill=green] (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{scope}
\pgfmathsetmacro{\A}{27}
\pgfmathparse{((\cubex+\cubey) + (\A / (\cubex*\cubey)))/3}
\pgfmathsetmacro{\xn}{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathparse{\A / (\cubex * \cubey)}
\pgfmathsetmacro{\cubez}{\pgfmathresult}
\begin{scope}[yshift=-35cm]
\draw[red,fill=green] (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{scope}
\end{tikzpicture}
\end{document}

答案1

每当您看到像示例中那样重复的代码块时,这就提示循环可以帮助您简化它。

以下代码与您的代码功能相同:

\documentclass{article}

\usepackage{tikz}
\begin{document}  
\begin{tikzpicture}[scale=.5]
\def\A{27}
\def\xn{0.98133}
\foreach \i in {0,...,5}{
\pgfmathparse{((2*\xn) + (\A / (\xn^2)))/3}
\xdef\xn{\pgfmathresult}
\pgfmathsetmacro{\cubex}{\xn}
\pgfmathsetmacro{\cubey}{\xn}
\pgfmathsetmacro{\cubez}{\A / (\cubex * \cubey)}
\begin{scope}[yshift=-5cm*\i]
\draw[red,fill=green] (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{scope}
}
\end{tikzpicture}
\end{document} 

相关内容