# How to extend class diagram box in pgf-umlcd?

I have this code -

  \documentclass{article}
\usepackage{pgf-umlcd}
\begin{document}
\begin{tikzpicture}
\begin{interface}{SG}{0,0}
\operation{addWord(addWork: String) : String}
\operation{generateSentence(): String}
\end{interface}
\end{tikzpicture}
\end{document}


The output of this code looks like -

Here the String is moved to new line which I don't want. How can I write these in one line?

• Might I suggest that you not include line numbers? Because if we want to test and modify your code, then we first have to remove them. – Torbjørn T. Aug 8 '17 at 16:31

## 1 Answer

You can modify the text width by adding the optional argument to the interface environment.

\documentclass{article}
\usepackage{pgf-umlcd}
\begin{document}
\begin{tikzpicture}
\begin{interface}[text width=6cm]{SG}{0,0}
\operation{addWord(addWork: String) : String}
\operation{generateSentence(): String}
\end{interface}
\end{tikzpicture}
\end{document}


If one wants these boxes to have the width of the longest line of text, a little bit more work is needed. The pgf-umlcd package sets text width=5cm in the style of the boxes, so the style has to be redefined to remove that. The original definition is

\tikzstyle{umlcd style class}=[rectangle split, rectangle split parts=3,
every text node part/.style={text centered},
draw, minimum height=2em, umlcolor, minimum width=2cm, text width=5cm,
minimum height=1cm, node distance=2cm]


which you can compare to the code below.

In addition, the \operation macro uses \newline for line breaks, which doesn't work when text width is not used in the node style, so I used xpatch to replace the \newline with \\, which does work.

\documentclass{article}
\usepackage{pgf-umlcd}
\usepackage{xpatch}

\tikzset{
umlcd style class/.style={ % redefine this style
rectangle split,
rectangle split parts=3,
draw,
minimum height=2em,
umlcolor,
minimum width=2cm,
align=left,
minimum height=1cm,
node distance=2cm,
every text node part/.append style={align=center}
}
}

% patch the \operation macro to use \\ instead of \newline
\xpatchcmd{\operation}{\newline}{\\}{}{}

\begin{document}
\begin{tikzpicture}
\begin{interface}{SG}{0,0}
\operation{addWord(addWork: String) : String}
\operation{generateSentence(): String}
\end{interface}
\end{tikzpicture}

\end{document}


Alternatively one can patch (or redefine) a couple of commands to calculate a text width based on the longest entry in the interface. Here I show an example only for the interface environment and \operation macro, if this is something that would be desirable in general, it would be better if the package itself was updated, to include similar features for the various environments and macros. Feature requests can be posted at https://github.com/xuyuan/pgf-umlcd/issues, though there hasn't been much activity in that repository lately.

The code below produces the following output, note that the width of the interface depends on the width of the longest line of text in it. No manual adjustments are made.

\documentclass[border=5mm]{standalone}
\usepackage{pgf-umlcd}

\usepackage{xpatch}
% at start of interface, set \umlcdInterfaceWidth to the longest line in header
\xapptocmd{\interface}{%
\pgfmathsetmacro{\umlcdInterfaceWidth}{%
max(width("$<<$interface$>>$"),
1.15*width("\umlcdClassName")) % 1.15 needed to account for bold text
}
}{}{}

% for every \operation, update \umlcdInterfaceWidth
\xapptocmd{\operation}{%
\pgfmathsetmacro{\umlcdInterfaceWidth}{max(\umlcdInterfaceWidth,1.02*width("#2"))}%
}{}{}

\tikzset{
% update the style class to use the calculated \umlcdInterfaceWidth
umlcd style class/.append style={
text width=\umlcdInterfaceWidth
}
}

\begin{document}
\begin{tikzpicture}
\begin{interface}{SG}{0,0}
\operation{addWord(addWork: String) : String}
\operation{generateSentence(): String}
\end{interface}

\begin{interface}{SG}{0,-2.5}
\operation{addWord}
\operation{generateSentence()}
\end{interface}

\begin{interface}{FOOBARBAZBARFOO}{6,0}
\operation{addWord}
\operation{generate}
\end{interface}

\begin{interface}{FOO}{5,-2.5}
\operation{add}
\operation{gen}
\end{interface}
\end{tikzpicture}

\end{document}

• I accept your answer. But, problem is, if the method name is more larger the text width must be modified. If you give a general solution that confirms edition of width one time, is better. – alhelal Aug 11 '17 at 9:55
• @BandaMuhammadAlHelal Had to look at what the package does, but see updated answer. – Torbjørn T. Aug 12 '17 at 11:38
• You can add a solution like this text width=\lengthoftext{some text text ....} – alhelal Oct 10 '17 at 3:00
• @alhelal I added another possibility. For individual environments you can use \begin{interface}[text width=width("some text text...")]{ss}{0,0}, though that's not much better than an explicit length. – Torbjørn T. Oct 10 '17 at 10:40