4

I am trying to create fraction diagrams for children using TikZ.

Is there an elegant way to fix the top of this diagram of 1/2 in a way that will scale when I want to show, say 29/6 with triangles... and eventually other shapes? It seems that intersections of paths that meet at non-90 degree angles don't quite intersect the way I want them to...

Messed up picture

\documentclass{article}

\usepackage{tikz}
\usepackage{xcolor}

\begin{document}

\(\frac{1}{2}\)

\begin{tikzpicture}
\filldraw[fill=gray, draw=black, thick] (0,0)--(1,0)--(1,2)--cycle;
\draw[thick] (1,0)--(2,0)--(1,2)--cycle;
\end{tikzpicture}

\end{document}
  • P.S. I am aware that in 15.3.1 of the TikZ/PGF Manual there the "miter" option, but this only seems to work within a single draw command. Is there a way to make it work across draw commands? – WeCanLearnAnything Aug 03 '15 at 21:55
  • I don't think so. A new \draw starts a new path. – cfr Aug 03 '15 at 21:57
  • You could clip all subsequent drawing commands against the triangle (ie filldraw the triangle, then reuse the path with \clip and draw all the rest) – Andrew Stacey Aug 03 '15 at 23:16
  • Sorry but I really do not think that it is polite to wait until you get a fantastic answer for any triangular case, and then update your question to require answers which can be used for other shapes as well. That changes the question very significantly and renders an extremely complete and sophisticated answer a merely partial one addressing only a special case. Do you really think that is fair? I would recommend undoing the edit, seriously considering accepting the clearly best answer and asking a new question if needed specifying which shapes. – cfr Aug 04 '15 at 01:30
  • Actually, the question that was asked was how to make paths from different – WeCanLearnAnything Aug 04 '15 at 01:39
  • [cont'd] \draw commands join more nicely for my fraction diagrams. I happened to use triangles as the one example shape because examples on this forum, I thought, are supposed to be minimal. I edited the original post because the pictures caused this thread to come across as way too triangle-focused, as opposed to path-joining-for-all-types-of-fraction-diagrams focused. Sorry for misleading anyone here; I greatly appreciate your responses! Next time I'll make sure my examples are broader more diverse. – WeCanLearnAnything Aug 04 '15 at 01:52
  • Should I start a new thread about joining the paths from separate \draw commands? – WeCanLearnAnything Aug 04 '15 at 02:05

2 Answers2

4

I am not sure if this is what you want, but looking at the linked image, it seems to be so: I borrowed some code from Ignasi's answer to How to draw triangular grid in TikZ?. Since the triangular grid is built with \nodes, you can easily use their anchors to do the filling, which I placed in the background layer:

enter image description here

The code:

\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{shapes,backgrounds}

\newcommand\grid[1]{
\foreach \i [count=\row from 0, remember=\row as \lastrow (initially 0)] in {0,...,#1}
  {
  \foreach \j [count=\column from 0, remember=\column as \lastcolumn (initially 0)] in {0,...,\i}
    {
    \ifnum\row=0
      \node[tri](0-0) {};
    \else
      \ifnum\column=0
        \node[tri, anchor=north](\row-0) at (\lastrow-0.corner 2) {};
      \else
         \node[tri, anchor=north](\row-\column) at (\lastrow-\lastcolumn.corner 3) {};
      \fi
    \fi
    }
  }
}

\begin{document}

\begin{tikzpicture}[
tri/.style={
  draw,
  regular polygon,
  regular polygon sides=3, 
  minimum size=2cm, 
  inner sep=0pt,
  outer sep=0pt,
  line width=1pt
  }
]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (0-0.corner 1) -- 
    (3-3.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}

\begin{scope}[xshift=8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (3-0.corner 1) -- 
    (3-3.corner 1) -- 
    (3-3.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}
\end{scope}

\begin{scope}[yshift=-8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (2-0.corner 1) -- 
    (2-2.corner 1) -- 
    (3-1.corner 3) -- 
    cycle;
\end{pgfonlayer}
\end{scope}

\begin{scope}[xshift=8cm,yshift=-8cm]
\grid{3}
\begin{pgfonlayer}{background}
  \filldraw[cyan!30] 
    (0-0.corner 1) -- 
    (3-1.corner 3) -- 
    (3-0.corner 2) -- 
    cycle;
\end{pgfonlayer}
\draw[line width=1pt]
  (0-0.corner 1) -- (3-1.corner 3);
\end{scope}
\end{tikzpicture}

\end{document}
Gonzalo Medina
  • 505,128
  • (+1) Most impressive. I knew I'd seen something like this somewhere before. Too bad the OP has just updated the question to include other shapes as well ;). – cfr Aug 04 '15 at 01:25
  • Please read my comment above about editing the OP. Also, I'll still be using this for sure! – WeCanLearnAnything Aug 04 '15 at 01:54
2

One way is to fill first and then draw a single path:

\documentclass[tikz,border=10pt]{standalone}
\begin{document}
\begin{tikzpicture}
  \fill [fill=gray] (0,0)--(1,0)--(1,2)--cycle;
  \draw[thick] (0,0) -- (1,2) edge (1,0) -- (2,0) -- cycle;
\end{tikzpicture}
\end{document}

fill then draw

What's an edge?

An edge is an independent bit of path which can go off in a different direction and may be drawn (or not) with completely different options from the main path. It does not disrupt the main path. In this case, the edge draws the perpendicular down, but the main path carries on from the vertex as if no edge had been specified. edges are useful for complex paths, especially. They are constructed after the main path.

To see what is going on, change the path specification as follows:

\draw[thick] (0,0) -- (1,2) edge [red] (1,0) -- (2,0) -- cycle;

By default, the edge inherits options from the main path so this one is thick but, unlike the main path, it is red. In the above image, of course, no distinct options were specified so the edge was also the default colour and blended with the main path.

red edge

cfr
  • 198,882
  • That works for 1/2. But if I wanted to do 29/6, would I then have to write a lot more \fill and \draw commands, right? It seemed more efficient when I could use the \filldraw command and take care of both in a single line. It also seems that drawing the path of more complex fractions with one draw command might be really hard... If that's what I have to do, though, then I'll just suck it up and do it. – WeCanLearnAnything Aug 03 '15 at 22:04
  • Also, what does the "edge" part do? – WeCanLearnAnything Aug 03 '15 at 22:04
  • @WeCanLearnAnything To be honest, I don't know what you are trying to draw. That is, I don't know just how this scales up. So I don't know what's possible. Is \fill + \draw worse than \filldraw + \draw`? Can you post or link to am image of the 29/6 version or something in between which would give an idea of the pattern? There may be a better way, though - I'm no TikZ guru. – cfr Aug 03 '15 at 22:12
  • @WeCanLearnAnything See edit re. edges. – cfr Aug 03 '15 at 22:21
  • Eventually, I'd like to draw something like this, but probably even more complicated. Notice the large number of intersections and separately shaded areas. It would be great to take care of paths and fills in the same line. – WeCanLearnAnything Aug 03 '15 at 22:23