6

I am trying to draw a matrix with arrows in it. It can't take up too much space however which stops the arrows from showing properly.

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[column sep=0pt, row sep=0pt]
&&0&0&0&0&0&0&0&0&0&0\\
&0\ar[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\ar[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\ar[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\ar[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\ar[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\ar[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\ar[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\ar[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\ar[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\ar[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\ar[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\ar[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4\\
\end{tikzcd}
\end{document}

I don't particularly need full arrows if simple lines would look better. But I do need all the diagonal lines to be in a straight line with each other (there is a curious curve currently), for the arrows not to be just arrow heads and for the vertical lines to centered between the rows.

enter image description here

This is a followup to Why does a vertical arrow get rendered incorrectly? .

5

A rather simple way of making the arrows longer is to set the inner sep to 0 and to increase the row and column seps in such a way that the size stays the same (roughly).

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[column sep=3pt, row sep=5pt,nodes={inner sep=0pt,align=center,
text width={width("20")}}]
&&0&0&0&0&0&0&0&0&0&0\\
&0\ar[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\ar[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\ar[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\ar[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\ar[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\ar[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\ar[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\ar[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\ar[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\ar[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\ar[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\ar[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4\\
\end{tikzcd}
\end{document}

enter image description here

7

Another option is using matrix of nodes in tikz environment, but it has lots of optios and makes more dificult but hightly customizable.

RESULT:

enter image description here

MWE:

% arara: pdflatex: {synctex: yes,  interaction: nonstopmode}
\documentclass[margin=0.5cm]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{matrix,arrows.meta}
\begin{document}
    \begin{tikzpicture}[
        %Global environment config.
        %baseline=0cm, %Nice but I think is usefull only in tikzpicture env.
        %Environment styles declarations
        >=Stealth,
        Matrix/.style={
            matrix of math nodes,
            nodes in empty cells,
            column sep=0.25em,
            row sep=1em,
            nodes={inner xsep=0.5pt,inner ysep=2pt,minimum width=1.5em}
            },
        Brackets/.style={
            left delimiter={[},
            right delimiter={]}
            }
    ]
    %Start drawing the thing.
    \matrix [Matrix] at (0,0)(M1){
        %Matrix contents
        &&0&0&0&0&0&0&0&0&0&0\\
        &0&1&2&3&4&5&6&7&8&9&10\\
        0&1&0&1&2&3&4&5&6&7&8&9\\
        0&2&1&0&1&2&3&4&5&6&7&8\\
        0&3&2&1&0&1&2&3&4&5&6&7\\
        0&4&3&2&1&0&1&2&3&4&5&6\\
        0&5&4&3&2&1&0&1&2&3&4&5\\
        0&6&5&4&3&2&1&0&1&2&3&4\\
        0&7&6&5&4&3&2&1&0&1&2&3\\
        0&8&7&6&5&4&3&2&1&0&1&2\\
        1&9&8&7&6&5&4&3&2&1&1&2\\
        1&10&9&8&7&6&5&4&3&2&2&2\\
        1&11&10&9&8&7&6&5&4&3&3&3\\
        1&12&11&10&9&8&7&6&5&4&4&4\\        
    };

    \foreach \x [evaluate=\x as \m using int(\x+1)]in {2,3,...,10}{
    \draw[->] (M1-\x-\x) -- (M1-\m-\m);
    }
    \draw[->] (M1-11-11) -- (M1-12-11);
    \draw[->] (M1-12-11) -- (M1-13-11);
    \draw[->] (M1-13-11) -- (M1-14-12);

    \end{tikzpicture}

\end{document}
  • tikz-cd uses the tikz matrix environments, so this approach is not different from the OP's (or mine). +1. Haven't seen you in a while, welcome back! – marmot Mar 21 at 21:59
  • Thanks, I maybe be back, in some sporadic way, I've missed the community, and the comments of experienced people and by the way certain strange fun – J Leon V. Mar 22 at 0:28
5

Not really a viable solution for general purpose, but I just wanted to prove to myself it could be done as a TABstack.

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabstackengine,graphicx}
\newcommand\minidarrow{\scalebox{.8}{\ensuremath{\downarrow}}}
\newcommand\minidrarrow{\scalebox{.8}{\rotatebox[origin=c]{-45}{$\rightarrow$}}}
\def\AR[#1#2]{% 
  \setbox0=\hbox{0}%
  \ifx#1d
    \ifx\relax#2\relax
      \kern-.5\wd0
      \bclap[.35\dimexpr\Lstackgap]{\minidarrow}%
      \kern.5\wd0%
    \else
      \ifx r#2
        \kern.5\wd0
        \bclap[.3\dimexpr\Lstackgap]{\minidrarrow}%
        \kern-.5\wd0
      \else
%      ...
      \fi
    \fi
  \else
%  ...
  \fi
}
\fixTABwidth{T}
\setstackgap{L}{1.15\baselineskip}
\setstacktabbedgap{3pt}% May need to be tailored
\begin{document}
\Matrixstack{
&&0&0&0&0&0&0&0&0&0&0\\
&0\AR[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\AR[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\AR[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\AR[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\AR[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\AR[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\AR[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\AR[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\AR[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\AR[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\AR[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\AR[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4}
\end{document}

enter image description here

One can play with the compression by changing a few lines:

\documentclass{article}
\usepackage[margin=1cm]{geometry}
\usepackage{tabstackengine,graphicx}
\newcommand\minidarrow{\scalebox{.8}[.6]{\ensuremath{\downarrow}}}
\newcommand\minidrarrow{\scalebox{.8}{\rotatebox[origin=c]{-45}{$\rightarrow$}}}
\def\AR[#1#2]{% 
  \setbox0=\hbox{0}%
  \ifx#1d
    \ifx\relax#2\relax
      \kern-.5\wd0
      \bclap[.15\dimexpr\Lstackgap]{\minidarrow}%
      \kern.5\wd0%
    \else
      \ifx r#2
        \kern.5\wd0
        \bclap[.3\dimexpr\Lstackgap]{\minidrarrow}%
        \kern-.5\wd0
      \else
%      ...
      \fi
    \fi
  \else
%  ...
  \fi
}
\fixTABwidth{T}
\setstackgap{L}{.8\baselineskip}
\setstacktabbedgap{0.7pt}% May need to be tailored
\begin{document}
\Matrixstack{
&&0&0&0&0&0&0&0&0&0&0\\
&0\AR[dr]&1&2&3&4&5&6&7&8&9&10\\
0&1&0\AR[dr]&1&2&3&4&5&6&7&8&9\\
0&2&1&0\AR[dr]&1&2&3&4&5&6&7&8\\
0&3&2&1&0\AR[dr]&1&2&3&4&5&6&7\\
0&4&3&2&1&0\AR[dr]&1&2&3&4&5&6\\
0&5&4&3&2&1&0\AR[dr]&1&2&3&4&5\\
0&6&5&4&3&2&1&0\AR[dr]&1&2&3&4\\
0&7&6&5&4&3&2&1&0\AR[dr]&1&2&3\\
0&8&7&6&5&4&3&2&1&0\AR[dr]&1&2\\
1&9&8&7&6&5&4&3&2&1&1\AR[d]&2\\
1&10&9&8&7&6&5&4&3&2&2\AR[d]&2\\
1&11&10&9&8&7&6&5&4&3&3\AR[dr]&3\\
1&12&11&10&9&8&7&6&5&4&4&4}
\end{document}

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.