Trouble using PythonTex variables in Latex file

In summary, the conversation discusses using Visual Studio Code as a text editor to achieve the ultimate goal of using PythonTex in a Latex file to generate multiple versions of the same worksheet for a class. However, when trying to define a variable and render it in the PDF, it shows double question marks. Suggestions are made to use an alternative method and to create multiple PDFs using a template and a template engine like Jinja. Steps for creating a PDF containing the text 'Hello' using PythonTex are provided, along with the suggestion to configure the LaTeX editor to run PythonTex. The main focus is on solving the issue of the double question marks and rendering variables correctly.
  • #1
SamRoss
Gold Member
254
36
TL;DR Summary
I defined a variable myvar = "hi" but I can't get it to render correctly in the pdf when I use \py{myvar}.
I'm using Visual Studio Code as my text editor. My ultimate goal is to use PythonTex in a Latex file so that I can generate multiple versions of the same worksheet for my class. As a test, I defined a variable myvar = "hi" but I can't get it to render correctly in the pdf when I use \py{myvar}. It just shows me double question marks.

latex.GIF


When I hover over \py{myvar}, it says "Missing autoprint content". I have no idea what that means. Any help would be greatly appreciated. Thanks!
 
Physics news on Phys.org
  • #4
jedishrfu said:
Since that doesn’t seem to work, I saw an alternative that you could try:
Code:
\pyc{print(myvar)}

Good news and bad news. The good news is the question marks went away. The bad news is nothing took their place.
Capture.GIF
 
  • #5
SamRoss said:
My ultimate goal is to use PythonTex in a Latex file so that I can generate multiple versions of the same worksheet for my class.
A more robust way to achieve your goal would be to generate multiple ## \LaTeX ## files from Python using a template, perhaps similar to this:
mark.tpl.tex:
\documentclass{article}
\begin{document}
Hello \VAR{ name }, you got \VAR{ mark } out of 10.

Well done \VAR{name}!
\end{document}

You can then iterate over the data for the students, either using simple code like this:
Python:
import re

infile = open('mark.tpl.tex')
template = infile.read()
infile.close()

students = {
    'alice.smith': {
        'name': 'Alice',
        'mark': 9,
    },
    'bob.jones': {
        'name': 'Bob',
        'mark': 7.5,
    },
    'charlie.robinson': {
        'name': 'Charlie',
        'mark': 8,
    },
}

for student, vars in students.items():
    result = template
    for key, value in vars.items():
        # Substitute variable, allowing for optional whitespace.
        result = re.sub('\\\\VAR{\\s*' + key + '\\s*}', str(value), result)

    # Here we create a .tex file for each student, we could use os.system()
    # to pipe the output through tex to create to .pdf files directly.
    outfile = open('mark-' + student + '.tex', 'w')
    outfile.write(result)
    outfile.close()

... or using a more advanced template engine like Jinja. Note that Jinja is designed for HTML so you have to make some configuration changes to avoid conflicts with ## \LaTeX ## markup. There are some notes here although I don't know if they work (I do a similar thing but using node.js and mustache templates).

Edit: this code works for creating PDF files on my system (just add it after the last line above), it may need to be changed to suit yours:
Python:
    os.system('pdflatex ' + 'mark-' + student + '.tex')
 
Last edited:
  • Like
Likes sysprog and SamRoss
  • #6
Thank you for your response. This might be something for me to look into later, but right now it is beyond my meager skills. The immediate problem I'm trying to solve is to make those double question marks going away so I can define some variables and have latex render them correctly.
 
  • #8
I was able to create a PDF containing the text 'Hello' from the following file
Code:
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{pythontex}

\begin{pycode}

foo = 'Hello'

\end{pycode}

\begin{document}

\py{foo}

\end{document}
using these steps:
Code:
> pdflatex test.tex
> pythontex test.tex
> pdflatex test.tex

After the first run of LaTeX I also had '??' in the output and a warning about missing autoprint text, but that is to be expected: '??' usually indicates that LaTeX is looking for something in a file which isn't there, usually because to create that file you must first run LaTeX on your document, then run some other tool which looks at the .aux file created by LaTeX in order to work out what it needs to do to create the file. Then you run LaTeX again.

BibTeX, MakeIndex and PythonTex all work this way.

Your LaTeX editor may not have a convenient way to run PythonTex, although you may be able to configure it to do so.
 
  • Like
Likes SamRoss and jedishrfu
  • #9
Thanks very much. This seems to
pasmith said:
...using these steps:
Code:
> pdflatex test.tex
> pythontex test.tex
> pdflatex test.tex

Your LaTeX editor may not have a convenient way to run PythonTex, although you may be able to configure it to do so.
Thank you very much. I feel like I'm getting closer to solving this problem but I still have some questions. You said you used the steps pdflatex test.tex, pythontex test.tex, pdflatex test.tex. I'm not sure what you meant by that. Do you mean you converted the latex into pythontex and then back in latex? If so, how? Also, if my LaTeX editor does not have a convenient way to run PythonTex, how can I configure it to do so? Please bear in mind this is the first "configuring" I will have ever done so, if it is not too much trouble, a step-by-step explanation or a link to a webpage that explains it in a step-by-step manner would be very helpful. I tried searching for answers myself but was unsuccessful. Thanks again!
 
  • #10
SamRoss said:
The immediate problem I'm trying to solve is to make those double question marks going away so I can define some variables and have latex render them correctly.
If you can achieve that you will have slain one alligator but I think you will have a long way to go to produce multiple PDFs each containing multiple substitutions from that one tex file.

I would encourage you to take another look at the solution I posted (forget about Jinja for now, the simple search-and-replace code I used works fine). This should help you drain the swamp in one go.
 
  • Like
Likes SamRoss
  • #11
SamRoss said:
Thanks very much. This seems to

Thank you very much. I feel like I'm getting closer to solving this problem but I still have some questions. You said you used the steps pdflatex test.tex, pythontex test.tex, pdflatex test.tex. I'm not sure what you meant by that. Do you mean you converted the latex into pythontex and then back in latex? If so, how? Also, if my LaTeX editor does not have a convenient way to run PythonTex, how can I configure it to do so? Please bear in mind this is the first "configuring" I will have ever done so, if it is not too much trouble, a step-by-step explanation or a link to a webpage that explains it in a step-by-step manner would be very helpful. I tried searching for answers myself but was unsuccessful. Thanks again!

This stackexchange post explains how to get PythonTex to work with Visual Studio Code. It appears to require manually editing a JSON file in a couple of places.

I use TeXMaker. The user manual is here. Section 3 describes the basic process of compiling a document.

On the toolbar is a drop-down list of standard TeX-related tools, which can be run on the current document by pressing the button to the left.

Untitled1.png


PythonTeX is not one of these, but as you see I have set is up as the first user defined command. To do this, go to User > User Commands > Edit User Commands and complete the dialog as follows (setting up user commands is only mentioned in passing in the user manual):
Untitled3.png


To compile the document correctly, you must do the following:
- Run LaTeX or PDFLaTeX
- Run PythonTex
- Run PDFLaTeX again.
 
  • Like
Likes jedishrfu
  • #12
pasmith said:
This stackexchange post explains how to get PythonTex to work with Visual Studio Code. It appears to require manually editing a JSON file in a couple of places.To compile the document correctly, you must do the following:
- Run LaTeX or PDFLaTeX
- Run PythonTex
- Run PDFLaTeX again.
I did see that stackexchange post but when I tried copying and pasting the code, it didn't work. I downloaded TeXMaker and tried following your instructions but I'm still getting the same error. Here's what I did...

1. I entered the PythonTeX user command.
snip 1.GIF


2. I chose PDFLaTeX from the dropdown menu and then clicked on the arrow on the left.
snip 2.GIF


3. I chose PythonTex from the dropdown menu (the one I just created) and then clicked on the arrow on the left.
snip 3.GIF


4. I chose PDFLaTeX again and clicked the arrow.
snip 2.GIF


5. I clicked the arrow to the left of View PDF.
snip view pdf.GIF


Here is the result...
snip 4.GIF
I tried doing the same thing but with LaTeX instead of the first PDFLaTeX and I got the same result. Am I doing something wrong?
 

Attachments

  • snip 2.GIF
    snip 2.GIF
    2.2 KB · Views: 97
  • #13
pbuk said:
I would encourage you to take another look at the solution I posted (forget about Jinja for now, the simple search-and-replace code I used works fine). This should help you drain the swamp in one go.
I'm sure I will have use for it once I'm more comfortable doing some very simple things. Thank you again.
 

1. How do I use PythonTex variables in my Latex file?

In order to use PythonTex variables in your Latex file, you will need to first define the variables in your Python code using the \py{} command. Then, in your Latex file, you can use the \pyvar{} command to access and use those variables.

2. Why am I getting an error when trying to use PythonTex variables in my Latex file?

There could be a few reasons for this. One possibility is that you have not properly defined the variables in your Python code using the \py{} command. Another possibility is that you are not using the correct syntax for the \pyvar{} command in your Latex file. Make sure to check your code for any typos or missing commands.

3. Can I use PythonTex variables in any part of my Latex document?

Yes, you can use PythonTex variables in any part of your Latex document, including within equations, tables, and figures. As long as you have properly defined the variables in your Python code and used the correct syntax in your Latex file, you should be able to use them anywhere.

4. How can I pass PythonTex variables from one Latex file to another?

To pass PythonTex variables from one Latex file to another, you can use the \inputpythontex command. This will allow you to access the variables defined in one file from another file. Make sure to use the correct file path when using this command.

5. Is it possible to use PythonTex variables in a Latex document that is compiled using a different program?

Yes, it is possible to use PythonTex variables in a Latex document that is compiled using a different program, such as Overleaf or Texmaker. You will just need to make sure that you have the PythonTex package installed and properly configured in your chosen program.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
23
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
727
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Replies
15
Views
4K
  • Programming and Computer Science
Replies
5
Views
175
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top