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}