3

Is there any way to control the margin within a tikz pgf-umlsd diagram?

I'm using the following code and could already remove the indent by using \noindent. But still there is some unused space to the left within the diagram.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{pgf-umlsd}
\begin{document}
\lipsum
\noindent%
\fbox{%
\begin{sequencediagram}%
    \newthread{client}{Some Client}
    \newinst{server}{Some Server}
    \newinst{third}{Some Third Service}
    \begin{call}{client}{do}{server}{}
    \end{call}
    \begin{messcall}{server}{notify}{third}
    \end{messcall}
\end{sequencediagram}%
}
\lipsum
\end{document}

enter image description here

Edit: To follow the community rules, I've replaced my code example by a compiling MWE. The screenshot of my original example is still available here, if you also want to see the code, please take a look in the edit history of this question.

  • With \resizebox{w}{h}{content} from graphicx at leased its possible to keep the diagram within the column, but still the margin is there and its no 100% nice solution. – white_gecko Jan 21 '16 at 17:06
  • Careless line ending costs lives! – cfr Jan 22 '16 at 2:03
3

The basic problem is that the package code does not take care to avoid the introduction of spurious spaces. This is a bug and should be fixed.

Normally, I'd suggest patching the relevant commands. However, I'm not sure how to patch something like

\newenvironment{sequencediagram}{

so that we get

\newenvironment{sequencediagram}{%

In any case, a *lot of patches would be required. At least 5 % are required in the first 5 lines of the definition of sequencediagram.

It is possible to simply replace the definition with a new one. The following fixes the spacing problem (and updates the syntax for TikZ 3.0):

\renewenvironment{sequencediagram}{%
  % declare layers
  \pgfdeclarelayer{umlsd@background}%
  \pgfdeclarelayer{umlsd@threadlayer}%
  \pgfsetlayers{umlsd@background,umlsd@threadlayer,main}%
%
  \begin{tikzpicture}
    [
      sequence/.style={coordinate},
      inststyle/.style={rectangle, draw, anchor=west, minimum height=0.8cm, minimum width=1.6cm, fill=white, drop shadow={opacity=1,fill=black}},
      blockstyle/.style={anchor=north west},
      blockcommentstyle/.style={anchor=north west, font=\small},
      dot/.style={inner sep=0pt,fill=black,circle,minimum size=0.2pt},
    ]
    \setlength{\unitlength}{1cm}
    \ifpgfumlsdroundedcorners
    \tikzset{%
      inststyle/.style+={rounded corners=3mm}}
    \fi
    \global\def\unitfactor{0.6}
    \global\def\threadbias{center}
    % reset counters
    \setcounter{preinst}{0}
    \setcounter{instnum}{0}
    \setcounter{threadnum}{0}
    \setcounter{seqlevel}{0}
    \setcounter{callevel}{0}
    \setcounter{callselflevel}{0}
    \setcounter{blocklevel}{0}

    % origin
    \node[coordinate] (inst0) {};
}{
  \begin{pgfonlayer}{umlsd@background}
    \ifnum\c@instnum > 0
    \foreach \t [evaluate=\t] in {1,...,\theinstnum}{
      \draw[dotted] (inst\t) -- ++(0,-\theseqlevel*\unitfactor-2.2*\unitfactor);
    }
    \fi
    \ifnum\c@threadnum > 0
    \foreach \t [evaluate=\t] in {1,...,\thethreadnum}{
      \path (thread\t)+(0,-\theseqlevel*\unitfactor-0.1*\unitfactor) node (threadend) {};
      \tikzstyle{threadstyle}+=[threadcolor\t]
      \drawthread{thread\t}{threadend}
    }
    \fi
  \end{pgfonlayer}
\end{tikzpicture}}

This helps, but it does not completely eliminate the problem because spaces are presumably being introduced by the commands within the sequencediagram environment as well. If I use an empty environment, I now get a box as large as if I'd just made an empty \fbox{} with no content. But once I replace the content, I get some spurious space again:

improved spacing for environment with spurious spaces remaining on left

To fix this, you'd need to patch or redefine all of the relevant macros to remove the spaces.

\documentclass{article}
\usepackage{tikz,pgf-umlsd}
\usepackage{kantlipsum}
\makeatletter
\renewenvironment{sequencediagram}{%
  % declare layers
  \pgfdeclarelayer{umlsd@background}%
  \pgfdeclarelayer{umlsd@threadlayer}%
  \pgfsetlayers{umlsd@background,umlsd@threadlayer,main}%
%
  \begin{tikzpicture}
    [
      sequence/.style={coordinate},
      inststyle/.style={rectangle, draw, anchor=west, minimum height=0.8cm, minimum width=1.6cm, fill=white, drop shadow={opacity=1,fill=black}},
      blockstyle/.style={anchor=north west},
      blockcommentstyle/.style={anchor=north west, font=\small},
      dot/.style={inner sep=0pt,fill=black,circle,minimum size=0.2pt},
    ]
    \setlength{\unitlength}{1cm}
    \ifpgfumlsdroundedcorners
    \tikzset{%
      inststyle/.style+={rounded corners=3mm}}
    \fi
    \global\def\unitfactor{0.6}
    \global\def\threadbias{center}
    % reset counters
    \setcounter{preinst}{0}
    \setcounter{instnum}{0}
    \setcounter{threadnum}{0}
    \setcounter{seqlevel}{0}
    \setcounter{callevel}{0}
    \setcounter{callselflevel}{0}
    \setcounter{blocklevel}{0}

    % origin
    \node[coordinate] (inst0) {};
}{
  \begin{pgfonlayer}{umlsd@background}
    \ifnum\c@instnum > 0
    \foreach \t [evaluate=\t] in {1,...,\theinstnum}{
      \draw[dotted] (inst\t) -- ++(0,-\theseqlevel*\unitfactor-2.2*\unitfactor);
    }
    \fi
    \ifnum\c@threadnum > 0
    \foreach \t [evaluate=\t] in {1,...,\thethreadnum}{
      \path (thread\t)+(0,-\theseqlevel*\unitfactor-0.1*\unitfactor) node (threadend) {};
      \tikzstyle{threadstyle}+=[threadcolor\t]
      \drawthread{thread\t}{threadend}
    }
    \fi
  \end{pgfonlayer}
\end{tikzpicture}}
\makeatother
\begin{document}
\kant[1]

\noindent%
\fbox{%
\begin{sequencediagram}%
  \newthread{client}{Some Client}
  \newinst{server}{Some Server}
  \newinst{third}{Some Third Service}
  \begin{call}{client}{do}{server}{}
  \end{call}
  \begin{messcall}{server}{notify}{third}
  \end{messcall}
\end{sequencediagram}%
}

\kant[2]
\end{document}
  • Presumably egreg's package could patch this without needing to overwrite the environment, but the warning on that package makes me reluctant to dabble myself - let alone impose such dabbling on innocent bystanders. – cfr Jan 22 '16 at 2:27
  • Wouldn't it be good to prepare a new release of this package, including this fix? There are quite some forks of the original google code project on github, don't know, which one is authoritative. – white_gecko Jan 22 '16 at 14:21
  • I think the remaining space it caused by the fact, that \newinsthas a default edge distance of [0.2], while \newinst is called from \newthread at the beginning. – white_gecko Jan 22 '16 at 21:11
  • @white_gecko Really, this should be reported to the current maintainer of the package so that the version on CTAN gets corrected. This is clearly a bug and it is not difficult to fix - just a question, mostly, of adding comment signs at the end of lines when required. – cfr Jan 22 '16 at 23:05
0

Another option is to patch the sequencediagram environment.

It is not very nice and will break once the original package is fixed, but you only have to add a few lines now and only will only have to remove them then ;-)

Add the following before \begin{document}:

\let\oldsequencediagram\sequencediagram
\let\oldendsequencediagram\endsequencediagram
\renewenvironment{sequencediagram}{%
    \hspace*{-7.8mm}\oldsequencediagram%
}{%
    \oldendsequencediagram\hspace*{-.5mm}%
}

Note: I found these values via trial and error. Maybe it is more clever to specify the negative space using other units. In my document, I use a left space of -8.6mm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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