Python in Latex
to Use Python in LaTeX it would be very convenient. Immediately warn that the proposed methods are considered unsafe, because Python knows how to spoil, if you write the appropriate code, so check it (=
There are the ready method in stylesheet, use this:
the
This package actually creates jobname.py writes the output to jobname.py.out, jobname.py.err, and reads it.
Method is bad because variables are not saved from code to code, and have to use pickle, to have every time all import'it, etc., and I think it's convenient to have all the calculations in one place in a separate file, so I built my amusement Park...
It's very simple. Add to the preamble the following:
the
The first macro calls python with the specified arguments, and the second runs contrary script with arguments, but the third can perform a single-line Python code, which is then preobrazovaniya little sed'ohms, to replace the dots by commas. And posledni macro added, what would be the Python output and the appearance of any error in Latex, it was clear where the problem is
In the script I have a little trick for beautiful output(though only for math environment), and gray, too, can share(of course, sed will only work if you have one).
the
the
Ie typing in LaTeX "\ptn{varAn1}" end document substituted the value of the variable "varAn1", if defined in "./path_to_your_scripts/script_name.py". And writing "\peval{1./5}" will get 0,2. Although this \peval I do not use it, so not finished and it works lousy.
PS to Process the LaTeX file needed with the option "-shell-escape"
Article based on information from habrahabr.ru
There are the ready method in stylesheet, use this:
the
\usepackage{python} \begin{python} from math import sin a = sin(5) c = sin(9) b = max(a,c) print b \end{python}
This package actually creates jobname.py writes the output to jobname.py.out, jobname.py.err, and reads it.
Method is bad because variables are not saved from code to code, and have to use pickle, to have every time all import'it, etc., and I think it's convenient to have all the calculations in one place in a separate file, so I built my amusement Park...
It's very simple. Add to the preamble the following:
the
\newcommand{\python}[1]{% \immediate\write18{python #1 > ./py.out 2> ./py.err} \immediate\input"py.out"} \newcommand{\ptn}[1]{% the \python{./path_to_your_scripts/script_name.py #1}} \newcommand{\peval}[1]{% the \python{-c "print #1" |./path_to_your_scripts/sedder}} \newcommand{\comments}[1]{}
The first macro calls python with the specified arguments, and the second runs contrary script with arguments, but the third can perform a single-line Python code, which is then preobrazovaniya little sed'ohms, to replace the dots by commas. And posledni macro added, what would be the Python output and the appearance of any error in Latex, it was clear where the problem is
In the script I have a little trick for beautiful output(though only for math environment), and gray, too, can share(of course, sed will only work if you have one).
sedder:
the
#!/bin/sh sed -e 's/\./,/' #Make it executable.
end-of-file "./path_to_your_scripts/script_name.py":
the
import sys from re import sub from __future__ import division def toStr(num,digitsAfter=2): """Num to string with zero stripping""" frmt = '%.' + str(digitsAfter) + 'f' num = (frmt%num) num = num.strip('0') if num[0] =='.': num = '0'+num if num[-1] == '.': num = num.strip('.') return num def tenpower(num,maximum=1e3,minimum=1e-2,base=1): """converts num to (num1,num2) where num1*10**num2=num converts when not minimum<num<maximum. converts base until<num1<2*base""" pwr = 0 while abs(num) > maximum or (abs(num)>2*base and pwr>0): pwr +=1 num = num / 10 while abs(num) < minimum or (abs(num)<base & pwr<0): pwr -=1 num = num * 10 return num,pwr def latexize(num,power=False,delim=','): if power: return ('%s \\cdot 10^{%d}' %(num,power)).replace('.',delim) else: return str(num).replace('.',delim) if __name__ == "__main__": #try: argc = len(sys.argv) var = 'Ooopsy-doopsy!' digitsAfter = 2 typ = 10 if argc >1: var = sub('[{}\. ]',",sys.argv[1]) comment = '\\comments{%s}'%sys.argv[1] var = globals()[var] else: raise ValueError if argc >2: try: digitsAfter = int(sys.argv[2]) except: typ = sys.argv[2] if argc > 3: typ = sys.argv[2] digitsAfter = int(sys.argv[3]) mini=1/10**digitsAfter if typ in ('int','i'): print comment,int(var) elif typ in ['float','f','nomath']: print comment+latexize(toStr(var,digitsAfter)) else: strings = tenpower(var minimum=mini) print comment+latexize(toStr(strings[0],digitsAfter),strings[1]) #except Exception: # pass
Ie typing in LaTeX "\ptn{varAn1}" end document substituted the value of the variable "varAn1", if defined in "./path_to_your_scripts/script_name.py". And writing "\peval{1./5}" will get 0,2. Although this \peval I do not use it, so not finished and it works lousy.
PS to Process the LaTeX file needed with the option "-shell-escape"
Комментарии
Отправить комментарий