4

The picture below is what I am trying to create.

I have asked a similar question before, and I got an excellent answer, but it's still not 100% what I am looking for. I know that I have to use \usepackage{tikz} and possibly \usepackage{xintexpr} and \usetikzlibrary{math}

This is what my homework is asking me to do \documentclass[tikz,border=5]{article} \usepackage{tikz} \usepackage{xintexpr} \usetikzlibrary{math} \begin{document}

\begin{tikzpicture}[x=2cm,y=-2cm, node 0/.style={fill=red!20}]
\tikzmath{%
  int \i, \j, \m, \n, \t;
  \m = 5; \n = 5;
  % Initialise board.
  for \i in {0,...,\m}{
  for \j in {0,...,\n}{
    \t{\i,\j} = 0;
         };
       };
  % Create holes.
  \t{2,3} = -1;
  \t{3,4} = -1;
  \t{5,2} = -1;
 % Perform calculations.
  for \i1 in {0,...,\m}{
    for \j1 in {0,...,\n}{
      if (\t{\i1,\j1} == -1) then {
        \t{\i1,\j1} = 0;
      } else {
         if (\i1 == 0 || \j1 == 0) then  {
          \t{\i1,\j1} = 1;            
      } else {
         \i2 = \i1 - 1;
         \j2 = \j1 - 1;
         \t{\i1,\j1} = \t{\i2,\j1} + \t{\i1,\j2}; 
            };
          };
        };
      };
 % Draw nodes.
    for \i1 in {0,...,\m}{
     for \j1 in {0,...,\n}{
     { \node [circle, fill=blue!20, minimum size=1cm, node \t{\i1,\j1}/.try] 
         (n-\i1-\j1) at (\j1, \i1) {\t{\i1,\j1}}; };
        };   
       };
  % Draw edges.
for \i1 in {0,...,\m}{
  for \j1 in {0,...,\n}{
    \i2 = \i1 + 1;
    \j2 = \j1 + 1;
       if (\i1 < \m) then {
         if (\t{\i2,\j1} > 0) then { 
               { \draw [thick, -stealth] (n-\i1-\j1) -- (n-\i2-\j1); };
      }; 
    };
    if (\j1 < \n) then {
    if (\t{\i1,\j2} > 0) then { 
          { \draw [thick, -stealth] (n-\i1-\j1) -- (n-\i1-\j2); };
        }; 
       };
     };
   };
  }
  \end{tikzpicture}

\end{document}

This code has the idea to what I am looking for, but what I want is to refine the code so it can look exactly like the picture included. I'm new to latex and the idea of programming/coding, but I am trying to learn.

Also my picture needs to fit into my homework problem setting

\documentclass[28pt]{article}
\usepackage{fancyhdr}
\usepackage[includeheadfoot,margin=1.0cm]{geometry}
  • 1
    Please post a compilable example that produces what you have by now. – TeXnician Jul 16 '17 at 8:47
5

Ok, so an additional data structure is required to model the "routes" which have holes.

\documentclass[border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}[x=2cm, y=-2cm]
\tikzmath{%
  int \i, \j, \m, \n, \t, \r;
  \m = 6; \n = 6;
  % Initialise board.
  for \i in {0,...,\m-1}{
    for \j in {0,...,\n-1}{
      \t{\i,\j} = 0;
    }; 
  };
  for \i in {0,...,\m-1}{
    for \j in {0,...,\n-1}{
      \r{\i,\j} = 11;
    }; 
  };
  % Starting point.
  \t{0,0} = 1;
  % Create holes in route.
  % 01 can go east
  % 10 can go south
  % 11 can go east and south
  \r{2,2} = 10;
  \r{3,3} = 10;
  \r{4,2} = 01;
  % Perform calculations.
  for \i1 in {0,...,\m-1}{
    for \j1 in {0,...,\n-1}{
      if (\i1 == 0 && \j1 == 0) then {
        \t{0,0} = 1;
      } else {
        if (\j1 > 0) then {
            \j2 = \j1 - 1;
            if (mod(\r{\i1,\j2}, 2) == 1) then {
                \t{\i1,\j1} = \t{\i1,\j1} + \t{\i1,\j2};
            };
        };
        if (\i1 > 0) then {
            \i2 = \i1 - 1;
            if (mod(floor(\r{\i2,\j1} / 10), 2) == 1) then {
                \t{\i1,\j1} = \t{\i1,\j1} + \t{\i2,\j1};
            };
        };
      };
    };
  };
 % Draw nodes.
    for \i1 in {0,...,\m-1}{
     for \j1 in {0,...,\n-1}{
     { \fill  (\j1, \i1) circle [radius=0.05]
        node [above left] {\t{\i1,\j1}}; };
      };   
       };
    % Draw edges.
    for \i1 in {0,...,\m-1}{
      for \j1 in {0,...,\n-1}{
        if (mod(\r{\i1,\j1}, 2) == 1 && \j1 < \n - 1) then {
           { \draw [thick, -stealth, shorten >=0.1cm] (\j1,\i1) -- ++(1, 0); };
        }; 
        if (mod(floor(\r{\i1,\j1} / 10), 2) == 1 && \i1 < \m - 1) then {
           { \draw [thick, -stealth,  shorten >=0.1cm] (\j1,\i1) -- ++(0,1); };
        }; 
      };
   };
}
\end{tikzpicture}
\end{document}

enter image description here

  • This is good, however the "holes" that I need to create cannot so easily be done with your code. The picture above can show you exactly what I mean. – Nixie777 Jul 17 '17 at 5:08
  • @Nixie777 see the updated answer. – Mark Wibrow Jul 17 '17 at 11:54
  • This is perfect! I know I not suppose to say thank you in comments, but truly, thank you so much! This helps out so much! – Nixie777 Jul 18 '17 at 7:29
2

I hope this is what you want

\documentclass{standalone}
\usepackage{tikz}
\usepgfmodule{parser}
\begin{document}

\catcode`\^^`=12\relax\pgfparserdef{countpath}{initial}{the character ^^`}{\nextcell}\catcode`\^^`=10\relax
\catcode`\^^M=12\relax\pgfparserdef{countpath}{initial}{the character ^^M}{\nextline}\catcode`\^^M=5\relax
                      \pgfparserdef{countpath}{all}{the character ;}{\endcountpath}
                      \pgfparserdef{countpath}{all}{the character +}{\nextcell\drawanode}
                      \pgfparserdef{countpath}{all}{the character |}{\nextcell\draw(\coordx,-\coordy)+(0,1)--+(0,-1);\expandafter\xdef\csname cp/\coordx/\coordy\endcsname{C}}
                      \pgfparserdef{countpath}{all}{the character -}{\nextcell\draw(\coordx,-\coordy)+(-1,0)--+(1,0);\expandafter\xdef\csname cp/\coordx/\coordy\endcsname{C}}
\def\countpath{
    \begin{tikzpicture}[shorten <=9pt,shorten >=9pt]
        \def\coordx{0}\def\coordy{0}
        \catcode`\^^M=12\relax\catcode`\^^`=12\relax
        \pgfparserparse{countpath}%
}
\def\endcountpath{
    \end{tikzpicture}
    \pgfparserswitch{final}
}
\def\nextcell{\pgfmathtruncatemacro\coordx{\coordx+1}}
\def\nextline{\pgfmathtruncatemacro\coordy{\coordy+1}\def\coordx{0}}
\def\drawanode{
    \def\numberofpaths{0}
    \ifnum\coordy=1
        \ifnum\coordx=1
            \def\numberofpaths{1}
        \fi
    \fi
    % check upper neighbor
    \pgfmathtruncatemacro\coordyminusi{\coordy-1}
    \expandafter\if\csname cp/\coordx/\coordyminusi\endcsname\relax\else
        \pgfmathtruncatemacro\coordyminusii{\coordy-2}
        \pgfmathtruncatemacro\numberofpaths{\numberofpaths+\csname cp/\coordx/\coordyminusii\endcsname}
    \fi
    % check left neighbor
    \pgfmathtruncatemacro\coordxminusi{\coordx-1}
    \expandafter\if\csname cp/\coordxminusi/\coordy\endcsname\relax\else
        \pgfmathtruncatemacro\coordxminusii{\coordx-2}
        \pgfmathtruncatemacro\numberofpaths{\numberofpaths+\csname cp/\coordxminusii/\coordy\endcsname}
    \fi
    \expandafter\xdef\csname cp/\coordx/\coordy\endcsname{\numberofpaths}
    \draw(\coordx,-\coordy)node{\numberofpaths};
}

\countpath
+-+-+-+-+-+-+-+ +-+
| |   | | |   | | |
+-+ +-+-+ +-+-+-+-+
|   | | |   | | | |
+-+-+-+-+-+-+-+ +-+
| | | | |     | | |
+-+ +-+-+ +-+-+ +-+
|   | | |   | | | |
+-+-+-+-+-+-+-+-+-+
| |   | | |   | | |
+-+-+-+-+-+-+-+ +-+
|   | | | | | | | |
+-+-+-+-+-+-+-+-+-+
;

\end{document}

  • This is impressive. I'll use this as well to figure out another compound pathway I have. – Nixie777 Jul 18 '17 at 7:30
  • SO I copied your code into sharelatex, but for some reason the output gives me the pathway, but with all the numbers are now zero. – Nixie777 Jul 18 '17 at 7:37
  • I now know why: this site translates my tabs into spaces so the coordinates are shifted to the right by four units. I fixed the code (by deleting extra spaces.) – Symbol 1 Jul 19 '17 at 3:19

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.