6

This code works perfectly:

% arara: pdflatex: { shell : yes }
% arara: pythontex
% arara: pdflatex: { shell : yes }
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{provapd.txt}
user_id|age
1|20
2|25
3|30
4|35
\end{filecontents*}
\usepackage{pythontex}
\begin{document}
    prova pandas

\begin{pycode} 
import pandas as pd
users = pd.read_table(r'provapd.txt', 
                      sep='|', index_col='user_id')
print(users.age.mean())
#print(users.head(3))
\end{pycode}
\end{document}

but if I uncomment print(users.head(3)) I get this error:

! Emergency stop.
<inserted text> 
                $
l.3 user_
         id
End of file on the terminal!

The result in pure Python is:

27.5
         age
user_id
1         20
2         25
3         30

How could I manage variable names with underscore in pythontex?

4
  • Hello! What are you trying to achive? Only typeset the code, or run it too? For the latter, I am using environment "pyblock" and then print it via \printpythontex[verbatim]; which prints the output in verbatim mode. Would that be acceptable for you? (I know verbatim is sometimes "ugly"). Otherwise, there would have to be some "parsing" required; I am doing that on "beginner" basis when I print data from database connection. I can try to give example using your MWE. Commented Nov 24, 2019 at 10:35
  • @TomášKruliš Thank you! I would like to show the output of the run, not the code itself. The same output I get when I run python mycode.py (where mycode.py is what you see in pycode environment.
    – CarLaTeX
    Commented Nov 24, 2019 at 10:43
  • 1
    Uhm... Just replace the underscor in the output-string with \_? You could write an escape routine that sanitizes strings by replacing all special characters with their escaped equivalents
    – Raven
    Commented Nov 24, 2019 at 10:48
  • @Raven Yes, I think it is the only solution.
    – CarLaTeX
    Commented Nov 24, 2019 at 11:16

2 Answers 2

3

I got this:

% arara: pdflatex: { shell : yes }
% arara: pythontex
% arara: pdflatex: { shell : yes }
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{provapd.txt}
user_id|age
1|20
2|25
3|30
4|35
\end{filecontents*}
\usepackage{pythontex}
\begin{document}
    prova pandas

\begin{pycode} 
import pandas as pd
users = pd.read_table(r'provapd.txt', 
                      sep='|', index_col='user_id')
print(users.age.mean())
print('\n\n')
s=str(users.head(3))
s = s.replace('_','\_').replace('\n','\n\n')
print(s)
\end{pycode}
\end{document}

Sadly, I dont know how to put the output here. I added double newlines in there to separate the mean value and data frame content, converted the output to its string representation and worked with it as a simple string, replacing anything neccessary. Sadly, that led to separating ID and age with newlines (which is weird).

Anyhow, this approach can be used anytime and you can make the output of pandas dataframe into LaTeX table by performing string replacement into form of LaTeX tabular environment...

EDIT: Now it works just about fine:

% arara: pdflatex: { shell : yes }
% arara: pythontex
% arara: pdflatex: { shell : yes }
\documentclass{article}
\usepackage{filecontents}
\begin{filecontents*}{provapd.txt}
user_id|age
1|20
2|25
3|30
4|35
\end{filecontents*}
\usepackage{pythontex}
\begin{document}
    prova pandas

\begin{pycode} 
import pandas as pd
users = pd.read_table(r'provapd.txt', 
                      sep='|', index_col='user_id')
print(users.age.mean())
print('\n\n')
s=str(users.head(3))
s = s.replace('_','\_').replace('\n',' ',1).replace('\n','\n\n')
print(s)
\end{pycode}
\end{document}

Note the first replacement - string.replace function takes optional, thirst argument, which is "count" - how many replacements should be performed. In that case, exactly one (first) is what you need.

8
  • That's almost perfect, for the newline I've already asked on Stack Overflow: stackoverflow.com/questions/59016084/…
    – CarLaTeX
    Commented Nov 24, 2019 at 11:31
  • 1
    Oh, ok, cool, I get it now... The easiest way to approach this could be to iterate through the string and specifically remove newline character after age. The could be done by iterating though the string (it is the same as list), returning index of char after "age" and replacing it to mere space. Or better, replace first \n with space!! (easier, phew ..) Commented Nov 24, 2019 at 12:00
  • 1
    I'll try as an exercise (I'm learning Python), thanks!
    – CarLaTeX
    Commented Nov 24, 2019 at 12:04
  • Maybe still, putting the output in table in latex would look better. You can achieve that by adding more replacements ;) Commented Nov 24, 2019 at 12:05
  • Oh, sorry, then I blew it... More fun is to put it inside a table! ;) Commented Nov 24, 2019 at 12:05
4

The print command will be printed and as always in LaTeX the underscore will give errors.

You can suppress the direct printing by setting the autoprint variable to false, and then use \stdoutpythontex or \printpythontex to print after the code block.

In a current latex the filecontents package is no longer needed.

\documentclass{article}
\begin{filecontents*}[overwrite]{provapd.txt}
user_id|age
1|20
2|25
3|30
4|35
\end{filecontents*}
\usepackage{pythontex}
\begin{document}
    prova pandas

\setpythontexautoprint{false}
\begin{pycode}
import pandas as pd
users = pd.read_table(r'provapd.txt',
                      sep='|', index_col='user_id')
print(users.age.mean())
print(users.head(3)) 
\end{pycode}

STDOUT
\stdoutpythontex[verbatim]

PRINT
\printpythontex[verbatim]

\end{document}

enter image description here

1
  • Thank you Ulrike, I prefer the other solution because I think it allows me to work on the output.
    – CarLaTeX
    Commented Nov 24, 2019 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .