Reputation: 1
I am an English teacher and for my online classes, I prepare a PDF with LaTeX which has the new vocabulary and grammar and other notes. Now I generally write the meaning of the words myself, however, in order to be more precise, I'm trying to use the sage package to use Python to do an API call to get the meaning of the words that I put as an input. Unfortunately, it is not working as expected and when I compile the PDF, instead of the meaning of the word, ?? appears.
The function works in Python and it will return the meaning, it seems that LaTeX cannot read the characters. Here is the code in question:
\documentclass{article}
\usepackage{tabularx}
\begin{document}
\begin{sagesilent}
import requests
def define(word):
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{word}"
try:
response = requests.get(url)
data = response.json()
if response.status_code == 200:
# Extracting definition from the API response
definition = data[0]['meanings'][0]['definitions'][0]['definition']
return definition
else:
return f"Failed to fetch definition. Status code: {response.status_code}"
except requests.RequestException as e:
return f"Error: {e}"
}
\end{sagesilent}
\input{title}
\section{Vocabulary}\label{sec:vocabulary}
\begin{tabularx}{\textwidth}{l|X}
\toprule
Word & Definition \\ \midrule
request & \sage{define(request)}
\bottomrule
\end{tabularx}
\end{document}
I have tried searching here, and also used ChatGPT, alas to no avail.
Upvotes: 0
Views: 57
Reputation: 1696
Try sage{define('request')}
— you need quotes around the argument you're passing to the Python function define
.
(I have all sorts of other problems with your LaTeX code: it needs \usepackage{sagetex}
, it might need \usepackage{amsmath}
, it fails because I don't have a file title.tex
, etc.)
Upvotes: 0