如何使用 tkz-graph 和 tikz-qtree 而不发生冲突?

如何使用 tkz-graph 和 tikz-qtree 而不发生冲突?

似乎tikz-qtreetkz-graph包都定义(或重新定义)了\edge宏...我想同时使用这两个,但似乎不可能:

\documentclass{article}

\usepackage{tikz}
\usepackage{tikz-qtree}
\usepackage{tkz-graph}


\begin{document}
\begin{tikzpicture}[level distance=40pt]
\Tree [.A [.B [.C a ] ]
          [.D [.E quote ]
               [.F \edge[roof]; {Eye of newt, and toe of frog} ] ] ]
\end{tikzpicture}

\begin{tikzpicture}
\GraphInit[vstyle=Classic]
\Vertex{z}
\end{tikzpicture}

\end{document}

编译上述文档时出现以下错误:

! Use of \@edge doesn't match its definition.
<recently read> \edge 

l.12               [.F \edge
                            [roof]; {Eye of newt, and toe of frog} ] ] ]
? 

有解决方法吗?

答案1

抱歉我有罪。我没有采取足够的预防措施。问题来自:

\newcommand*{\Edge}[1][]{\@edge[#1]}%  because tikz-qtree defines `\@edge` too :(

更好的方法是使用 \tkz@edge 代替\@edge。在下一个使用 pgfkeys 的版本中,我会更加小心。

实际上你可以保存下一个文件,名称为:patch-tkz-graph.tex

  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  % patch-tkz-graph.tex : patch for tkz-graph

 \makeatletter
 \renewcommand*{\Edge}[1][]{\tkz@edge[#1]}%
 \def\tkz@edge[#1](#2)(#3){%
 \setkeys[GR]{edge}{#1}%
  \begingroup%
 \ifthenelse{\equal{\cmdGR@edge@double}{}}{%
 \tikzset{LocalEdgeStyle/.style={color = \cmdGR@edge@color,
                                 line width = \cmdGR@edge@lw}}}{%
 \tikzset{LocalEdgeStyle/.style={line width = \cmdGR@edge@dd,
                                 color = \cmdGR@edge@double,
                                 double distance = \cmdGR@edge@lw,
                                 double  = \cmdGR@edge@color}}}%
 \ifGR@edge@local%
       \tikzset{EdgeStyle/.style={}}%
       \fi
    \ifthenelse{\equal{\cmdGR@edge@label}{}}{%
      \protected@edef\@tempa{%
      \noexpand   \draw[LocalEdgeStyle,\cmdGR@edge@style,EdgeStyle]}%
                  \@tempa (#2) to (#3)}{%
      \protected@edef\@tempa{%
      \noexpand   \draw[LocalEdgeStyle,\cmdGR@edge@style,EdgeStyle] (#2) to%
     node[fill = \cmdGR@edge@labelcolor,
          text = \cmdGR@edge@labeltext,
          \cmdGR@edge@labelstyle,LabelStyle]}\@tempa
    {\cmdGR@edge@label} (#3)}%
    ;
 \endgroup%
 }%    
 \makeatother  
\endinput

 %%%%%%%%%%%%%%%%%%%%%%%%

进而

\documentclass{article}
% no need to load tikz, tkz loads tikz 
\usepackage{tkz-graph}% in first  
\input{patch-tkz-graph}
\usepackage{tikz-qtree}   


\begin{document}
\begin{tikzpicture}[level distance=40pt]
\Tree [.A [.B [.C a ] ]
          [.D [.E quote ]
               [.F \edge[roof]; {Eye of newt, and toe of frog} ] ] ]
\end{tikzpicture}

\begin{tikzpicture}
\GraphInit[vstyle=Hasse] 
\Vertex[style={line width=2pt}]{A}
\Vertex[x=6,y=0,style={line width=2pt}]{B}
\Edge[style={->,>=latex,bend left=90},label=$x$](A)(B)
\end{tikzpicture} 

\end{document}   

在此处输入图片描述

相关内容