通过 Sage 自动获取 tikz 中的 QPA 箭筒

通过 Sage 自动获取 tikz 中的 QPA 箭筒

我使用 GAP 包 QPA 来获取箭筒(它们只是有向图),箭筒的 QPA 输出例如如下所示:

["v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15"], [["v1","v3","a1"],["v2","v1","a2"],["v3","v8","a3"],["v4","v6","a4"],["v5","v6","a5"]\
,["v6","v4","a6"],["v6","v5","a7"],["v6","v8","a8"],["v7","v12","a9"],["v8","v9","a10"],["v8","v10","a11"],["v9","v7","a12"],["v9","v11","a13"],["v10","v7","a14"],["v10","v11","\
a15"],["v11","v12","a16"],["v12","v2","a17"],["v12","v13","a18"],["v13","v14","a19"],["v13","v15","a20"],["v14","v13","a21"],["v15","v13","a22"]]

这是第一个列表["v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15"]

只是顶点的名称和第二个列表

[["v1","v3","a1"],["v2","v1","a2"],["v3","v8","a3"],["v4","v6","a4"],["v5","v6","a5"] ,["v6","v4","a6"],["v6","v5","a7"],["v6","v8","a8"],["v7","v12","a9"],["v8","v9","a10"],["v8","v10","a11"],["v9","v7","a12"],["v9","v11","a13"],["v10","v7","a14"],["v10","v11"," a15"],["v11","v12","a16"],["v12","v2","a17"],["v12","v13","a18"],["v13","v14","a19"],["v13","v15","a20"],["v14","v13","a21"],["v15","v13","a22"]]

只是箭头的名称以及它们的起点和终点。

主要问题是使用某些软件自动将此有向图转换为适合 tikz 的漂亮 latex 图片。现在可以使用 Sage 将此箭筒转换为适合 latex 的 tikz,如下所示:

def from_QPA(Q):
    arrows = [(v.SourceOfPath(), v.TargetOfPath(), v) for v in Q.ArrowsOfQuiver() ]
    elements = Q.VerticesOfQuiver()
    return DiGraph([elements, arrows], loops=True, format='vertices_and_edges')

libgap.LoadPackage("QPA")
Q = libgap.Quiver(    ["v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15"],  [["v1","v3","a1"],["v2","v1","a2"],["v3","v8","a3"],["v4","v6","a4"],["v5","v6","a5"]\
 ,["v6","v4","a6"],["v6","v5","a7"],["v6","v8","a8"],["v7","v12","a9"],["v8","v9","a10"],["v8","v10","a11"],["v9","v7","a12"],["v9","v11","a13"],["v10","v7","a14"],["v10","v11","\
a15"],["v11","v12","a16"],["v12","v2","a17"],["v12","v13","a18"],["v13","v14","a19"],  ["v13","v15","a20"],["v14","v13","a21"],["v15","v13","a22"]] )
U=from_QPA(Q)
display(U.plot(edge_labels=True))
U.set_latex_options(edge_labels=True)
display(latex(U))

例如,您可以将其输入到 Sage 在线单元格中https://sagecell.sagemath.org/查看结果,即图片和相应的 tikz 代码,用于在乳胶中获取箭筒。

遗憾的是,对于本例中的大箭筒来说,结果看起来很糟糕。

问题 1:有没有一种简单的方法可以让 tikz 中复杂的箭筒效果看起来不错?

例如,当箭头的名称位于箭头顶部而不是箭头内部时可能会更好(也许它们应该更小)。

问题 2:除了使用 Sage 之外,有没有更好的方法可以使用其他程序直接从 tikz 中的 QPA 获取箭筒,或者甚至以某种方式使用 tikz 本身?

答案1

在您的评论中,您提到在研究文章中使用图形。这意味着仅使用 Sage 是不够的,因为期刊是出了名的挑剔。许多期刊不想要颜色,并且对您使用的字体和字体大小有特定的要求。LaTeX 是最好的工具。您可以将 Sage 与软件包一起使用,sagetextkz-graphtkz-berge您完成许多困难的工作。请参阅 Alain Matthes 的回答这里有关排版图表的更多信息。以下是使用您在上面的评论中提到的序言代码的一些示例代码。

\documentclass[border={2mm 2mm 8mm 8mm}]{standalone}
\usepackage{sagetex,xcolor,tikz,tkz-graph,tkz-berge,tikz-cd}
\usetikzlibrary{matrix,patterns,shapes,decorations.pathmorphing,decorations.pathreplacing,calc,arrows}
\begin{document}
\begin{sagesilent}
def from_QPA(Q):
arrows = [(v.SourceOfPath(), v.TargetOfPath(), v) for v in Q.ArrowsOfQuiver() ]
elements = Q.VerticesOfQuiver()
return DiGraph([elements, arrows],loops=True,format='vertices_and_edges')

libgap.LoadPackage("QPA")
Q = libgap.Quiver(    ["v1","v2","v3","v4","v5","v6","v7","v8","v9","v10","v11","v12","v13","v14","v15"],  [["v1","v3","a1"],["v2","v1","a2"],["v3","v8","a3"],["v4","v6","a4"],["v5","v6","a5"]\
,["v6","v4","a6"],["v6","v5","a7"],["v6","v8","a8"],["v7","v12","a9"],["v8","v9","a10"],["v8","v10","a11"],["v9","v7","a12"],["v9","v11","a13"],["v10","v7","a14"],["v10","v11","\
a15"],["v11","v12","a16"],["v12","v2","a17"],["v12","v13","a18"],["v13","v14","a19"],  ["v13","v15","a20"],["v14","v13","a21"],["v15","v13","a22"]] )
U=from_QPA(Q)
U.set_latex_options(graphic_size=(20,20), vertex_labels=True,edge_labels=True)
Ustring = latex(U)
\end{sagesilent}
\sagestr{Ustring}
\end{document}

结果运行可钙是: 在此处输入图片描述

请注意,图片显示 LaTeX 正在抱怨错误,但图片却呈现了出来。您说没有收到错误;也许是更新的版本?每次Build编写代码时,您都会得到不同的图形,因此您需要捕捉外观,以便处理他们希望您进行的几乎肯定的修改。当你找到接近满意的图片时,从.sout文件中获取代码: 在此处输入图片描述 现在您可以复制此代码(我已经从不同的 Build 复制并修改了代码)以获得如下内容:

\documentclass[11pt,border={10pt 10pt 10pt 10pt}]{standalone}
\usepackage{sagetex,xcolor,tikz,tkz-graph,tkz-berge,tikz-cd}
\usetikzlibrary{matrix,patterns,shapes,decorations.pathmorphing,decorations.pathreplacing,calc,arrows}
\begin{document}
\begin{sagesilent}
a = 3.75
b = 3.75
output = r"\begin{tikzpicture}"
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_0$},x=%s cm,y=%s cm]{v0}"%(3.2737*a,0.5293*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_1$},x=%s cm,y= %s cm]{v1}"%(3.5269*a,1.7219*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_2$},x= %s cm,y=%s cm]{v2}"%(2.4491*a,0.527*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_3$},x=%s cm,y=%s cm]{v3}"%(0.0*a,0.9498*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_4$},x=%s cm,y=%s cm]{v4}"%(0.4106*a,0.0*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_5$},x=%s cm,y=%s cm]{v5}"%(0.8179*a,0.8914*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_6$},x=%s cm,y=%s cm]{v6}"%(2.3603*a,3.4245*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_7$},x=%s cm,y=%s cm]{v7}"%(1.7828*a,1.4959*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_8$},x=%s cm,y=%s cm]{v8}"%(1.7707*a,2.778*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_9$},x=%s cm,y=%s cm]{v9}"%(2.1004*a,2.4636*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_{10}$},x=%s cm,y=%s cm]{v10}"%(2.5107*a,2.9341*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_{11}$},x=%s cm,y=%s cm]{v11}"%(3.2906*a,3.092*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_{12}$},x=%s cm,y=%s cm]{v12}"%(4.2021*a,4.0255*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_{13}$},x=%s cm,y=%s cm]{v13}"%(4.485*a,5.0*b)
output += r"\Vertex[style={minimum size=1.0cm,shape=circle},LabelOut=false,L=\hbox{$v_{14}$},x=%s cm,y=%s cm]{v14}"%(5.0*a,4.1315*b)
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v0)(v2)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v1)(v0)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v2)(v7)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v3)(v5)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v4)(v5)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v5)(v3)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v5)(v4)"
output += r"\Edge[lw=0.1cm,style={post, straight,},label=$a_8$](v5)(v7)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v6)(v11)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v7)(v8)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v7)(v9)"
output += r"\Edge[lw=0.1cm,style={post, straight,},](v8)(v6)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v8)(v10)"
output += r"\Edge[lw=0.1cm,style={post, straight,},](v9)(v6)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v9)(v10)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v10)(v11)"
output += r"\Edge[lw=0.1cm,style={post, bend right},](v11)(v1)"
output += r"\Edge[lw=0.1cm,style={post, straight,},label=$a_{18}$](v11)(v12)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v12)(v13)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v12)(v14)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v13)(v12)"
output += r"\Edge[lw=0.1cm,style={post, bend right,},](v14)(v12)"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}

在此处输入图片描述

将每行代码放在原始字符串中;例如,output = r"\begin{tikzpicture}"将允许您借助您喜欢的坐标微调定位并选择您自己的a和值b。我从一张令我满意的图片中获取了位置,并通过调整(例如,3.2737*a,0.5293*b我可以拉伸图形以使图形变得更好)。如果您愿意,您可以修改顶点,删除标签并使有向边变直。例如,output += r"\Edge[lw=0.1cm,style={post, straight,},label=$a_{18}$](v11)(v12)"创建带有标签的直线有向边。

那么问题 1 的答案就是:使用包tkz-bergetkz-graphsagetex来创建图片。Sage 将进行计算,但 LaTeX 将处理图片。关于问题 2,我认为没有比我建议的更好的方法了,因为 Sage 使用 GAP 包 QPA 进行计算。

相关内容