将 Latex 代码写入点文件

将 Latex 代码写入点文件

有没有办法将 Latex 代码写入点文件(例如在 python 中使用 networkx),以便在读取 Latex 文件时执行 Latex 代码?

如果我使用此代码创建一个点文件,并使用 graphviz 将其包含到 latex 中,则 latex 代码将显示为原始文本,而不会执行。我必须以特殊格式编写 latex 代码吗?

import networkx as nx
def draw_graph():
    G = nx.DiGraph()
    G.add_edge(r'$\alpha$', r'$\beta$', label=r'$\alpha+\beta$')
    G.add_nodes_from([r'$\alpha$', r'$\beta$'])
    A = nx.drawing.nx_agraph.to_agraph(G)
    A.draw('somegraph.dot', prog='dot', args="-Grankdir=LR")
draw_graph()

我也尝试过用 dot2tex 将点文件转换为乳胶,但是这会将乳胶代码 $\alpha$ 转换为 $alpha$...:

\begin{tikzpicture}[>=latex',line join=bevel,]
  \pgfsetlinewidth{1bp}
%%
\pgfsetcolor{black}
  % Edge: $\alpha$ -> $\beta$
  \draw [->] (77.003bp,18.0bp) .. controls (105.9bp,18.0bp) and (145.93bp,18.0bp)  .. (186.71bp,18.0bp);
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (132.0bp,25.5bp) node {\$alpha+beta\$};
  % Node: $\alpha$
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (38.5bp,18.0bp) ellipse (38.5bp and 18.0bp);
  \draw (38.5bp,18.0bp) node {\$alpha\$};
\end{scope}
  % Node: $\beta$
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (221.5bp,18.0bp) ellipse (34.5bp and 18.0bp);
  \draw (221.5bp,18.0bp) node {\$beta\$};
\end{scope}
%
\end{tikzpicture}

答案1

如果只是使用希腊字母作为标签,而不需要进一步的 LaTeX 排版(例如分数等),那么您可以直接在图表中使用 Unicode 输入并保存为 pdf,该 pdf 可以包含在您的 LaTeX 文档中。

梅威瑟:

import networkx as nx
def draw_graph():
    G = nx.DiGraph()
    G.add_edge(r'α', r'β', label=r'α+β')
    G.add_nodes_from([r'α', r'β'])
    A = nx.drawing.nx_agraph.to_agraph(G)
    A.draw('somegraph2.pdf', prog='dot', args="-Grankdir=LR")
draw_graph()

生成的pdf:

在此处输入图片描述

答案2

在你的 Python 脚本中,删除 $ 并转义 \

import networkx as nx
def draw_graph():
    G = nx.DiGraph()
    G.add_edge(r'\\alpha', r'\\beta', label=r'\\alpha+\\beta')
    G.add_nodes_from([r'\\alpha', r'\\beta'])
    A = nx.drawing.nx_agraph.to_agraph(G)
    A.draw('somegraph.dot', prog='dot', args="-Grankdir=LR")
draw_graph()

运行这个脚本应该会

strict digraph "" {
    graph [bb="0,0,224.89,36",
        rankdir=LR
    ];
    node [label="\N"];
    "\\alpha"    [height=0.5,
        pos="32.497,18",
        width=0.9027];
    "\\beta"     [height=0.5,
        pos="196.94,18",
        width=0.77632];
    "\\alpha" -> "\\beta"    [label="\\alpha+\\beta",
        lp="116.99,25.5",
        pos="e,168.77,18 65.38,18 92.225,18 130.2,18 158.46,18"];
}

然后dot2tex调用

dot2tex -t math --figonly -o somegraph.tex somegraph.dot

-t math选项用于在数学模式下包含标签内容

然后你进去somegraph.tex

\begin{tikzpicture}[>=latex,line join=bevel,]
  \pgfsetlinewidth{1bp}
%%
\pgfsetcolor{black}
  % Edge: \\alpha -> \\beta
  \draw [->] (65.394bp,18.0bp) .. controls (92.248bp,18.0bp) and (130.24bp,18.0bp)  .. (168.82bp,18.0bp);
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (117.0bp,25.5bp) node {$\alpha+\beta$};
  % Node: \\beta
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (197.0bp,18.0bp) ellipse (28.0bp and 18.0bp);
  \draw (197.0bp,18.0bp) node {$\beta$};
\end{scope}
  % Node: \\alpha
\begin{scope}
  \definecolor{strokecol}{rgb}{0.0,0.0,0.0};
  \pgfsetstrokecolor{strokecol}
  \draw (32.5bp,18.0bp) node {$\alpha$};
\end{scope}
%
\end{tikzpicture}

相关内容