6

I want to be able to define an arbitrary TikZ dash pattern and apply it in the options to a \node. If I use a macro to hold the definition, I get an error:

./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        ]

MWE

\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}

I've tried a token list and various \edef approaches, but the result is the same. Seems like it should be simple, and maybe it is, but I've missed the correct incantation.

  • 3
    You need dash pattern/.expand once= – percusse Apr 4 '18 at 18:00
  • Thank you. I keep discovering hitherto unexpected depths to TikZ. I don't think I would have found .expand once on my own. Thanks again. If you make that into an answer I will accept it. – sgmoye Apr 4 '18 at 18:12
6

It's better to define a style:

\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}

enter image description here

  • I was coming to the same conclusion. However, I require to be able to specify the pattern on the fly, something like this: \def\mkcustomdash#1{% \tikzset{custom dash/.style={dash pattern=#1}} } but that fails with the same error. And I'm still curious why a simple replacement macro causes this error. – sgmoye Apr 4 '18 at 17:59
  • @sgmoye If you use a dash pattern on the fly, use the dirrect syntax in the draw command. The point to define a general dash pattern is to use the same one at different points keeping consitancy, the on the fly is useless then. – Tarass Apr 4 '18 at 19:18
  • @sgmoye Not sure what you mean by “on the fly”. – egreg Apr 4 '18 at 20:34
  • @egreg For instance, I'd like to use keyval to say custompattern = on 3pt off 3pt on 6pt on 3pt, assign that to a macro (say, \mypattern) and use it in this way: dash pattern=\mypattern. percusse pointed my attention to expand once which, in turn, directed me to page 890 of the TikZ user manual. A good day, in all: I learned something. Thanks. My MWE (thanks for the edit) was, perhaps, too 'M'. – sgmoye Apr 4 '18 at 20:51

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.