2

Edit

I found something, but not working as I would like under TikZ. Here is the code :

\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}

enter image description here

Inside the tikz block, I would like to replace :

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

with something like :

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

Original post

I would like to retrieve multiple variables separatly after using pythontex. I know how to retrieve a single variable, and multiples variables at the same time, but not separatly. Here is an example :

\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}

and the output is :

enter image description here

What I want to do is to use x, y and z variables returned by python code, to write for example :

The result of 2 + 3 = 5.

And I want to know if it's possible to do that outside the python code.

Thanks

Ingmar
  • 6,690
  • 5
  • 26
  • 47
Stan
  • 173
  • When mixing pythontex and tikz, the pusyb environment can be useful, for example: https://tex.stackexchange.com/a/65294/10742 – G. Poore Feb 06 '18 at 12:58
  • yes I saw it yesterday, but with \draw (!{\py{calculus(\xa,\ya,\xb,\yb)[1]}},!{\py{calculus(\xa,\ya,\xb,\yb)[1]}}) node [circle,inner sep=1pt,fill] {} ; the compilation doesn't stop. – Stan Feb 06 '18 at 13:17

1 Answers1

1

It doesn't seem that \def\xa{0} LaTeX statements can be used in combination with pythontex. If you change those statements (starting at your center environment) to \pyc{xa=0} statements, then you can use the pysub environment as @GPoore suggests.

Here's your code, modified:

\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}
Bill N
  • 652
  • 6
  • 16