在 tikz 中,如何从样式设置节点的内容?
例如,不要这样写:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes] {
|[name=A-Foo]|A.Foo & |[name=B-Foo]|B.Foo & \dots \\
};
\end{tikzpicture}
\end{document}
我想写这个,但contents
没有选项:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
nc/.style 2 args={name={#1-#2}, content={#1.#2}}
]
\matrix[matrix of nodes] {
|[nc={A}{Foo}]| & |[nc={B}{Foo}]| & \dots \\
};
\end{tikzpicture}
\end{document}
答案1
在 中matrix
,这可以通过对宏进行小的修复来完成\tikz@lib@matrix@end@cell
。
以下解决方案添加了密钥的内容,content
添加了结尾节点(但不是“空”节点。
代码
\documentclass[tikz]{standalone}
%\usepackage{tikz}
\usetikzlibrary{matrix}
\makeatletter
\def\tikz@lib@matrix@end@cell@{%
\pgfkeysvalueof{/tikz/content}%
\iftikz@lib@matrix@plain\else%
\expandafter\egroup\expandafter;\fi}
\tikzset{
content/.initial=,
matrix of nodes with the possibility to set the content/.style={
matrix of nodes,
/utils/exec=\let\tikz@lib@matrix@end@cell\tikz@lib@matrix@end@cell@}}
\makeatother
\begin{document}
\begin{tikzpicture}[nc/.style 2 args={name={#1-#2}, content={#1.#2}}]
\matrix[matrix of nodes with the possibility to set the content] {
|[nc={A}{Foo}]| & |[nc={B}{Foo}]| & \dots \\
};
\end{tikzpicture}
\begin{tikzpicture}
\matrix[
matrix of nodes with the possibility to set the content,
nodes in empty cells, content={\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn}] {
& {} & {} & {} & {} \\
{} & & {} & {} & {} \\
{} & {} & & {} & {} \\
{} & {} & {} & & {} \\
{} & {} & {} & {} & \\};
\end{tikzpicture}
\end{document}
输出