My pythontex package might be an option (latest version on GitHub; will be on CTAN soon). It offers the following features.
- Enter Python within your LaTeX document, and bring the output back into LaTeX.
- User-defined sessions. If you have independent calculations that are slow, just put them in separate sessions, and they will automatically run in parallel, using all available cores.
- All calculations are saved or cached; code is only executed when modified (unless you specify otherwise).
- Any errors or warnings are synchronized with your document, so that you know which line in your document caused problems.
The easiest thing to do would be to pass whatever parameters you need to Python, do your calculations, and then print LaTeX code back to the TeX side. There are a few examples in the documentation, and I've also used pythontex to answer a few questions on this site.
If you just want to get values back from Python, stored in macros, here's an example. This allows you to pass a length to Python, perform some calculations on it, and then return the output to TeX, stored in a macro. Again, usually it's probably easier to just assemble a complete block of LaTeX code within Python, and print that back instead (maybe have a template, and perform substitutions.)
\documentclass{article}
\usepackage{pythontex}
\makeatletter
% Define a Python function that takes a TeX length and a name,
% extracts the numerical part of the length and performs
% a calculation with it, and finally prints a macro definition
% back to TeX that stores the calculated value, with its
% units, using the specified name for the macro.
\begin{pylabcode}
def my_calc(length, name):
num = float(length[:-2])
unit = length[-2:]
print(r'\def\{0}{{{1}{2}}}'.format(name, str(sqrt(num**2/2)), unit))
\end{pylabcode}
\newcommand{\mycalc}[2]{%
% Create a default macro for the output.
% This avoids errors before PythonTeX runs
% and creates the real value
\expandafter\def\csname #2\endcsname{0pt}%
% When using PythonTeX commands inside macros,
% it's sometimes easiest to employ a helper
% macro to get everything expanded properly.
\expandafter\mycalc@i\expandafter{\the#1}{#2}%
}
\newcommand{\mycalc@i}[2]{%
\pylabc{my_calc('#1','#2')}%
}
\makeatother
\begin{document}
% This passes the value of \textwidth to Python,
% performs a calculation on it, and stores the result,
% including units, in the macro \returnvalue
\mycalc{\textwidth}{returnvalue}
\returnvalue
\end{document}
write18feature. But what you actually plan to do, I don't know. If you search this site, you will get codes wherewrite18is used. I use it to runconvert(of imagemagick) from within the latex document. – Jan 14 '13 at 03:28fp. I used it to make in-text computations & analogue of "electronic tables". Here you can read something aboutfp(on russian). – Eddy_Em Jan 14 '13 at 05:51\tableofcontents: on first run external program (bcfor example) fills some file with constuctions like\def\somevar{something} \newlength\anotherlength \anotherlength=newvaletc. On second run this file will be\include'd to your project. – Eddy_Em Jan 14 '13 at 05:56luatex. – Marco Jan 14 '13 at 13:46