I want to define an environment for the tikzpicture environment which defines tikz specific commands.
Here's an abbreviated example for a UML environment which defines a \Class command):
\newenvironment{UMLDiagram}{%
\newcommand{\Class}[3]{\node[class](##1){##2\nodepart{second}{##3}};}%
\begin{tikzpicture}[%
class/.style={draw, rectangle split, rectangle split parts=2, align=left}%
]%
}{%
\end{tikzpicture}%
}
I wanted to use this the following way:
\begin{UMLDiagram}
\Class{id}{ClassName}{attributeA=a\\attributeB=b}
\end{UMLDiagram}
This---not entirely unexpected---throws an error:
! Undefined control sequence tizk@invoke@collected@onpath ...mmand \tikz@temp \pgf@stop (...)
However, a command with a single argument did not fail.
So, how do you do this? Or, on a related note, does tikz provide a mechanism for tikz specific command definitions?
MWE
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{shapes}
\newenvironment{UMLDiagram}{%
\newcommand{\Class}[3]{\node[class](##1){##2\nodepart{second}{##3}};}%
\begin{tikzpicture}[%
class/.style={draw, rectangle split, rectangle split parts=2, align=left}%
]%
}{%
\end{tikzpicture}%
}
\begin{document}
\begin{UMLDiagram}
\Class{id}{ClassName}{attributeA=a\\attributeB=b}
\end{UMLDiagram}
\end{document}
Follow Up
The solution that worked for me was the use of a tabular environment within the node label as provided here.
\newenvironment{UMLDiagram}{%
\newcommand{\Class}[3]{
\node[class](##1){%
##2%
\nodepart{second}%
\begin{tabular}{l}##3\end{tabular}%
};
}%
\begin{tikzpicture}[%
class/.style={draw, rectangle split, rectangle split parts=2, align=left}%
]%
}{%
\end{tikzpicture}%
}
The align=left and \\ pattern would have been favorable but it did not work in this case.
\Class{id}{Name}{attributeA=a\\attributeB=b}without any width workarounds. – FK82 Jul 08 '14 at 09:03