3

I try to place multiple graphics in my Document. I tried to create a scope and shift the graphics, is there a better way?

\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}
4

Whenever you see chunks of code repeated like in your example, that's a hint that looping can help you simplify it.

The following code does the same as yours:

\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} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.