# How do you define a command for use in a tikzpicture environment?

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}


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.

-

The \\ is the reason for the error, if you remove it, your code succeed:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{shapes}

\newenvironment{UMLDiagramm}{%
\newcommand{\Class}[3]{\node[class](##1){\underline{##2}\nodepart{second}{##3}};}%
\begin{tikzpicture}[%
class/.style={draw, rectangle split, rectangle split parts=2, align=left}%
]%
}{%
\end{tikzpicture}%
}

\begin{document}

\begin{UMLDiagramm}
\Class{id}{ClassName}{attributeA=a,attributeB=b}% <=== replaced \\ with ,
\end{UMLDiagramm}

\end{document}


If you define a text width, you can use a newline command:

\documentclass{minimal}

\usepackage{tikz}
\usetikzlibrary{shapes}

\newenvironment{UMLDiagramm}{%
\newcommand{\Class}[3]{\node[class](##1){\underline{##2}\nodepart{second}{##3}};}%
\begin{tikzpicture}[%
class/.style={draw, rectangle split, rectangle split parts=2, align=left,
}%
]%
}{%
\end{tikzpicture}%
}

\begin{document}

\begin{UMLDiagramm}
\Class{id}{ClassName}{attributeA=a\\attributeB=b}
\end{UMLDiagramm}

\end{document}

-
Ah, I see. Do you know any way to force the line break for an arbitrary width? – FK82 Jul 7 '14 at 10:56
Perhaps you find the solution in other questions: tex.stackexchange.com/questions/24372/… – knut Jul 7 '14 at 18:37
Not in those answers as far as I can see. What I want is \Class{id}{Name}{attributeA=a\\attributeB=b} without any width workarounds. – FK82 Jul 8 '14 at 9:03