7

I have the following code to print a matrix:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={minimum width=3cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0, fill=mlightgray}},
  row 2/.style = {text height=1.3ex, text depth=0},
  row 3/.style = {text height=1.3ex, text depth=0},
  row 4/.style = {text height=1.3ex, text depth=0},
  row 5/.style = {text height=1.3ex, text depth=0},  
  row 6/.style = {text height=1.3ex, text depth=0},
  ] (m)
  {   &  \text{1}   \\
    \text{1} & =20  \\
    \text{2} & =90  \\
    \text{3} & =110 \\
    \text{4} & =80  \\
    \text{5} & = {\scriptstyle \funsum(\scell{1}{1}:\scell{4}{1})} \\
  };
  \node[font=\Large,anchor=south] at (m.north) {\inmblue{Formulas}};
\end{tikzpicture}
\end{document}

enter image description here

Now, I would like to add the dependency arrow inside. The arrow could be either in the middle or on one side. Does anyone know how to realise this?

enter image description here

enter image description here

  • You might try tikzmark to set position marks and draw the arrow afterwards. – Philipp Imhof Sep 6 '15 at 15:51
  • 1
    Not tested but this should work: \draw[blue,-stealth,xshift=-1ex] (m-2-2.east) -- (m-6-2.east); \foreach \x in {2,...,6}{ \fill[blue,xshift=-1ex] (m-\x-2.east) circle (2pt); } – user11232 Sep 6 '15 at 15:55
  • Your example wont compile as is, there's at least one undefined colour mlightgray, three undefined macros (\funsum, \scell, \inmblue), and you're missing amsmath for \text. – Torbjørn T. Sep 6 '15 at 16:06
  • @HarishKumar Almost, I think you must add the xshift within each coordinate, and the foreach should stop at 5. – Torbjørn T. Sep 6 '15 at 16:15
4

You can use the names for the matrix nodes and their anchors:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand\funsum{}
\newcommand\scell[2]{}
\newcommand\inmblue[1]{}

\colorlet{mlightgray}{lightgray}

\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={minimum width=3cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0, fill=mlightgray}},
  row 2/.style = {text height=1.3ex, text depth=0},
  row 3/.style = {text height=1.3ex, text depth=0},
  row 4/.style = {text height=1.3ex, text depth=0},
  row 5/.style = {text height=1.3ex, text depth=0},  
  row 6/.style = {text height=1.3ex, text depth=0},
  ] (m)
  {   &  \text{1}   \\
    \text{1} & =20  \\
    \text{2} & =90  \\
    \text{3} & =110 \\
    \text{4} & =80  \\
    \text{5} & = {\scriptstyle \funsum(\scell{1}{1}:\scell{4}{1})} \\
  };
  \node[font=\Large,anchor=south] at (m.north) {\inmblue{Formulas}};
  \draw[blue,-latex] 
    ([xshift=-1.5pt]m-2-2.center) -- ([xshift=-1.5pt]m-6-2.center); 
  \foreach \x in {2,...,5}
    {\fill[blue] ([xshift=-1.5pt]m-\x-2.center) circle (1.25pt);} 
  \draw[blue,-latex] 
    ([xshift=-5pt]m-2-2.east) -- ([xshift=-5pt]m-6-2.east); 
  \foreach \x in {2,...,5}
    {\fill[blue] ([xshift=-5pt]m-\x-2.east) circle (1.25pt);} 
\end{tikzpicture}

\end{document}

enter image description here

Using some \makeboxes you can align the equals signs:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand\funsum{}
\newcommand\scell[2]{}
\newcommand\inmblue{}

\colorlet{mlightgray}{lightgray}

\begin{document}
\begin{tikzpicture}[cell/.style={rectangle,draw=black}, nodes in empty cells]
  \matrix[
  matrix of math nodes,
  row sep =-\pgflinewidth,
  column sep = -\pgflinewidth,
  nodes={anchor=center, minimum width=2cm, cell},
  column 1/.style = {nodes={minimum width=1cm, fill=mlightgray}},
  column 2/.style = {nodes={minimum width=3cm}},
  row 1/.style = {nodes={text height=1.3ex, text depth=0, fill=mlightgray}},
  row 2/.style = {text height=1.3ex, text depth=0},
  row 3/.style = {text height=1.3ex, text depth=0},
  row 4/.style = {text height=1.3ex, text depth=0},
  row 5/.style = {text height=1.3ex, text depth=0},  
  row 6/.style = {text height=1.3ex, text depth=0},
  ] (m)
  {   &  \text{1}   \\
    \text{1} & \makebox[27pt][l]{${}=20$}  \\
    \text{2} & \makebox[27pt][l]{${}=90$}  \\
    \text{3} & \makebox[27pt][l]{${}=110$} \\
    \text{4} & \makebox[27pt][l]{${}=80$}  \\
    \text{5} & = {\scriptstyle \funsum(\scell{1}{1}:\scell{4}{1})} \\
  };
  \node[font=\Large,anchor=south] at (m.north) {\inmblue{Formulas}};
  \draw[blue,-latex] 
    ([xshift=-1.5pt]m-2-2.center) -- ([xshift=-1.5pt]m-6-2.center); 
  \foreach \x in {2,...,5}
    {\fill[blue] ([xshift=-1.5pt]m-\x-2.center) circle (1.25pt);} 
  \draw[blue,-latex] 
    ([xshift=-5pt]m-2-2.east) -- ([xshift=-5pt]m-6-2.east); 
  \foreach \x in {2,...,5}
    {\fill[blue] ([xshift=-5pt]m-\x-2.east) circle (1.25pt);} 
\end{tikzpicture}

\end{document}

enter image description here

I had to provide some dummy definitions to make your code compilable.

0

One more answer :-) ... based on excellent (and accepted) Gonzalo Medina's answer with try to reduce and simplify its and OP code as I found to be possible:

\documentclass[border=3mm,
               tikz,
               prewiev]{standalone}
\usetikzlibrary{arrows.meta,matrix}
\usepackage{amsmath}

\newcommand\funsum{\mathsf{SUM}}
\newcommand\scell[2]{[#1,#2]}
\newcommand\inmblue[1]{\textcolor{blue}{#1}}
\colorlet{mlightgray}{lightgray}

    \begin{document}
\begin{tikzpicture}[nodes in empty cells]
\matrix[
    matrix of math nodes,
    row sep =-\pgflinewidth,
    column sep = -\pgflinewidth,
    nodes={rectangle, draw=black, text height=1.3ex},
    column 1/.style = {nodes={minimum width=1cm,fill=mlightgray}},
    column 2/.style = {nodes={   text width=3cm,align=left}},
    row 1/.style = {nodes={fill=mlightgray}},
    row 6/.style = {nodes={text depth=0.3ex}},
        ] (m)
{   & \hspace{14mm}    1  \\
1   & \hspace{13mm} = 20  \\
2   & \hspace{13mm} = 90  \\
3   & \hspace{13mm} = 110 \\
4   & \hspace{13mm} = 80  \\
5   & \quad         = {\scriptstyle\funsum(\scell{1}{1}:\scell{4}{1})} \\
  };
  \node[font=\Large,above] at (m.north) {\inmblue{Formulas}};

\begin{scope}[blue,thick,shorten <=-0.88mm,
               opacity=.5, transparency group]
\path[draw,{Circle[length=2mm]}-]
    (m-2-2.center) edge (m-3-2.center)
    (m-3-2.center) edge (m-4-2.center)
    (m-4-2.center)  --  (m-5-2.center);
\draw[{Circle[length=2mm]}-{Triangle[length=2mm]},shorten >=-0.5mm]  
    (m-5-2.center)  --  (m-6-2.center);
\end{scope}
\end{tikzpicture}
    \end{document}

For arrows I use `arrows.meta package and also arrows make transparent.

enter image description here

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.