Text FormattingImport result from Python into regular sentence

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
rotmat
Posts: 4
Joined: Sun Mar 20, 2022 10:48 pm

Import result from Python into regular sentence

Post by rotmat »

Hi
Im writing a book with Latex and looking for a way to import values calculated outside into a standard sentence.

Pythonscript:
Eriks birthday: 770101
Erik_age =today - 770101

Output from formatted Latexfile:
Erik is 45 years old

I want the age to be formatted as normal text. For the reader it shouldnt be visible that there was a calculation behind the age data.

There are a lot of examples where input is formatted as a table. But nothing on inserting data into normal text.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Bartman
Posts: 369
Joined: Fri Jan 03, 2020 2:39 pm

Import result from Python into regular sentence

Post by Bartman »

You could calculate the age of several people with Python and write the result in a text file. With the help of the datatool package, you could then select the value you are looking for and print it at the desired place.

Code: Select all

\begin{filecontents}{data.csv}
name,age
Erik,45
John,28
Mike,32
\end{filecontents}

\documentclass{article}
\usepackage{datatool}

\DTLloaddb{people}{data.csv}

\begin{document}
Erik is \DTLfetch{people}{name}{Erik}{age} years old.
\end{document}
rotmat
Posts: 4
Joined: Sun Mar 20, 2022 10:48 pm

Import result from Python into regular sentence

Post by rotmat »

Great! Exactly what Ive been looking for, Thanks!
rotmat
Posts: 4
Joined: Sun Mar 20, 2022 10:48 pm

Re: Import result from Python into regular sentence

Post by rotmat »

Hi, back here again some years later. But installing again on a new computer below example does not work any longer (neither does the inline example here in the forum). Has anything changed in the datatool package and is it supposed to be used in another way? @Bartman, any suggestions?
rotmat
Posts: 4
Joined: Sun Mar 20, 2022 10:48 pm

Re: Import result from Python into regular sentence

Post by rotmat »

Just found that by adding a wildcard after filecontents it works again. Not sure why
Post Reply