6

The question is really simple but I am a beginner in the world of tikz.

I have the following example and I am trying to simplify the connections of the nodes.

Here the working example:

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline,remember picture,
                   every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
                 column sep=.75cm,
                 row sep=0.75cm,nodes={anchor=center},]
{
\dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\
\dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\
\dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\
\dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\
\vdots      & \vdots      & \vdots      & \vdots      &\vdots\\
};
\draw (bruch-1-1) -- (bruch-2-1);
\draw (bruch-2-1) -- (bruch-1-2);
\draw (bruch-1-2) -- (bruch-1-3);
\draw (bruch-1-3) -- (bruch-2-2);
\draw (bruch-2-2) -- (bruch-3-1);
\draw (bruch-3-1) -- (bruch-4-1);
\draw (bruch-4-1) -- (bruch-3-2);
\draw (bruch-3-2) -- (bruch-2-3);
\draw (bruch-2-3) -- (bruch-1-4);
\draw (bruch-1-4) -- (bruch-1-5);
\end{tikzpicture}
\end{equation}
\end{document}

I tried the following foreach construction but it fails.

\foreach \x-\y [remember=\x-\y as \lastx-\lasty (initially 1-1)] in%
   (2-1,1-2,1-3,2-2,3-1,4-1,3-2,2-3,1-4,1-5){
    \draw (bruch-\lastx-\lasty) -- (bruch-\x-\y);}

How can I simplify the connecting of nodes?

EDIT

The solution of Altermundus works with a the CSV-version (see 1). The csv-version needs a manual installation. So is there a way to get a solution which works without the csv?

(1) CSV-Version from Altermundus.com or from the sourceforge website(it's newer)

Moriambar
  • 11,466
Marco Daniel
  • 95,681

3 Answers3

5

I don't know if it's possible to use a syntax like remember=\x-\ybut you can write something like this

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}
\begin{equation}
\begin{tikzpicture}[baseline,remember picture,
                   every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
                 column sep=.75cm,
                 row sep=0.75cm,nodes={anchor=center},]
{
\dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\
\dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\
\dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\
\dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\
\vdots      & \vdots      & \vdots      & \vdots      &\vdots\\
};

\foreach \x/\y [remember=\x as \lastx  (initially 1),
                remember=\y as \lasty  (initially 1) ] in%
   {2/1,1/2,1/3,2/2,3/1,4/1,3/2,2/3,1/4,1/5}{
    \draw (bruch-\lastx-\lasty) -- (bruch-\x-\y);}
\end{tikzpicture}
\end{equation}  

\begin{equation}
\begin{tikzpicture}[baseline,remember picture,
                   every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
                 column sep=.75cm,
                 row sep=0.75cm,nodes={anchor=center},]
{
\dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\
\dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\
\dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\
\dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\
\vdots      & \vdots      & \vdots      & \vdots      &\vdots\\
};
\draw (bruch-1-1) -- (bruch-2-1);
\draw (bruch-2-1) -- (bruch-1-2);
\draw (bruch-1-2) -- (bruch-1-3);
\draw (bruch-1-3) -- (bruch-2-2);
\draw (bruch-2-2) -- (bruch-3-1);
\draw (bruch-3-1) -- (bruch-4-1);
\draw (bruch-4-1) -- (bruch-3-2);
\draw (bruch-3-2) -- (bruch-2-3);
\draw (bruch-2-3) -- (bruch-1-4);
\draw (bruch-1-4) -- (bruch-1-5);
\end{tikzpicture}
\end{equation}
\end{document}

enter image description here

There is a problem with this code if you use pgf 2.1 . pgf 2.1 CVS is necessary. The next comment is from Andrew Stacey :

The problem appears to be with the initially bit. If you take that out, it works except that it complains about the first values. So if you put \def\lastx{1}\def\lasty{1} just before the \foreach loop then it works. I guess that the TikZ/PGF team are aware of this and fixed it in the CVS.

Moriambar
  • 11,466
Alain Matthes
  • 95,075
  • Do you get the correct output with this? I tried the same, but all the arrows are drawn from bruch-1-1, so it doesn't work as it should. – Torbjørn T. Oct 08 '11 at 16:43
  • 1
    Yes it's the same. With remember pictureyou need to compile twice ! – Alain Matthes Oct 08 '11 at 16:50
  • @Altermundus: I have the same problem like Torbjorn. – Marco Daniel Oct 08 '11 at 17:06
  • Hm, by copying your code I get this: http://i.imgur.com/lg89b.png with an updated TeXlive 2011, and multiple compiler runs. – Torbjørn T. Oct 08 '11 at 17:06
  • I work with MacTex 2011 and pgf 2.1 CVS (last version). Perhaps someone can confirm that the code gives a good result. – Alain Matthes Oct 08 '11 at 17:09
  • Same with Torbjorn. I also tried \x in {1-1,2-1,1-2} option and that also leads to arrows with common starting point. – percusse Oct 08 '11 at 17:42
  • I try with pgf 2.1 from MacTeX 2011 and the problem appears. You need the version 2.1 CVS. – Alain Matthes Oct 08 '11 at 17:45
  • 1
    I use this one : http://pgf.cvs.sourceforge.net/pgf/ . You can find the version (27/07/2011) on my site : http://altermundus.com/pages/builds/index.html – Alain Matthes Oct 08 '11 at 20:09
  • 4
    I just tried this code with PGF2.10. From reading the manual, it ought to work but it doesn't. The problem appears to be with the initially bit. If you take that out, it works except that it complains about the first values. So if you put \def\lastx{1}\def\lasty{1} just before the \foreach loop then it works. I guess that the TikZ/PGF team are aware of this and fixed it in the CVS. – Andrew Stacey Oct 09 '11 at 19:48
  • @Altermundus: Can you insert the comment of Andrew in you answer. – Marco Daniel Oct 10 '11 at 16:32
  • @Marco yes no problem – Alain Matthes Oct 10 '11 at 18:49
5

Altermundus has given the actual answer to your question, but I'm afraid that I just can't stand all that repetition. Here's a slightly different implementation of the diagram you're drawing.

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}[every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
                 column sep=.75cm,
                 row sep=0.75cm,
                 nodes={anchor=center},
                 execute at empty cell={\node {\dfrac{\the\pgfmatrixcurrentcolumn}{\the\pgfmatrixcurrentrow}};},
]
{
& & & & \ldots \\
& & & & \ldots \\
& & & & \ldots \\
& & & & \ldots \\
\vdots & \vdots & \vdots & \vdots & \vdots \\
};
\foreach \i in {1,...,4} 
\foreach \j in {1,...,\i} {
  \pgfmathtruncatemacro{\ti}{Mod(\i,2) ? \j : \i - \j + 1}
  \pgfmathtruncatemacro{\tj}{Mod(\i,2) ? \i - \j + 1 : \j}
  \pgfmathtruncatemacro{\ni}{\j == \i ? \ti + (Mod(\i,2) ? 1 : 0) : \ti - (-1)^Mod(\i,2)}
  \pgfmathtruncatemacro{\nj}{\j == \i ? \tj + (Mod(\i,2) ? 0 : 1) : \tj + (-1)^Mod(\i,2)}
  \draw (bruch-\ti-\tj) -- (bruch-\ni-\nj);
}
\end{tikzpicture}
\end{document}

Things to note:

  1. The contents of the cells are placed automatically using the execute at empty cell key. In the non-empty cells - the dots - this is ignored. In the others, it places the fraction with the numerator and denominator determined by the cell coordinates.

  2. The path between the cells is computed using a double loop rather than a single loop with two variables. Within the loop, the current and next cells are computed explicitly, rather than being remembered, but due to this it is very easy to extend the diagram further.

Result:

Countability of rationals

Moriambar
  • 11,466
Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • +1 very nice. At the moment I don't understand some parts of your code. But I will read the manual now. – Marco Daniel Oct 11 '11 at 07:28
  • @Marco: You should definitely read the manual, but there is a lot of information in it so don't expect to take it all in first go! I'm still finding much that I didn't know. If you have any questions about parts of this code, feel free to ask me in the main chat room. – Andrew Stacey Oct 11 '11 at 07:37
1

Keeping in mind the bug that was mentioned in Altermundus' nice answer, the following also works. Well, by "works" I mean at least the syntax is accepted by TikZ and the result is the same with the figures given with strange behavior. Hence, by extrapolating, I assume that this would also work after the bug fix.

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,arrows,}
\usepackage{amsmath}
\begin{document}

\begin{tikzpicture}[baseline,remember picture,
                   every path/.style={-latex,thick}]
\matrix (bruch) [matrix of math nodes,%
                 column sep=.75cm,
                 row sep=0.75cm,nodes={anchor=center},]
{
\dfrac{1}{1} & \dfrac{2}{1} & \dfrac{3}{1} & \dfrac{4}{1} &\ldots\\
\dfrac{1}{2} & \dfrac{2}{2} & \dfrac{3}{2} & \dfrac{4}{2} &\ldots\\
\dfrac{1}{3} & \dfrac{2}{3} & \dfrac{3}{3} & \dfrac{4}{3} &\ldots\\
\dfrac{1}{4} & \dfrac{2}{4} & \dfrac{3}{4} & \dfrac{4}{4} &\ldots\\
\vdots      & \vdots      & \vdots      & \vdots      &\vdots\\
};
\def\lastx{1-1}
\foreach \x [remember=\x as \lastx] in%
   {2-1,1-2,1-3,2-2,3-1,4-1,3-2,2-3,1-4,1-5}{
    \draw (bruch-\lastx) -- (bruch-\x);}
\end{tikzpicture}

\end{document}

It would be great if one can verify this, since I don't have the CVS version.

EDIT The code above now works with Andrew's fix.

Moriambar
  • 11,466
percusse
  • 157,807