4

I want to set flag from inside of decoration which I want to check outside of the decoration. I set the flag (call \toggletrue{} or \togglefalse{}) but when I check this flag it always false.

For MWE I define a new decoration that do nothing except set the flag. Apply this decoration to the circle. And then check the flag.

\documentclass{standalone}

\usepackage{etoolbox}
\usepackage{tikz}
\usetikzlibrary{decorations}

\makeatletter

\newtoggle{toggletest}

\pgfdeclaredecoration{custom decoration}{initial}{%
  \state{initial}[
    width=+0pt,
    next state=final,
    persistent precomputation={%
      \toggletrue{toggletest}
    }
  ]{}

  \state{final}{}
}

\makeatother

\begin{document}
\begin{tikzpicture}

\draw [
  postaction={%
    decorate,
    decoration={%
      custom decoration
    }
  }
] (0,0) circle (2cm);

\iftoggle{toggletest}{%
  \draw [green] (0,0) circle (1cm);
}{%
  \draw [red] (0,0) circle (1cm);
}

\end{tikzpicture}

\end{document}

It should draw green circle if toggletest is true and red one otherwise. But it draw always red one.

What I am doing wrong?

3

use a global setting:

\pgfdeclaredecoration{custom decoration}{initial}{%
  \state{initial}[
    width=+0pt,
    next state=final,
    persistent precomputation={%
      \global\toggletrue{toggletest}
    }
  ]{}

  \state{final}{}
}

However, a simple \newif would be enough here:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations}

\newif\iftoggletest \toggletestfalse

\pgfdeclaredecoration{custom decoration}{initial}{%
  \state{initial}[
    width=+0pt,
    next state=final,
    persistent precomputation={%
      \global\toggletesttrue
    }
  ]{}
  \state{final}{}
}

\begin{document}
\begin{tikzpicture}

\draw [
  postaction={%
    decorate,
    decoration={%
      custom decoration
    }
  }
] (0,0) circle (2cm);

\iftoggletest
  \draw [green] (0,0) circle (1cm);
\else
  \draw [red] (0,0) circle (1cm);
\fi

\end{tikzpicture}

\end{document}
  • Is there any reason why the use of \newif would be problematic when the file is \input into another document using the facilities of standalone? I can only get it to work if I comment the lines concerning the if in the preamble of the input file. (Obviously I define \newif etc. in the calling document.) I thought that standalone would skip the entire preamble but for some reason \newif\ifmine\minefalse causes errors if it is still present in the preamble. – cfr Apr 30 '14 at 10:21

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.