在 tikz-cd 中自动显示节点标签

在 tikz-cd 中自动显示节点标签

CoDi 软件包具有一项prompter功能,启用后,它会将每个节点放入一个框中,并用其对象名称进行标记(请参阅CoDi 手册):

在此处输入图片描述

是否可以做tikz-cd类似的事情,根据网格位置标记图中的非空节点?


例子:这样的选项应该可以改变图表

在此处输入图片描述 (来源:

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[row sep={4.5em,between origins}, column sep={4.5em,between origins}, ampersand replacement=\&]
    \&\&\&\&
    \bullet
    \arrow[rrrd]
    \&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrru]
    \&\&\&\&\&\&
    \bullet
    \arrow[rddd]
    \&
    \\
    \&\&\&
    \bullet
    \arrow[rdd]
    \arrow[ruu]
    \&\&\&\&\&
    \\
    \bullet
    \arrow[rrru]
    \arrow[ruu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&\&
    \bullet
    \arrow[rrrr]
    \&\&\&\&
    \bullet
    \\
    \&\&
    \bullet
    \arrow[rrrr]
    \arrow[luuuu]
    \&\&\&\&
    \bullet
    \arrow[rddd,bend right]
    \arrow[rddd,bend left]
    \arrow[ruuuu]
    \&\&
    \\
    \bullet
    \arrow[rrrd]
    \arrow[uuu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&
    \bullet
    \arrow[ruuu]\&\&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrrd]
    \arrow[luu]
    \arrow[ruuu]
    \&\&\&\&\&\&
    \bullet
    \arrow[ruuuu]
    \&
    \\
    \&\&\&\&
    \bullet
    \arrow[luu,bend right]
    \arrow[luu,bend left]
    \arrow[rrru]
    \&\&\&\&
\end{tikzcd}
\end{document}

) 放入图中:

在此处输入图片描述

(来源:

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath}
\begin{document}
\begin{tikzcd}[row sep={4.5em,between origins}, column sep={4.5em,between origins},ampersand replacement=\&]
    \&\&\&\&
    \boxed{\bullet}_{\text{1-5}}
    \arrow[rrrd]
    \&\&\&\&
    \\
    \&
    \boxed{\bullet}_{\text{2-2}}
    \arrow[rrru]
    \&\&\&\&\&\&
    \boxed{\bullet}_{\text{2-8}}
    \arrow[rddd]
    \&
    \\
    \&\&\&
    \boxed{\bullet}_{\text{3-4}}
    \arrow[rdd]
    \arrow[ruu]
    \&\&\&\&\&
    \\
    \boxed{\bullet}_{\text{4-1}}
    \arrow[rrru]
    \arrow[ruu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&\&
    \boxed{\bullet}_{\text{5-5}}
    \arrow[rrrr]
    \&\&\&\&
    \boxed{\bullet}_{\text{5-9}}
    \\
    \&\&
    \boxed{\bullet}_{\text{6-3}}
    \arrow[rrrr]
    \arrow[luuuu]
    \&\&\&\&
    \boxed{\bullet}_{\text{6-7}}
    \arrow[rddd,bend right]
    \arrow[rddd,bend left]
    \arrow[ruuuu]
    \&\&
    \\
    \boxed{\bullet}_{\text{7-1}}
    \arrow[rrrd]
    \arrow[uuu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&
    \boxed{\bullet}_{\text{8-4}}
    \arrow[ruuu]\&\&\&\&\&
    \\
    \&
    \boxed{\bullet}_{\text{9-2}}
    \arrow[rrrd]
    \arrow[luu]
    \arrow[ruuu]
    \&\&\&\&\&\&
    \boxed{\bullet}_{\text{9-8}}
    \arrow[ruuuu]
    \&
    \\
    \&\&\&\&
    \boxed{\bullet}_{\text{10-5}}
    \arrow[luu,bend right]
    \arrow[luu,bend left]
    \arrow[rrru]
    \&\&\&\&
\end{tikzcd}
\end{document}

答案1

这不是我的答案,而是一位朋友的答案。重要的信息似乎是行和列索引分别存储在 TeX 计数\pgfmatrixcurrentrow和中\pgfmatrixcurrentcolumn。然后可以使用它们来自动标记非空单元格。

\documentclass{article}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzcd}[row sep={4.5em,between origins}, 
    column sep={4.5em,between origins}, ampersand replacement=\&,
    nodes in empty cells=false,
    cells={
    nodes={draw,
        execute at end node={%
            \makebox[0pt][l]{$~~{}_{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}$}}
        }
        }
        ]
    \&\&\&\&
    \bullet
    \arrow[rrrd]
    \&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrru]
    \&\&\&\&\&\&
    \bullet
    \arrow[rddd]
    \&
    \\
    \&\&\&
    \bullet
    \arrow[rdd]
    \arrow[ruu]
    \&\&\&\&\&
    \\
    \bullet
    \arrow[rrru]
    \arrow[ruu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&\&
    \bullet
    \arrow[rrrr]
    \&\&\&\&
    \bullet
    \\
    \&\&
    \bullet
    \arrow[rrrr]
    \arrow[luuuu]
    \&\&\&\&
    \bullet
    \arrow[rddd,bend right]
    \arrow[rddd,bend left]
    \arrow[ruuuu]
    \&\&
    \\
    \bullet
    \arrow[rrrd]
    \arrow[uuu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&
    \bullet
    \arrow[ruuu]\&\&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrrd]
    \arrow[luu]
    \arrow[ruuu]
    \&\&\&\&\&\&
    \bullet
    \arrow[ruuuu]
    \&
    \\
    \&\&\&\&
    \bullet
    \arrow[luu,bend right]
    \arrow[luu,bend left]
    \arrow[rrru]
    \&\&\&\&
\end{tikzcd}
\end{document}

在此处输入图片描述

答案2

这是 user237902 答案的一个很小的变化:使用\tikzcdset,可以创建密钥prompterprompter all

\tikzcdset{%
    prompter/.code={%
        \tikzset{%
            nodes in empty cells=false,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~~{}_{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}$}}}}
        }%
    }%
}%
\tikzcdset{%
    prompter all/.code={%
        \tikzset{%
            nodes in empty cells=true,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~~{}_{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}$}}}}
        }%
    }%
}%

能够\begin{tikzcd}以 的形式调用此功能\begin{tikzcd}[prompter]。以下是示例:

\documentclass{article}
\usepackage{tikz-cd}
\tikzcdset{%
    prompter/.code={%
        \tikzset{%
            nodes in empty cells=false,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~~{}_{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}$}}}}
        }%
    }%
}%
\tikzcdset{%
    prompter all/.code={%
        \tikzset{%
            nodes in empty cells=true,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~~{}_{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}$}}}}
        }%
    }%
}%
\begin{document}
\begin{tikzcd}[row sep={4.5em,between origins},column sep={4.5em,between origins}, ampersand replacement=\&,prompter]%
    \&\&\&\&
    \bullet
    \arrow[rrrd]
    \&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrru]
    \&\&\&\&\&\&
    \bullet
    \arrow[rddd]
    \&
    \\
    \&\&\&
    \bullet
    \arrow[rdd]
    \arrow[ruu]
    \&\&\&\&\&
    \\
    \bullet
    \arrow[rrru]
    \arrow[ruu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&\&
    \bullet
    \arrow[rrrr]
    \&\&\&\&
    \bullet
    \\
    \&\&
    \bullet
    \arrow[rrrr]
    \arrow[luuuu]
    \&\&\&\&
    \bullet
    \arrow[rddd,bend right]
    \arrow[rddd,bend left]
    \arrow[ruuuu]
    \&\&
    \\
    \bullet
    \arrow[rrrd]
    \arrow[uuu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&
    \bullet
    \arrow[ruuu]\&\&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrrd]
    \arrow[luu]
    \arrow[ruuu]
    \&\&\&\&\&\&
    \bullet
    \arrow[ruuuu]
    \&
    \\
    \&\&\&\&
    \bullet
    \arrow[luu,bend right]
    \arrow[luu,bend left]
    \arrow[rrru]
    \&\&\&\&
\end{tikzcd}
\end{document}

生成 在此处输入图片描述

而相同的代码,但prompter替换prompter all在此处输入图片描述 当然,后者相当丑陋,但根据图表,此功能可能很有用。

进一步的美学变化是将键更改prompterprompter all以下内容:

\tikzcdset{%
    prompter/.code={%
        \tikzset{%
            nodes in empty cells=false,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~{}_{%
                \smash{%
                    \raisebox{-0.75em}{
                        \colorbox{OIblue}{%
                            \textcolor{white}{%
                                \textbf{\textsf{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}}%
                            }%
                        }%
                    }%
                }%
            }$}}}}%
        }%
    }%
}%
\tikzcdset{%
    prompter all/.code={%
        \tikzset{%
            nodes in empty cells=true,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~{}_{%
                \smash{%
                    \raisebox{-0.75em}{
                        \colorbox{OIblue}{%
                            \textcolor{white}{%
                                \textbf{\textsf{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}}%
                            }%
                        }%
                    }%
                }%
            }$}}}}%
        }%
    }%
}%

(第一个)给出 在此处输入图片描述

以下是上述内容的 MWE:

\documentclass{article}
\usepackage{tikz-cd}
\usepackage{amsmath}
\usepackage{xcolor}
\definecolor{OIblue}{RGB}{0,114,178}
\tikzcdset{%
    prompter/.code={%
        \tikzset{%
            nodes in empty cells=false,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~{}_{%
                \smash{%
                    \raisebox{-0.75em}{
                        \colorbox{OIblue}{%
                            \textcolor{white}{%
                                \textbf{\textsf{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}}%
                            }%
                        }%
                    }%
                }%
            }$}}}}%
        }%
    }%
}%
\tikzcdset{%
    prompter all/.code={%
        \tikzset{%
            nodes in empty cells=true,cells={nodes={draw,execute at end node={\makebox[0pt][l]{$~{}_{%
                \smash{%
                    \raisebox{-0.75em}{
                        \colorbox{OIblue}{%
                            \textcolor{white}{%
                                \textbf{\textsf{\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}}%
                            }%
                        }%
                    }%
                }%
            }$}}}}%
        }%
    }%
}%
\begin{document}
\begin{tikzcd}[row sep={4.5em,between origins},column sep={4.5em,between origins}, ampersand replacement=\&,prompter]%
    \&\&\&\&
    \bullet
    \arrow[rrrd]
    \&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrru]
    \&\&\&\&\&\&
    \bullet
    \arrow[rddd]
    \&
    \\
    \&\&\&
    \bullet
    \arrow[rdd]
    \arrow[ruu]
    \&\&\&\&\&
    \\
    \bullet
    \arrow[rrru]
    \arrow[ruu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&\&
    \bullet
    \arrow[rrrr]
    \&\&\&\&
    \bullet
    \\
    \&\&
    \bullet
    \arrow[rrrr]
    \arrow[luuuu]
    \&\&\&\&
    \bullet
    \arrow[rddd,bend right]
    \arrow[rddd,bend left]
    \arrow[ruuuu]
    \&\&
    \\
    \bullet
    \arrow[rrrd]
    \arrow[uuu]
    \&\&\&\&\&\&\&\&
    \\
    \&\&\&
    \bullet
    \arrow[ruuu]\&\&\&\&\&
    \\
    \&
    \bullet
    \arrow[rrrd]
    \arrow[luu]
    \arrow[ruuu]
    \&\&\&\&\&\&
    \bullet
    \arrow[ruuuu]
    \&
    \\
    \&\&\&\&
    \bullet
    \arrow[luu,bend right]
    \arrow[luu,bend left]
    \arrow[rrru]
    \&\&\&\&
\end{tikzcd}
\end{document}

答案3

如果您提供 MWE,可能会有所帮助。我想像我的示例一样,根据节点的坐标来标记节点,对吗?节点 4-(-4) 的坐标为 4 和 -4?

\documentclass[border=1.21cm]{standalone}
\usepackage{tikz}

\begin{document}
    
    \begin{tikzpicture}
        
        \foreach\x/\y in {0/0,2/2,4/-4}
        \draw[] (\x,\y)  node[fill=white,draw=black,rectangle,inner sep=2pt] {\x-\y};
        
    \end{tikzpicture}
    
\end{document}

在此处输入图片描述

相关内容