3

TikZ is able to define parametrized styles using arguments like #1. If used in \newcommand or \newenvironment, they mix up with the arguments of the command. The effect is so severe that the following example will not even compile, but cause TeX to stall. Is there a way to prevent command argument expansion and delay it to be handled by TikZ?

\documentclass{standalone}
\usepackage{tikz}
\newenvironment{fancypicture}[1]{
  \begin{tikzpicture}[
      fancy/.style={circle, draw, fill=#1}
      % The line above here poses the problem.
      % Instead of \newenvironment, this #1 shall be left to be handled by TikZ.
    ]
    \node [fancy=red] at (0, 2) {#1};
}{
  \end{tikzpicture}
}

\begin{document}
\begin{fancypicture}
  \node [draw] {s};
\end{fancypicture}
\end{document}

I know that it would help to put the style declaration in a top-level \tikzset. But that would make it global to all pictures in the document, which I want to avoid.

5

This is discussed in

What is the meaning of double pound symbol (number sign, hash character) ##1 in an argument?

\documentclass{article}
\usepackage{tikz}
\newenvironment{fancypicture}[1][]{
  \begin{tikzpicture}[
      fancy/.style={circle, draw, fill=##1}
    ]
    \draw[style=help lines] (0cm,0cm) grid[step=1cm] (2cm,2cm);
    \node [fancy=red] at (0,1cm) {#1};
}{
  \end{tikzpicture}
}

\begin{document}
\begin{fancypicture}[A]
  \node[draw] at (1cm,1cm) {s};
\end{fancypicture}
\end{document}

enter image description here

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.