为自定义 TikZ 虚线图案定义宏时出现问题

为自定义 TikZ 虚线图案定义宏时出现问题

我希望能够定义任意的 TikZ 虚线图案并将其应用于 的选项中\node。如果我使用宏来保存定义,则会收到错误:

./dashissue.tex:21: Use of \tikz@scandashon doesn't match its definition.
\pgfkeys@code ...kz@scandashon \pgfutil@gobble #1o
                                                  \@nil \edef \tikz@temp {{\...
l.21        ]

平均能量损失

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usepackage{ifthen}

\usetikzlibrary{shapes.multipart}

\def\customdash{on 2pt off 3pt on 4pt off 4pt}

\begin{document}

\tikz\node[%
    draw,
    rectangle split,
    rectangle split parts=2,
    %% This works:
    %dash pattern=on 2pt off 3pt on 4pt off 4pt
    %% This does not work:
    dash pattern=\customdash
    ]
    {This is on the top,\nodepart{two}and this is on the bottom};

\end{document}

我尝试了 token 列表和各种\edef方法,但结果是一样的。看起来应该很简单,也许确实如此,但我错过了正确的咒语。

答案1

最好定义一种风格:

\documentclass{article}

\usepackage{tikz}

\usetikzlibrary{shapes.multipart}

\tikzset{
  custom dash/.style={dash pattern=on 2pt off 3pt on 4pt off 4pt},
}

\begin{document}

\tikz\node[
    draw,
    rectangle split,
    rectangle split parts=2,
    custom dash,
    ]
    {This is on the top,\nodepart{two}and this is on the bottom};

\end{document}

在此处输入图片描述

相关内容