7

Why the shape (sB) is unknown in the following code ? How to avoid this ?

\documentclass[]{standalone}
\usepackage{tikz,xparse}
\usetikzlibrary{calc}

\tikzset{pics/.cd,
    % Marque croix
    Cross/.style args={#1 and #2}{%
        code = {%
        \draw[#2,rotate=45,scale=1.4] (0,#1 pt) -- (0,-#1 pt) ;
        \draw[#2,rotate=-45,scale=1.4] (0,#1 pt) -- (0,-#1 pt) ;
        }
    },
    Cross/.default={2 and black}
}

\NewDocumentCommand{\Segment}{mO{}}{%
    \begin{scope}[#2]
    \path    (0,0) coordinate (sA) pic {Cross={1.5 and gray}}
        -- (#1,0) coordinate (sB) pic {Cross={1.5 and gray}} ;
    \draw (sA)--(sB) ; % Can't reuse (sB)
    \end{scope}
}


\begin{document}
    \begin{tikzpicture}
    \Segment{1.5}
    \end{tikzpicture}
\end{document}
Tarass
  • 16,912
  • 2
    For your second question on avoiding: \path (0,0) coordinate (sA) pic {Cross={1.5 and gray}}; \path (#1,0) coordinate (sB) pic {Cross={1.5 and gray}} ; in the definition of \Segment –  Nov 11 '14 at 07:55
  • @HarishKumar is not possible to use two or more pics on same path? – Ignasi Nov 11 '14 at 08:00
  • @Ignasi Why not? Try \draw (0,0) -- pic[pos=0] {Cross={1.5 and gray}} pic[pos=1] {Cross={1.5 and gray}} (#1,0); for example. –  Nov 11 '14 at 08:05
  • @HarishKumar Mixing your to remarks, on can do : \draw (0,0) coordinate (sA) -- (#3,0) coordinate (sB) \foreach \i in {0,1} {pic[pos=\i] {Cross={1.5 and gray}}}; – Tarass Nov 11 '14 at 08:14
  • @HarishKumar OK, forget my question. Mark solved it. – Ignasi Nov 11 '14 at 08:23

1 Answers1

5

This appears to be a bug. The internal command \iftikz@node@is@pic is not reset after a pic on a path so any subsequent nodes (coordinates are nodes) are parsed as pics and consequently nothing is done.

A temporary fix (and not guaranteed to work in every possible use case) is to put the following (with appropriate category code changes) in the preamble:

\def\tikz@do@after@node{\tikz@node@is@picfalse\tikz@scan@next@command}
Mark Wibrow
  • 70,437
  • I was not able to find a bug report, so I did it here : https://sourceforge.net/p/pgf/bugs/341/ – Kpym Nov 27 '14 at 17:56
  • There is another workaround to this bug that I have seen somwehre on TeX.SX (the author was @percusse I think), we can isolate the pic in a group like this {pic[..]{..}}. For exemple in OP we can do \path (0,0) coordinate (sA) {pic{Cross={1.5 and gray}}} -- (#1,0) coordinate (sB) pic{Cross={1.5 and gray}};. – Kpym Jan 14 '15 at 13:13