TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}
share|improve this question
    
Welcome to TeX.sx! – hpesoj626 Nov 27 '12 at 14:41

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} 
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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