マトリックスに注釈を付けたり網掛けしたりする方法

マトリックスに注釈を付けたり網掛けしたりする方法

動的プログラミング行列を描こうとしています。これまでの試みは次のとおりです。

\documentclass[]{article}
\begin{document}
\begin{tabular}{c||c|ccccc|cccccccccc}
    &&1&1&1&1&1&0&0&0&0&0&0&0&0&0&0\\\hline\hline
    &0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15\\\hline
    0&1&1&2&3&4&5&5&6&7&8&9&10&11&12&13&14\\
    0&2&2&2&3&4&5&5&5&6&7&8&9&10&11&12&13\\
    0&3&3&3&3&4&5&5&5&5&6&7&8&9&10&11&12\\\hline
    1&4&3&3&3&3&4&5&6&6&6&7&8&9&10&11&12\\
    1&5&4&3&3&3&3&4&5&6&7&7&8&9&10&11&12\\
    1&6&5&4&3&3&3&4&5&6&7&8&8&9&10&11&12\\
    1&7&6&5&4&3&3&4&5&6&7&8&9&9&10&11&12\\
    1&8&7&6&5&4&3&4&5&6&7&8&9&10&10&11&12\\\hline
    0&9&8&7&6&5&4&3&4&5&6&7&8&9&10&10&11\\
    0&10&9&8&7&6&5&4&3&4&5&6&7&8&9&10&10\\
    0&11&10&9&8&7&6&5&4&3&4&5&6&7&8&9&10\\
    0&12&11&10&9&8&7&6&5&4&3&4&5&6&7&8&9\\
\end{tabular}
\end{document}

ここで 2 つのことを付け加えておきたいと思います。

  • 数字の間にある矢印は、左上から右下への最短経路を示します。
  • いくつかのブロック(シンボルが異なるブロック)を赤で塗りつぶしたいと思います。

これを tikz (別のテーブルを使用) で実行しようとしましたが、うまくいきませんでした。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\usepackage{pdflscape}
\begin{document}
    \tikzset{ 
        table/.style={
            matrix of nodes,
            row sep=-\pgflinewidth,
            column sep=-\pgflinewidth,
            nodes={rectangle,text width=1.5em,align=center},
            text depth=0.3125ex,
            text height=0.625ex,
            nodes in empty cells
        },
        row 1/.style={nodes={fill=green!10,text depth=0.4ex,text height=2ex}},
        row 6/.style={nodes={text depth=0.4ex,text height=2ex}},
        column 1/.style={nodes={fill=green!10}},
    }
    \begin{landscape}
        \begin{tikzpicture}
        %\begin{tabular}{c||c|cccccccccc|cccccc|ccccccccc}
        \matrix (mat) [table]{
            &&0&0&0&0&0&0&0&0&0&0&1&1&1&1&1&1&0&0&0&0&0&0&0&0&0\\
            &0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25\\
            1&1&|[fill=red!20]|1&|[fill=red!20]|2&|[fill=red!20]|3&|[fill=red!20]|4&|[fill=red!20]|5&|[fill=red!20]|6&|[fill=red!20]|7&|[fill=red!20]|8&|[fill=red!20]|9&|[fill=red!20]|10&10&11&12&13&14&15&|[fill=red!20]|16&|[fill=red!20]|17&|[fill=red!20]|18&|[fill=red!20]|19&|[fill=red!20]|20&|[fill=red!20]|21&|[fill=red!20]|22&|[fill=red!20]|23&|[fill=red!20]|24\\
            1&2&|[fill=red!20]|2&|[fill=red!20]|2&|[fill=red!20]|3&|[fill=red!20]|4&|[fill=red!20]|5&|[fill=red!20]|6&|[fill=red!20]|7&|[fill=red!20]|8&|[fill=red!20]|9&|[fill=red!20]|10&10&10&11&12&13&14&|[fill=red!20]|15&|[fill=red!20]|16&|[fill=red!20]|17&|[fill=red!20]|18&|[fill=red!20]|19&|[fill=red!20]|20&|[fill=red!20]|21&|[fill=red!20]|22&|[fill=red!20]|23\\
            1&3&|[fill=red!20]|3&|[fill=red!20]|3&|[fill=red!20]|3&|[fill=red!20]|4&|[fill=red!20]|5&|[fill=red!20]|6&|[fill=red!20]|7&|[fill=red!20]|8&|[fill=red!20]|9&|[fill=red!20]|10&10&10&10&11&12&13&|[fill=red!20]|14&|[fill=red!20]|15&|[fill=red!20]|16&|[fill=red!20]|17&|[fill=red!20]|18&|[fill=red!20]|19&|[fill=red!20]|20&|[fill=red!20]|21&|[fill=red!20]|22\\
            1&4&|[fill=red!20]|4&|[fill=red!20]|4&|[fill=red!20]|4&|[fill=red!20]|4&|[fill=red!20]|5&|[fill=red!20]|6&|[fill=red!20]|7&|[fill=red!20]|8&|[fill=red!20]|9&|[fill=red!20]|10&10&10&10&10&11&12&|[fill=red!20]|13&|[fill=red!20]|14&|[fill=red!20]|15&|[fill=red!20]|16&|[fill=red!20]|17&|[fill=red!20]|18&|[fill=red!20]|19&|[fill=red!20]|20&|[fill=red!20]|21\\
            0&5&4&4&4&4&4&5&6&7&8&9&|[fill=red!20]|10&|[fill=red!20]|11&|[fill=red!20]|11&|[fill=red!20]|11&|[fill=red!20]|11&|[fill=red!20]|12&12&13&14&15&16&17&18&19&20\\
            0&6&5&4&4&4&4&4&5&6&7&8&|[fill=red!20]|9&|[fill=red!20]|10&|[fill=red!20]|11&|[fill=red!20]|12&|[fill=red!20]|12&|[fill=red!20]|12&12&12&13&14&15&16&17&18&19\\
            0&7&6&5&4&4&4&4&4&5&6&7&|[fill=red!20]|8&|[fill=red!20]|9&|[fill=red!20]|10&|[fill=red!20]|11&|[fill=red!20]|12&|[fill=red!20]|13&12&12&12&13&14&15&16&17&18\\
            1&8&|[fill=red!20]|7&|[fill=red!20]|6&|[fill=red!20]|5&|[fill=red!20]|5&|[fill=red!20]|5&|[fill=red!20]|5&|[fill=red!20]|5&|[fill=red!20]|5&|[fill=red!20]|6&|[fill=red!20]|7&7&8&9&10&11&12&|[fill=red!20]|13&|[fill=red!20]|13&|[fill=red!20]|13&|[fill=red!20]|13&|[fill=red!20]|14&|[fill=red!20]|15&|[fill=red!20]|16&|[fill=red!20]|17&|[fill=red!20]|18\\
            %\end{tabular}
        };
        \foreach \x in {2,6,9}
        {
            \draw 
            ([xshift=-.125\pgflinewidth,yshift=3]mat-\x-1.south west) --   
            ([xshift=-.125\pgflinewidth,yshift=3]mat-\x-27.south east);
        }
    \begin{scope}[shorten >=7pt,shorten <= 7pt]
    \draw[->]  (mat-2-2.center) -- (mat-2-3.center);
    \draw[->]  (mat-2-3.center) -- (mat-2-4.center);
    \draw[->]  (mat-2-4.center) -- (mat-3-5.center);
    \draw[->]  (mat-3-5.center) -- (mat-4-6.center);
    \draw[->]  (mat-4-6.center) -- (mat-5-7.center);
    \draw[->]  (mat-5-7.center) -- (mat-6-8.center);
    \draw[->]  (mat-6-8.center) -- (mat-7-9.center);
    \draw[->]  (mat-7-9.center) -- (mat-8-10.center);
    \draw[->]  (mat-8-10.center) -- (mat-9-11.center);
    \draw[->]  (mat-9-11.center) -- (mat-10-12.center);

    \end{scope}

        \end{tikzpicture}
    \end{landscape}
\end{document}

これにはいくつか問題があります。

  • マトリックス内の 2 本の線の間に奇妙なスペースがあります。
  • シェーディングがシフトされているため、ブロックが適切に塗りつぶされません。
  • 矢印がすべて正しく整列していないようです。
  • LaTeX ソースは非常に冗長です。

ご協力いただければ幸いです。

答え1

訂正は次のとおりです:

  • row 6スタイルを削除する
  • セルの塗りつぶしと内容のずれを避けるために、削除して にtext depth設定するtext height1.1ex
  • 各セルを塗りつぶす代わりに、マトリックスの上にある 20% の不透明度の四角形で領域をシェーディングします。

    \documentclass{standalone}
    \usepackage{tikz}
    \usetikzlibrary{matrix}
    \begin{document}
    \tikzset{ 
        table/.style={
            matrix of nodes,
            row sep=-\pgflinewidth,
            column sep=-\pgflinewidth,
            nodes={rectangle,text width=1.5em,align=center},
            text height=1.1ex,
            nodes in empty cells,
        },
        row 1/.style={nodes={fill=green!10}},
        column 1/.style={nodes={fill=green!10}}
    }
    
    \begin{tikzpicture}
            \matrix (mat) [table]{
            &&0&0&0&0&0&0&0&0&0&0&1&1&1&1&1&1&0&0&0&0&0&0&0&0&0\\
            &0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25\\
            1&1&1&2&3&4&5&6&7&8&9&10&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24\\
            1&2&2&2&3&4&5&6&7&8&9&10&10&10&11&12&13&14&15&16&17&18&19&20&21&22&23\\
            1&3&3&3&3&4&5&6&7&8&9&10&10&10&10&11&12&13&14&15&16&17&18&19&20&21&22\\
            1&4&4&4&4&4&5&6&7&8&9&10&10&10&10&10&11&12&13&14&15&16&17&18&19&20&21\\
            0&5&4&4&4&4&4&5&6&7&8&9&10&11&11&11&11&12&12&13&14&15&16&17&18&19&20\\
            0&6&5&4&4&4&4&4&5&6&7&8&9&10&11&12&12&12&12&12&13&14&15&16&17&18&19\\
            0&7&6&5&4&4&4&4&4&5&6&7&8&9&10&11&12&13&12&12&12&13&14&15&16&17&18\\
            1&8&7&6&5&5&5&5&5&5&6&7&7&8&9&10&11&12&13&13&13&13&14&15&16&17&18\\
        };
        \fill[red,opacity=0.2] (mat-3-3.north west) rectangle (mat-6-12.south east);
        \fill[red,opacity=0.2] (mat-7-13.north west) rectangle (mat-9-18.south east);
        \fill[red,opacity=0.2] (mat-3-19.north west) rectangle (mat-6-27.south east);
        \fill[red,opacity=0.2] (mat-10-3.north west) rectangle (mat-10-12.south east);
        \fill[red,opacity=0.2] (mat-10-19.north west) rectangle (mat-10-27.south east);
        \foreach \x in {2,6,9}
        {
            \draw 
            ([xshift=-.125\pgflinewidth]mat-\x-1.south west) --   
            ([xshift=-.125\pgflinewidth]mat-\x-27.south east);
        }
    \begin{scope}[shorten >=7pt,shorten <= 7pt]
    \draw[->]  (mat-2-2.center) -- (mat-2-3.center);
    \draw[->]  (mat-2-3.center) -- (mat-2-4.center);
    \draw[->]  (mat-2-4.center) -- (mat-3-5.center);
    \draw[->]  (mat-3-5.center) -- (mat-4-6.center);
    \draw[->]  (mat-4-6.center) -- (mat-5-7.center);
    \draw[->]  (mat-5-7.center) -- (mat-6-8.center);
    \draw[->]  (mat-6-8.center) -- (mat-7-9.center);
    \draw[->]  (mat-7-9.center) -- (mat-8-10.center);
    \draw[->]  (mat-8-10.center) -- (mat-9-11.center);
    \draw[->]  (mat-9-11.center) -- (mat-10-12.center);
    
    \end{scope}
    
        \end{tikzpicture}
    \end{document}
    

ここに画像の説明を入力してください

編集

マトリックスのブロックをその上の20%不透明の四角形に塗りつぶして色付けするという解決策は、満足のいくものではありませんでした。セルにスタイルをブロック割り当てしたかったのですが、tikzにはそのためのオプションがありません。この質問、私はそれをなんとか実現しました:filinredオプションは、2 つのループ ( で定義) を通じてセルのブロックのスタイルを定義し/.list、次にブロックのリストがマトリックスの先頭で形式 (上、左から下、右) で定義されます:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
    \tikzset{ 
        table/.style={
            matrix of nodes,
            row sep=-\pgflinewidth,
            column sep=-\pgflinewidth,
            nodes={rectangle,text width=1.5em,align=center},
            text height=1.1ex,
            nodes in empty cells,
        },
        row 1/.style={nodes={fill=green!10}},
        column 1/.style={nodes={fill=green!10}},
        fillinred/.style args = {(#1,#2 to #3,#4)}{
            blockrows/.style={
                block/.style={
                    row ##1 column ####1/.style={nodes={fill=red!20}}
                },
                block/.list={#2,...,#4}
            },
            blockrows/.list={#1,...,#3}
        } 
    }

        \begin{tikzpicture}
            \matrix (mat) [table,fillinred/.list={(3,3 to 6,12),(10,3 to 10,12),(7,13 to 9,18),(3,19 to 6,27),(10,19 to 10,27)}]{
            &&0&0&0&0&0&0&0&0&0&0&1&1&1&1&1&1&0&0&0&0&0&0&0&0&0\\
            &0&1&2&3&4&5&6&7&8&9&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24&25\\
            1&1&1&2&3&4&5&6&7&8&9&10&10&11&12&13&14&15&16&17&18&19&20&21&22&23&24\\
            1&2&2&2&3&4&5&6&7&8&9&10&10&10&11&12&13&14&15&16&17&18&19&20&21&22&23\\
            1&3&3&3&3&4&5&6&7&8&9&10&10&10&10&11&12&13&14&15&16&17&18&19&20&21&22\\
            1&4&4&4&4&4&5&6&7&8&9&10&10&10&10&10&11&12&13&14&15&16&17&18&19&20&21\\
            0&5&4&4&4&4&4&5&6&7&8&9&10&11&11&11&11&12&12&13&14&15&16&17&18&19&20\\
            0&6&5&4&4&4&4&4&5&6&7&8&9&10&11&12&12&12&12&12&13&14&15&16&17&18&19\\
            0&7&6&5&4&4&4&4&4&5&6&7&8&9&10&11&12&13&12&12&12&13&14&15&16&17&18\\
            1&8&7&6&5&5&5&5&5&5&6&7&7&8&9&10&11&12&13&13&13&13&14&15&16&17&18\\
        };
        \foreach \x in {2,6,9}
        {
            \draw 
            ([xshift=-.125\pgflinewidth]mat-\x-1.south west) --   
            ([xshift=-.125\pgflinewidth]mat-\x-27.south east);
        }
        \begin{scope}[shorten >=7pt,shorten <= 7pt]
            \draw[->]  (mat-2-2.center) -- (mat-2-3.center);
            \draw[->]  (mat-2-3.center) -- (mat-2-4.center);
            \draw[->]  (mat-2-4.center) -- (mat-3-5.center);
            \draw[->]  (mat-3-5.center) -- (mat-4-6.center);
            \draw[->]  (mat-4-6.center) -- (mat-5-7.center);
            \draw[->]  (mat-5-7.center) -- (mat-6-8.center);
            \draw[->]  (mat-6-8.center) -- (mat-7-9.center);
            \draw[->]  (mat-7-9.center) -- (mat-8-10.center);
            \draw[->]  (mat-8-10.center) -- (mat-9-11.center);
            \draw[->]  (mat-9-11.center) -- (mat-10-12.center);
        \end{scope}

    \end{tikzpicture}
\end{document}

関連情報