![如何对齐序列图调用中的文本?](https://linux22.com/image/426365/%E5%A6%82%E4%BD%95%E5%AF%B9%E9%BD%90%E5%BA%8F%E5%88%97%E5%9B%BE%E8%B0%83%E7%94%A8%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC%EF%BC%9F.png)
1. 如何对齐或左右移动序列图调用中的文本?例如,在下面的例子中,如何将第 1 条消息稍微向右移动,将第 5 条和第 6 条消息向左移动以适合一页?(我想避免使用 resizebox,因为它也会缩小字体大小)。我在这里简化了文本,shortstack 中的文本是一组相互叠加的方程式。
- 如果序列图超出页面(垂直方向),则会在页面底部被截断。有没有办法让序列图跨越两页?
\documentclass{article}
\usepackage[french,USenglish]{babel}
\usepackage{pgf-umlsd}
\usepackage{tikz}
\begin{document}
\begin{figure}
\centering
% \resizebox{0.45\textwidth}{!}{%
\centering
\begin{sequencediagram}
\tikzstyle {inststyle}+=[bottom color=orange!5, top color=orange!5, rounded corners =1 mm]
\newthread [cyan!10] {u} {$U_{i}$}
\newinst {uid} {$UID_{Ui}$}
\newinst {vm} {$VM_{i}$}
\newinst {spc} {$sPC_{Ui}$}
\newinst {src} {$sRC_{Vi}$}
\newinst {ts} {$TS_{i}$}
\newinst {rto} {$RTO$}
\newinst {ib} {$IB$}
\newinst {as} {$AS$}
\begin{sdblock}{\shortstack[l]{}}{}
\path (inst1.west) -- (inst9.east);
\begin{call}{u}
{\shortstack[l] {
1. Hello, test1 test2 test3 test4\\
1. Hello, test1 test2 test3 test4
}}
{vm}
{\shortstack[l] {
\postlevel
2. Hello, test1 test2 test3\\
2. Hello, test1 test2 test3
}}
\begin{call}{vm}
{\shortstack[l] {
\postlevel
3. Hello, test1 test2 test3 test4 test5 test6\\
3. Hello, test1 test2 test3 test4 test5 test6
}}
{rto}
{\shortstack[l] {
\postlevel
4. Hello, test1 test2 test3 test4 test5\\
4. Hello, test1 test2 test3 test4 test5
}}
\begin{call}{rto}
{\shortstack[l] {
\postlevel
5. Hello, test1 test2 test3 test4 test5 test6 test7\\
5. Hello, test1 test2 test3 test4 test5 test6 test7
}}
{as}
{\shortstack[l] {
\postlevel
6. Hello, test1 test2 test3 test4\\
6. Hello, test1 test2 test3 test4
}}
\end{call}
\end{call}
\end{call}
\end{sdblock}
\end{sequencediagram}
\caption{SequenceDiagram1}
\label{seqdiag}
\end{figure}
\end{document}
答案1
这里的call
环境是从
% from
\begin{call}[height]{caller} {function}{callee} {return}
% to
\begin{call}[height]{caller}[options]{function}{callee}[options]{return}
% ^ first new opt-arg ^ second
两个新的可选参数都接受tikz
节点选项,它们将分别应用于具有文本function
和的节点return
。
为了进一步缩小输出范围,我还在\tikzset{inststyle/.append style={minimum width=1.2cm}}
之后添加了\begin{sequencediagram}
。
您可以使用不同的值和节点选项。
对于第二个问题,没有sequencediagram
使用tikzpicture
,不能跨越多页。
\documentclass{article}
\usepackage[french,USenglish]{babel}
\usepackage{pgf-umlsd}
\usetikzlibrary{positioning}
\usepackage{xpatch}
%\makeatletter
% before
% \begin{call}[height]{caller} {function}{callee} {return}
% 1 2 3 4 5
% now
% \begin{call}[height]{caller}[options]{function}{callee}[options]{return}
% 1 2 3 (new) 4<-3 5<-4 6 (new) 7<-5
\RenewDocumentEnvironment{call}{ O{1} m O{} m m O{} m }{
\tikzset{
umlsd/call function/.style={#3},
umlsd/call return/.style={#6}
}
\ifthenelse{\equal{#2}{#5}}
{\begin{callself}[#1]{#2}{#4}{#7}}
{\begin{callanother}[#1]{#2}{#4}{#5}{#7}}
}{
\ifthenelse{\equal{\f\thecallevel}{\t\thecallevel}}
{\end{callself}}
{\end{callanother}}
}
\xpatchcmd\callanother
{midway, above}
{midway, above, umlsd/call function}
{}{\fail}
\xpatchcmd\endcallanother
{midway, above}
{midway, above, umlsd/call return}
{}{\fail}
\xpatchcmd\callself
{near start, above right}
{near start, above right, umlsd/call function}
{}{\fail}
\xpatchcmd\endcallself
{midway, right}
{midway, right, umlsd/call return}
{}{\fail}
\begin{document}
\begin{sequencediagram}
\tikzstyle {inststyle}+=[bottom color=orange!5, top color=orange!5, rounded corners =1 mm]
% set inst node narrower
\tikzset{inststyle/.append style={minimum width=1.2cm}} % <<< added here
\newthread [cyan!10] {u} {$U_{i}$}
\newinst {uid} {$UID_{Ui}$}
\newinst {vm} {$VM_{i}$}
\newinst {spc} {$sPC_{Ui}$}
\newinst {src} {$sRC_{Vi}$}
\newinst {ts} {$TS_{i}$}
\newinst {rto} {$RTO$}
\newinst {ib} {$IB$}
\newinst {as} {$AS$}
\begin{sdblock}{\shortstack[l]{}}{}
\path (inst1.west) -- (inst9.east);
\begin{call}{u}
% to avoid overlapping, move 5pt upward and 0pt right
[pos=0, above right=5pt and 0pt] % <<< added here
{\shortstack[l] {
1. Hello, test1 test2 test3 test4\\
1. Hello, test1 test2 test3 test4
}}
{vm}
[pos=1, above right] % <<< added here
{\shortstack[l] {
\postlevel
2. Hello, test1 test2 test3\\
2. Hello, test1 test2 test3
}}
\begin{call}{vm}
{\shortstack[l] {
\postlevel
3. Hello, test1 test2 test3 test4 test5 test6\\
3. Hello, test1 test2 test3 test4 test5 test6
}}
{rto}
{\shortstack[l] {
\postlevel
4. Hello, test1 test2 test3 test4 test5\\
4. Hello, test1 test2 test3 test4 test5
}}
\begin{call}{rto}
[pos=1, above left] % <<< added here
{\shortstack[l] {
\postlevel
5. Hello, test1 test2 test3 test4 test5 test6 test7\\
5. Hello, test1 test2 test3 test4 test5 test6 test7
}}
{as}
[pos=0, above left] % <<< added here
{\shortstack[l] {
\postlevel
6. Hello, test1 test2 test3 test4\\
6. Hello, test1 test2 test3 test4
}}
\end{call}
\end{call}
\end{call}
\end{sdblock}
\end{sequencediagram}
\end{document}
messcall
更新:针对环境的类似修补
\documentclass{article}
\usepackage[french,USenglish]{babel}
\usepackage{pgf-umlsd}
\usetikzlibrary{positioning}
\usepackage{xpatch}
%
% add opt-arg for "messcall" env
%
% before
% \begin{messcall}[height]{caller} {function}{callee}
% 1 2 3 4
% now
% \begin{messcall}[height]{caller}[options]{function}{callee}
% 1 2 3 (new) 4<-3 5<-4
%
\RenewDocumentEnvironment{messcall}{ O{1} m O{} m m }{
\stepcounter{seqlevel}
\stepcounter{callevel} % push
\path
(#2)+(0,-\theseqlevel*\unitfactor-0.7*\unitfactor) node (cf\thecallevel) {}
(#5.\threadbias)+(0,-\theseqlevel*\unitfactor-0.7*\unitfactor) node (ct\thecallevel) {};
\draw[->,>=angle 60] ({cf\thecallevel}) -- (ct\thecallevel)
node[midway, above, #3] {#4};
\def\l\thecallevel{#1}
\def\f\thecallevel{#2}
\def\t\thecallevel{#5}
\tikzstyle{threadstyle}+=[instcolor#2]
}
{
\addtocounter{seqlevel}{\l\thecallevel}
\path
(\f\thecallevel)+(0,-\theseqlevel*\unitfactor-0.7*\unitfactor) node (rf\thecallevel) {}
(\t\thecallevel.\threadbias)+(0,-\theseqlevel*\unitfactor-0.3*\unitfactor) node (rt\thecallevel) {};
\drawthread{ct\thecallevel}{rt\thecallevel}
\addtocounter{callevel}{-1} % pop
}
\begin{document}
\begin{sequencediagram}
\newthread{t}{:Thread}
\newinst[1]{i}{:Instance}
\newinst[1]{a}{:Action}
\begin{messcall}{t}[blue, pos=0, above right]{function1()}{i}
\begin{messcall}{i}[red, pos=1, above left]{function2()}{a}
\end{messcall}
\end{messcall}
\end{sequencediagram}
\end{document}