4

I am trying to draw some "matrices" that looks like this

enter image description here

So above the diagonal the sticks should be blue and below they should be red. Using some help from the answer by marmot in my previous question. I was able to produce the following result

enter image description here

using the code below. Any suggestions or improvements for the code? In particular I am not happy with hacking in

    \path[use as bounding box] (0,1) rectangle (#1-1,#1); 

to obtain the correct bounding boxes.

Code

\documentclass[border=5mm]{article}
\usepackage{tikz,subcaption}

\definecolor{stickBlue}{HTML}{0099ff}
\definecolor{stickRed}{HTML}{ff0033}

\tikzset{%
  pics/staircaseSum/.style={code={
      \foreach \XX in {1,...,#1}
      {\foreach \YY in {1,...,#1}
      {
      \ifnum\XX<\YY
        \draw[ultra thick,shorten <=1.5mm,stickBlue] (\XX-1,\YY-1) -- (\XX-1, \YY);
      \fi
      \ifnum\XX>\YY
        \draw[ultra thick,shorten <=1.5mm,stickRed] (\XX-1,\YY) -- (\XX-1, \YY+1);
       \fi
      }
      }
      }
    },
  mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}
}

\newcommand{\staircaseSum}[1]{%
  \begin{tikzpicture}
    \path[use as bounding box] (0,1) rectangle (#1-1,#1); 
        \path (0,0) pic{staircaseSum=#1};
  \end{tikzpicture}
}

\begin{document}

\begin{figure}[!h]
   \centering
   \foreach \X in {1,...,4}
   {\begin{subfigure}[b]{\X.5cm}
       \centering
       \staircaseSum{\X}
       \caption*{$F_\X$}
   \end{subfigure}}
   \caption{}
\end{figure}

\end{document}
3

I think your code is very nice. The need for the bounding box can be waived by shifting the domain of \YY (or, alternatively, changing the code of the pic). The reason why you needed it in the first place was that you insert the coordinate (0,0) but because of the \ifnum the actual y values for the sticks started at 1. (You can always find out what the current bounding box is by adding \draw (current bounding box.south west) rectangle (current bounding box.north east); at the end of the picture.)

\documentclass[border=5mm]{article}
\usepackage{tikz,subcaption}

\definecolor{stickBlue}{HTML}{0099ff}
\definecolor{stickRed}{HTML}{ff0033}

\tikzset{%
  pics/staircaseSum/.style={code={
      \foreach \XX [parse=true] in {0,...,#1-1}
      {\foreach \YY [parse=true] in {0,...,#1-1}
      {
      \ifnum\XX<\YY
        \draw[ultra thick,shorten <=1.5mm,stickBlue] (\XX-1,\YY-1) -- (\XX-1, \YY);
      \fi
      \ifnum\XX>\YY
        \draw[ultra thick,shorten <=1.5mm,stickRed] (\XX-1,\YY) -- (\XX-1, \YY+1);
       \fi
      }
      }
      }
    },
  mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}
}

\newcommand{\staircaseSum}[1]{%
  \begin{tikzpicture}
        \path (0,0) pic{staircaseSum=#1};
        %\draw (current bounding box.south west) rectangle (current bounding box.north east);
  \end{tikzpicture}
}

\begin{document}

\begin{figure}[!h]
   \centering
   \foreach \X in {1,...,4}
   {\begin{subfigure}[b]{\X.5cm}
       \centering
       \staircaseSum{\X}
       \caption*{$F_\X$}
   \end{subfigure}}
   \caption{}
\end{figure}

\end{document}

enter image description here

An alternative definition of the pic is

\tikzset{%
  pics/staircaseSum/.style={code={
      \foreach \XX [parse=true] in {1,...,#1}
      {\foreach \YY [parse=true] in {1,...,#1}
      {
      \ifnum\XX<\YY
        \draw[ultra thick,shorten <=1.5mm,stickBlue] (\XX-1,\YY-2) -- (\XX-1,\YY-1);
      \fi
      \ifnum\XX>\YY
        \draw[ultra thick,shorten <=1.5mm,stickRed] (\XX-1,\YY-1) -- (\XX-1, \YY);
       \fi
      }
      }
      }
    },
  mydash/.style={thick,shorten <=1mm,dash pattern=on 8mm off 2mm}
}
  • Oooh, nice idea. Any idea why using [parse=true] is giving me the error Package pgfkeys Error: do not know the key '/pgf/foreach/parse' to which you passed 'true'? – N3buchadnezzar Jun 16 at 21:45
  • 1
    @N3buchadnezzar Probably you have not the newest pgf version installed. So you need to either install it or use the alternative definition at the bottom of the answer. – marmot Jun 16 at 21:50

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.