如何使用 pythontex 分别检索多个变量?

如何使用 pythontex 分别检索多个变量?

编辑

我发现了一些东西,但在 Ti 下没有像我希望的那样工作Z.这是代码:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pythontex}
\usepackage{tikz}
\begin{document}

\begin{pycode}
from  math import *
def calculus(xa,ya,xb,yb):
    d=round(sqrt((xb-xa)**2+(yb-ya)**2),3)
    xk=(xa+xb)/2
    yk=(ya+yb)/2
    return d,xk,yk
\end{pycode}


With \textbackslash{}py\{calculus(0,0,1,1)\} the result is \py{calculus(0,0,1,1)}

With \textbackslash{}py\{calculus(0,0,1,1)[0]\} the result is \py{calculus(0,0,1,1)[0]}

With \textbackslash{}py\{calculus(0,0,1,1)[1]\} the result is \py{calculus(0,0,1,1)[1]}

With \textbackslash{}py\{calculus(0,0,1,1)[2]\} the result is \py{calculus(0,0,1,1)[2]}

\begin{center}
\def\xa{0}
\def\ya{0}
\def\xb{1}
\def\yb{1}
\begin{tikzpicture}[x=1.0cm,y=1.0cm, scale=1, every node/.style={scale=1}]
    \draw (\xa,\ya) node [circle,inner sep=1pt,fill] {} node [left] {$A$} -- (\xb,\yb) node [circle,inner sep=1pt,fill] {} node [right] {$B$};
    \draw ({(\xa+\xb)/2},{(\ya+\yb)/2}) node [circle,inner sep=1pt,fill] {} node [below right] {$K$};
\end{tikzpicture}
\end{center}
\end{document}

在此处输入图片描述

在 tikz 块中,我想替换:

\draw ({(\xa+\xb)/2},{(\ya+\yb)/2}) node [circle,inner sep=1pt,fill] {} node [below right] {$K$};

类似这样的:

\draw ({\py{calculus(\xa,\ya,\xb,\yb)[1]}},{\py{calculus(\xa,\ya,\xb,\xb)[2]}})  node [circle,inner sep=1pt,fill] {} node [above] {$K$};

原始帖子

使用 pythontex 后,我想分别检索多个变量。我知道如何同时检索单个变量和多个变量,但不能分别检索。以下是示例:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pythontex}
\begin{document}

\begin{pycode}
def sum1(x,y):
    z=x+y
    return z
\end{pycode}

With sum1 the result is \py{sum1(2,3)}

\begin{pycode}
def sum2(x,y):
    z=x+y
    return x,y,z
\end{pycode}

With sum2 the result is \py{sum2(2,3)}

\end{document}

输出为:

在此处输入图片描述

我想要做的是使用 python 代码返回的 x、y 和 z 变量来编写例如:

2 + 3 = 5 的结果。

我想知道是否可以在 Python 代码之外做到这一点。

谢谢

答案1

LaTeX 语句似乎不能\def\xa{0}与 结合使用pythontex。如果您将这些语句(从您的center环境开始)更改为\pyc{xa=0}语句,那么您可以pysub按照@GPoore 的建议使用环境。

这是您的代码,已修改:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{pythontex}
\usepackage{tikz}
\begin{document}

    \begin{pycode}
from  math import *
def calculus(xa,ya,xb,yb):
    d=round(sqrt((xb-xa)**2+(yb-ya)**2),3)
    xk=(xa+xb)/2
    yk=(ya+yb)/2
    return d,xk,yk
\end{pycode}


With \textbackslash{}py\{calculus(0,0,1,1)\} the result is \py{calculus(0,0,1,1)}

With \textbackslash{}py\{calculus(0,0,1,1)[0]\} the result is \py{calculus(0,0,1,1)[0]}

With \textbackslash{}py\{calculus(0,0,1,1)[1]\} the result is \py{calculus(0,0,1,1)[1]}

With \textbackslash{}py\{calculus(0,0,1,1)[2]\} the result is \py{calculus(0,0,1,1)[2]}

\begin{center}
    \pyc{xa=0}
    \pyc{ya=0}
    \pyc{xb=1}
    \pyc{yb=1}
\begin{pysub}
\begin{tikzpicture}[x=1.0cm,y=1.0cm, scale=1, every node/.style={scale=1}]
    \draw (!{xa},!{ya}) node [circle,inner sep=1pt,fill] {} node [left] {$A$} -- (!{xb},!{yb}) node [circle,inner sep=1pt,fill] {} node [right] {$B$};

    \draw (!{calculus(xa,ya,xb,yb)[1]},!{calculus(xa,ya,xb,xb)[2]})  node [circle,inner sep=1pt,fill] {} node [above] {$K$};
\end{tikzpicture}
\end{pysub}
\end{center}
\end{document}

相关内容