有什么办法可以控制 tikz pgf-umlsd 图内的边距?
我正在使用以下代码,并且已经可以使用 删除缩进\noindent
。但图表左侧仍有一些未使用的空间。
\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}
编辑:为了遵守社区规则,我用编译的 MWE 替换了我的代码示例。截屏我原来的例子仍然可用这里,如果您也想查看代码,请查看该问题的编辑历史。
答案1
基本问题是软件包代码没有注意避免引入虚假空格。这是一个错误,应该修复。
通常情况下,我建议修补相关命令。但是,我不确定如何修补类似
\newenvironment{sequencediagram}{
这样我们得到
\newenvironment{sequencediagram}{%
无论如何,*很多需要补丁数。%
定义的前 5 行中至少需要5 个补丁sequencediagram
。
可以简单地用新的定义替换旧定义。下面修复了间距问题(并更新了 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}}
这很有帮助,但并不能完全消除问题,因为环境中的命令可能也会引入空格sequencediagram
。如果我使用一个空环境,我现在会得到一个框,就像我刚刚创建一个\fbox{}
没有内容的空框一样大。但是一旦我替换内容,我又会得到一些虚假的空间:
要解决这个问题,您需要修补或重新定义所有相关的宏以删除空格。
\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}
答案2
另一个选择是修补sequencediagram
环境。
它不太好,一旦原包装修好就会破损,但您现在只需要添加几行,然后只需要删除它们;-)
在之前添加以下内容\begin{document}
:
\let\oldsequencediagram\sequencediagram
\let\oldendsequencediagram\endsequencediagram
\renewenvironment{sequencediagram}{%
\hspace*{-7.8mm}\oldsequencediagram%
}{%
\oldendsequencediagram\hspace*{-.5mm}%
}
笔记:我通过反复试验找到了这些值。也许使用其他单位指定负空间更为明智。在我的文档中,我使用 的左空间-8.6mm
。