Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

In another thread (Basic geometric shapes, sections, shading), I stalled figuring out how to position things properly. What follows is clearly wrong and indicative of almost completely missing the boat. OK, scratch the "almost" in that last sentence. How should one do this right?

The goal is to simply lay out the circle, the square and the pill horizontally where the sum of the circle and square equal the pill. I missing something about nodes, assuming that is what I should be doing.

ThanX to Gonzalo for the original code!

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{shapes,snakes}

\begin{document}
Consider the joining of a circle and square partitioned into four equal sections and labeled 1/4, as given below.  
\\ \\
\newcommand\FillSquare[5]{%
\begin{tikzpicture}
\fill[#2] (0,0) rectangle +(0.5*#1,0.5*#1);
\fill[#3] (0.5*#1,0cm) rectangle +(0.5*#1,0.5*#1);
\fill[#4] (0,0.5*#1) rectangle +(0.5*#1,0.5*#1);
\fill[#5] (0.5*#1,0.5*#1) rectangle +(0.5*#1,0.5*#1);
\end{tikzpicture}%
}

\newcommand\MySquare[5]{%
\begin{tikzpicture}
\FillSquare{#1}{#2}{#3}{#4}{#5}
\draw (0,0) rectangle (-#1,#1);
\node [right, black] at (0,1) {+}; 

\draw (-#1,0.5*#1) -- +(#1,0);
\draw (-0.5*#1,0) -- +(0,#1);
\node [below right, white] at (-0.75,1.85) {$\frac{1}{4}$}; 
\end{tikzpicture}%
}

\newcommand\MyCircle[5]{%
\begin{tikzpicture}
\fill[#4] (0,0) arc[radius=0.5*#1,start angle=180,end angle=90] -- +(0,-0.5*#1) --cycle;
\fill[#5] (#1,0) arc[radius=0.5*#1,start angle=0,end angle=90] -- +(0,-0.5*#1) --cycle;
\fill[#3] (#1,0) arc[radius=0.5*#1,start angle=0,end angle=-90] -- +(0,0.5*#1) -- cycle;
\fill[#2] (0.5*#1,-0.5*#1) arc[radius=0.5*#1,start angle=-90,end angle=-180] -- +(0.5*#1,0) -- cycle;
\draw (0.5*#1,0) circle [radius=0.5*#1];
\node [below right, black] at (2,0.3) {=}; 

\draw (0,0) -- +(#1,0);
\draw (0.5*#1,-0.5*#1) -- +(0,#1);
\node [below right, white] at (1.2,0.8) {$\frac{1}{4}$};
\end{tikzpicture}%
}

\newcommand\Joined[9]{%
\begin{tikzpicture}
\fill[#2] (0,0) arc[radius=0.5*#1,start angle=270,end angle=180] -- +(1.5cm,0) --cycle;
\fill[#5] (-0.5*#1,0.5*#1) arc[radius=0.5*#1,start angle=180,end angle=90] -- +(0,-0.5*#1) --cycle;
\fill[#6] (1.5*#1,0.5*#1) arc[radius=0.5*#1,start angle=0,end angle=90] -- +(0,-0.5*#1) --cycle;
\fill[#9] (1.5*#1,0.5*#1) arc[radius=0.5*#1,start angle=0,end angle=-90] -- +(0,0.5*#1) -- cycle;
\FillSquare{#1}{#3}{#4}{#7}{#8}
\draw (0,0) -- +(-#1,0) 
  arc[radius=0.5*#1,start angle=270,end angle=90] -- +(#1,0)
  arc[radius=0.5*#1,start angle=90,end angle=-90] -- cycle;
\draw (-1.5*#1,0.5*#1) -- +(2*#1,0);
\draw (-0.5*#1,0) -- +(0,#1);
\draw (-#1,0) -- +(0,#1);
\draw (0,0) -- +(0,#1);
\node [below right, white] at (-0.25,1.8) {$\frac{1}{4}$};
\end{tikzpicture}%
}

\MySquare{2cm}{white}{white}{white}{gray} \ \ \ \ \ \ \ \ \ \ \ \ \ \
\MyCircle{2cm}{white}{white}{white}{gray} \
\Joined{2cm}{white}{white}{white}{white}{gray}{white}{gray}{white}\quad
\\ \\
As the picture suggests, somehow 1/4 + 1/4 = 1/4.\\
\end{document}
share|improve this question
1  
I’m not entirely clear, what should happen here, but two things came to my mind, don’t nest TikZ picture. It would be better to use the TikZ paths in a macro and place them manually in a TikZ picture. Placing the + and = in a node works of course but it would be better to use these three TikZ pictures in math mode (displayed!), then you also wouldn’t need to do dirty tricks with \ and \\ \\. The baseline option, e.g. baseline=-.5ex could help here. (Or you could try the code from my answer from the linked question. ;)) –  Qrrbrbirlbel Jun 21 '13 at 0:57
    
I agree with @Qrrbrbirlbel's comment and, in particular, with the last part. Had I known that this was the kind of thing you were trying to do, I would have suggested a different approach in the other question. The modifications needed in my code to be easily usable here are too many, and I think Qrrbrbirlbel's answer will be better for this. –  Gonzalo Medina Jun 21 '13 at 2:25
1  
I've edited my first answer to the question linked, introducing some improvements. Anyway, since that first answer was only focused on the drawing (not in using labels), Qrrbrbirlbel's approach is still better. –  Gonzalo Medina Jun 21 '13 at 2:49
    
As I began/begin to understand the code, it occurred/occurs to me that the very issues that you bring up were/are essential. I had originally thought that the positioning would be much simpler, but came to realize that I needed a better "container" for these objects. Lots for me to to learn...thanX for the help. –  Clark Jun 21 '13 at 5:38
add comment

1 Answer

up vote 3 down vote accepted

Here's a possibility using Qrrbrbirlbel's excellent answer to Basic geometric shapes, sections, shading:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,shapes.misc}

\makeatletter
\def\qrr@ppbb{path picture bounding box}
\tikzset{
  divide/.style={%
    /utils/exec=%
      \let\qrr@pathpicture\pgfutil@empty
      \pgfutil@tempcnta\z@ % rows
      \pgfutil@tempcntb\z@,% cols
    /tikz/@divide rows/.list={#1},
    path picture/.expand once=\qrr@pathpicture
  },
  @divide rows/.style={%
    /utils/exec=\pgfutil@tempcntb\z@,
    @divide cols/.list={#1},
    /utils/exec=\advance\pgfutil@tempcnta\@ne
  },
  @divide cols/.code=
    \let\pgf@tempb\pgfutil@empty
    \let\pgf@tempc\pgfutil@empty
    \pgfutil@in@{:}{#1}%
    \ifpgfutil@in@
      \pgfkeysalso{@divide cols split={#1}}%
    \else
      \def\pgf@tempa{#1}%
    \fi
    \eappto\qrr@pathpicture{%
      \noexpand\path
            ($(\qrr@ppbb.north west)!\the\pgfutil@tempcntb/\noexpand\the\pgfutil@tempcntb!(\qrr@ppbb.north east)$)
            coordinate (qrr@pp@tl)
            ($(\qrr@ppbb.north west)!\number\numexpr\pgfutil@tempcntb+\@ne\relax/\noexpand\the\pgfutil@tempcntb!(\qrr@ppbb.north east)$)
            coordinate (qrr@pp@tr)
            ($(\qrr@ppbb.north west)!\the\pgfutil@tempcnta/\noexpand\the\pgfutil@tempcnta!(\qrr@ppbb.south west)$)
            coordinate (qrr@pp@lt)
            ($(\qrr@ppbb.north west)!\number\numexpr\pgfutil@tempcnta+\@ne\relax/\noexpand\the\pgfutil@tempcnta!(\qrr@ppbb.south west)$)
            coordinate (qrr@pp@bl);
      \noexpand\path[
        every divide/.try,
        every divide \the\pgfutil@tempcntb\space row/.try,
        every divide \the\pgfutil@tempcnta\space column/.try,
        every divide \the\pgfutil@tempcntb-\the\pgfutil@tempcntb\space cell/.try,
        \pgf@tempa,
        every divide later/.try,
        every divide \the\pgfutil@tempcntb\space row later/.try,
        every divide \the\pgfutil@tempcnta\space column later/.try,
        every divide \the\pgfutil@tempcntb-\the\pgfutil@tempcntb\space cell later/.try,
        midway
        ] (qrr@pp@tl |- qrr@pp@lt) 
          coordinate (cell-\the\pgfutil@tempcntb-\the\pgfutil@tempcnta-tl)
          rectangle (qrr@pp@tr |- qrr@pp@bl) 
          coordinate (cell-\the\pgfutil@tempcntb-\the\pgfutil@tempcnta-br)
          \ifx\pgf@tempc\pgfutil@empty\else
            node[
              every divide node/.try,
              every divide \the\pgfutil@tempcntb\space row node/.try,
              every divide \the\pgfutil@tempcnta\space column node/.try,
              every divide \the\pgfutil@tempcntb-\the\pgfutil@tempcntb\space cell node/.try,
              \expandafter\unexpanded\expandafter{\pgf@tempb}] {\expandafter\unexpanded\expandafter{\pgf@tempc}}
          \fi
          ;
    }%
    \advance\pgfutil@tempcntb\@ne,
  @divide cols split/.code args={#1:#2}{
    \def\pgf@tempa{#1}%
    \pgfutil@ifnextchar[\qrr@divide@splitopt{\qrr@divide@splitopt[]}#2\@qrr@divide@splitopt
  }
}
\def\qrr@divide@splitopt[#1]#2\@qrr@divide@splitopt{\def\pgf@tempb{#1}\def\pgf@tempc{#2}}
\makeatother
\tikzset{every divide node/.style={midway,text=black}}

\begin{document}

Consider the joining of a circle and square partitioned into four equal sections and labeled $\frac{1}{4}$, as given below.
\[
\begin{tikzpicture}
\draw[every divide/.style={fill},,every divide later/.style={draw=black}] (0,0) rectangle (2,2) [divide={{white,gray:$\frac{1}{4}$},{white,white}}];
\draw[every divide/.style={fill},every divide later/.style={draw=black}]  
  (4,1) circle [radius=1] [divide={{white,gray:$\frac{1}{4}$},{white,white}}];
\node[draw,rounded rectangle,minimum size=4cm,minimum height=2cm,
  divide={{draw,draw=gray!75!black,{fill=gray,draw=gray!75!black},{draw,fill=gray}},{draw,,draw=gray!75!black,draw}}
] (n) at (8,1) {};
\node[yshift=-0.5cm,xshift=1cm] at (n.north) {$\frac{1}{4}$}; 
\node at (2.5,1) {$+$};
\node at (5.5,1) {$=$};
\end{tikzpicture}
\]
As the picture suggests, somehow $\frac{1}{4} + \frac{1}{4} = \frac{1}{4}$.

\end{document}

enter image description here

share|improve this answer
add comment

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.