一个使用tikz自制的uml类图包图环境为什么一个包环境里只能放一个类环境

发布于 2021-05-27 20:10:13

无意间在网上看到了个uml类图包图的编写宏包,觉得挺有意思的,自己跟着搞了搞,用自己的话编辑的,但是逻辑还是照抄的。用的texstdio utf-8编码。目前使用起来包环境中单一个类环境没有问题,但是两个类就会报错,只有一条:Undefined control sequence. end{bao}。我认为问题应该出现在加入操作属性里那几个xdef里面,毕竟原文里面用的命令只有这里改动最大。这里是原代码[pgf-umlcd.sty]
(https://pics.latexstudio.net/uploads/20210527/7e0cd9a637749c126f61c738dd8cbdd6.sty)

我的代码主要逻辑
就在这里,相当于给源代码做了一下删减只想实现嵌套逻辑

\documentclass{ctexart}

\usepackage{tikz}
\usetikzlibrary { arrows.meta, arrows , shapes.geometric,shapes.multipart , backgrounds , fit , decorations.pathmorphing , decorations.text}  %调用tikz下的程序库

\tikzstyle{lei moshi}=[outer sep=1cm,draw, minimum height=2em,  minimum width=2cm, text width=5cm,every text node part/.style={text centered},minimum height=1cm, node distance=2cm ,rectangle split]

\pgfdeclarelayer{background}
\pgfdeclarelayer{connectionlayers}
\pgfsetlayers{background,connectionlayers,main}

\newcounter{shuxingcount}
\newcounter{caozuocount}

\def\Baonei{}


%类

%%创建类环境
\newenvironment{classone}[3][]%
{
    \begin{addshuxing}{#1}{#2}{#3}
    }%
    {
        \node[thislei, anchor=north] (class-\leiming) at (\fangwei)
        {
            \textbf{\leiming}
            \nodepart{second}
            \shuxingall
        };
        
    \end{addshuxing}
}

%%属性
\newcommand{\shuxing}[1]{%
    \ifnum\value{shuxingcount}=0
    \protected\def\shuxingall{#1}
    \else
    \let\oldshuxing\shuxingall
    \protected\def\shuxingall{\oldshuxing \ \newline
        #1}
    \fi
    \stepcounter{shuxingcount}
}

%%加入操作和属性
\newenvironment*{addshuxing}[3]
{
    \def\leiming{#2}%
    \def\fangwei{#3}
    \def\shuxingall{}%
    \setcounter{shuxingcount}{0}%
    \tikzstyle{thislei}=[lei moshi, rectangle split parts=2, #1]
}%
{
    \let\BaoneiOld\Baonei
    \protected\xdef\Baonei{\BaoneiOld (class-\leiming)}
    
}

%包
\newenvironment{bao}[1]{
    \def\Baonei{}
    \def\Baoming{#1}
}{
    \begin{pgfonlayer}{background}
        \node[>=angle 90,draw, inner sep=1, fit = \Baonei] (\Baoming) {};
        \node[>=angle 90,draw, anchor=south west] (\Baoming-head) at
        (\Baoming.north west) {\Baoming};
    \end{pgfonlayer}
}



\begin{document}
    \begin{tikzpicture}
\begin{bao}{baobao}
     \begin{classone}[text width=5cm]{ClassName}{0,0}
         \shuxing{shuxing1}
         \shuxing{shuxing2}
     \end{classone}
     \begin{classone}[text width=5cm]{ClassName2}{0,5}
     \shuxing{shuxing1}
     \shuxing{shuxing2}
    % \caozuo{caozuo1}
    % \caozuo{caozuo2}
     \end{classone}
\end{bao}
    \end{tikzpicture}

\end{document}
这里输入代码

查看更多

关注者
0
被浏览
1.3k
越来越少
越来越少 2021-05-27
这家伙很懒,什么也没写!

我把你的代码改动了两个定义

%%属性
\newcommand{\shuxing}[1]{%
    \ifnum\value{shuxingcount}=0
%        \protected\def\shuxingall{#1}
        \def\shuxingall{#1}
    \else
        \let\oldshuxing\shuxingall
%        \protected\def\shuxingall{\oldshuxing \ \newline         #1}
        \edef\shuxingall{\oldshuxing\noexpand\newline #1}
    \fi
    \stepcounter{shuxingcount}
}

%%加入操作和属性
\newenvironment*{addshuxing}[3]
{
    \def\leiming{#2}%
    \def\fangwei{#3}
    \def\shuxingall{}%
    \setcounter{shuxingcount}{0}%
    \tikzstyle{thislei}=[lei moshi, rectangle split parts=2, #1]
}%
{
    \let\BaoneiOld\Baonei
%    \protected\xdef\Baonei{\BaoneiOld (class-\leiming)}
    \xdef\Baonei{\BaoneiOld (class-\leiming)}
}

然后编译得到
0cbb447004b09221637217f0af86f585.png
还建议把下面定义

%包
\newenvironment{bao}[1]{
    \def\Baonei{}
    \def\Baoming{#1}
}{
    \begin{pgfonlayer}{background}
        \node[>=angle 90,draw, inner sep=1, fit = \Baonei] (\Baoming) {};
        \node[>=angle 90,draw, anchor=south west] (\Baoming-head) at
        (\Baoming.north west) {\Baoming};
    \end{pgfonlayer}
}

中的 >=angle 90, 去掉。

1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览