4

I am trying to draw a dashed, shaped arrow. In order to do this, I'm working on a tikz macro.

The routine works by drawing the arrow, then drawing some white filled rectangles on top of the arrow.

I want to be able to handle arrows of various lengths, so I want to calculate the number and spacing of the white rectangles based on the length.

The code works just fine when I have a number for the upper limit. But when I have a calculation for the upper limit, I get "Incomplete \iffalse; all text was ignored".

I would welcome any help that can be provided. Below is the MWE.

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[x=1mm,y=1mm]
\def\w{2.5}
\def\d{.25}
\def\lt{12}
\def\la{6}
\def\s{.25}
\def\l{50}
\def\sw{1.5}
\def\dw{.65}
\def\sw{1.5}
\def\dw{.65}
\def\nd{int((\l-2*\la)/(\sw+\dw)) }
\def\intervals{\pgfmathparse{\nd}\pgfmathresult }
\def\offs{(\l-2*\la)/\nd}
\def\limit{\intervals-1}
%\def\limit{16}  % Uncomment this to see the picture I want


\draw [color=green,fill=green]
  (0,0) -- (\la+\d/2,\w/2)
  .. controls (\la+\d/4,3*\w/8) .. (\la,\s)
  .. controls (\l/2-\lt, \s) .. (\l/2,\w/2)
  .. controls (\l/2+\lt, \s) .. (\l-\la,\s)
  .. controls (\l-\la-\d/4,3*\w/8) ..(\l-\la-\d/2, \w/2)
  -- (\l,0)
  -- (\l-\la-\d/2, - \w/2)
   .. controls (\l-\la-\d/4,-3*\w/8) .. (\l-\la,-\s)
   .. controls (\l/2+\lt, -\s) .. (\l/2,-\w/2)
   .. controls (\l/2-\lt,  -\s) .. (\la,-\s)
   .. controls (\la+\d/4,-3*\w/8) .. (\la,-\w/2)
   -- cycle;

  \foreach \i in {0,1,...,\limit} {
    \fill[color=white] ( {\la+\offs*(.5+\i)-\dw/2}, {-\w/2} ) 
          rectangle ({\la+\offs*(.5+\i)+\dw/2} , \w/2 );
    }

\end{tikzpicture}
\end{document}
  • 1
    foreach doesn't parse math in the limit expressions. Use \number\numexpr\intervals-1\relax – percusse Feb 1 '17 at 22:54
  • I tried this, but then got "Missing number, treated as zero". I had tried this earlier based on some other SX answers. – Carl Sorensen Feb 1 '17 at 22:59
  • It's because \def\intervals{\pgfmathparse{\nd}\pgfmathresult } is not appropriate. IT doesn't store the result it stores the code – percusse Feb 1 '17 at 23:13
  • \number\numexpr\nd-1\relax also gives "Missing number, treated as zero. – Carl Sorensen Feb 1 '17 at 23:30
  • Same problem. It has to hold a number not an expression – percusse Feb 1 '17 at 23:59
3

You should use \pgfmathsetmacro{<macro>}{<expression>} if you want to store the evaluated result of an <expression> in a <macro>:

enter image description here

\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[x=1mm,y=1mm]
\def\w{2.5}
\def\d{.25}
\def\lt{12}
\def\la{6}
\def\s{.25}
\def\l{50}
\def\sw{1.5}
\def\dw{.65}
\def\sw{1.5}
\def\dw{.65}
\def\nd{int((\l-2*\la)/(\sw+\dw)) }
\pgfmathsetmacro{\intervals}{\nd}
\pgfmathsetmacro{\offs}{(\l-2*\la)/\nd}
\pgfmathsetmacro{\limit}{\intervals-1}

\draw [color=green,fill=green]
  (0,0) -- (\la+\d/2,\w/2)
  .. controls (\la+\d/4,3*\w/8) .. (\la,\s)
  .. controls (\l/2-\lt, \s) .. (\l/2,\w/2)
  .. controls (\l/2+\lt, \s) .. (\l-\la,\s)
  .. controls (\l-\la-\d/4,3*\w/8) ..(\l-\la-\d/2, \w/2)
  -- (\l,0)
  -- (\l-\la-\d/2, - \w/2)
   .. controls (\l-\la-\d/4,-3*\w/8) .. (\l-\la,-\s)
   .. controls (\l/2+\lt, -\s) .. (\l/2,-\w/2)
   .. controls (\l/2-\lt,  -\s) .. (\la,-\s)
   .. controls (\la+\d/4,-3*\w/8) .. (\la,-\w/2)
   -- cycle;

  \foreach \i in {0,1,...,\limit} {
    \fill[color=white] ( {\la+\offs*(.5+\i)-\dw/2}, {-\w/2} ) 
          rectangle ({\la+\offs*(.5+\i)+\dw/2} , \w/2 );
    }

\end{tikzpicture}
\end{document}
  • Thanks, Werner. I had not used \pgfmathsetmacro before. It works well. – Carl Sorensen Feb 1 '17 at 23:01

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.