Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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,
        text width=3cm% <=== added
        }%
    ]%
}{%
    \end{tikzpicture}%
}

\begin{document}

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

\end{document}
share|improve this answer
    
Ah, I see. Do you know any way to force the line break for an arbitrary width? –  FK82 Jul 7 at 10:56
    
Perhaps you find the solution in other questions: tex.stackexchange.com/questions/24372/… –  knut Jul 7 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 at 9:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.