0

So my friend is writing some python code for me to do some calculations for me. Is there a way that I can get the output of these calculations to be automatically translated into LaTeX formatting?

Nothing too complicated, the output is just a bunch of rational functions. I'd like to be able to get these put into LaTeX coding automatic. Is there an easy way to go about this?

  • 3
    python isn't really relevant, it is just a text file that you want to show, you could use verbatim for example (assuming the python output is just plain text) – David Carlisle Sep 12 '20 at 18:36
  • 3
    If you mean typesetting the code try using the listings package. If you are talking about typesetting the results of the calculation from python look into the python TeX package. – Herb Schulz Sep 12 '20 at 18:40
  • 6
    Can you give an example of the output of the calculations that you want to typeset? A bunch of rational functions can mean that you want to depict some plots, or some mathematical expressions, or maybe just rational numbers (which wouldn't really be functions). – gernot Sep 12 '20 at 18:40
  • the output will be a linear combination of functions of the form $\frac{p(x,y)}{q(x,y}}$ with coefficients $\mathbb{Z}$ and the exponents of $x,y$ that show up can be any rational number. – Ferris Boyler Sep 12 '20 at 18:58
  • 6
    what is the output form, or do you mean the python is already writing out latex syntax? Your question is very unclear – David Carlisle Sep 12 '20 at 19:24
  • 1
    What Herb said. Here is the link to pythontex: https://ctan.org/pkg/pythontex – DG' Sep 12 '20 at 19:29
  • Nah python just outputting normal text in command prompt, I want it to become LaTeX formatted – Ferris Boyler Sep 12 '20 at 21:18
  • 2
    why are you making it impossible for anyone to help? If you want latex to typeset $\frac{p(x,y)}{q(x,y}}$ from something produced by your python you need to show the text that is being produced. Currently your question just says "can latex typeset some expressions, give an unspecified input?" there is no answer that can be given. – David Carlisle Sep 12 '20 at 21:22
  • The progrma hasn't been written yet. I am confused why you need more information than the fact that the output is a rational function in two variables, and the output is all I want to get put into LaTeX format – Ferris Boyler Sep 13 '20 at 00:06
  • 1
    Why can't you just tell python to output $\frac{p(x,y)}{q(x,y)}$? Or do you have some class rationalfunction, in which case, why can't you just def totex(self)? – Teepeemm Sep 13 '20 at 01:14
  • I'm using someting called SymPi or something that allows python to do math calculations. The input is a partition of an integer n and then the program does a bunch of computations and spits out the answer. I'd like an easy way to turn that answer into LaTeX format, because it is going to be very long and awful to do by hand. – Ferris Boyler Sep 13 '20 at 01:15

1 Answers1

2

Example of a test.Rnw file compiled in Rstudio. The phyton code can be evaluated and instead show the result, or just show the code highlighted, or show the code and the result together. The file is internally exported to LaTeX (using R and knitr) to obtain the PDF.

\documentclass[twocolumn]{article}
\begin{document}
\section{Phyton code and result}
<<test0, engine='python'>>=
import random
print(random.randint(0,9))
@
\section{Code without result}
<<test2, echo=T, eval=F, comment="", engine='python'>>=
import calendar
yy = 2020  # year
mm = 9    # month
print(calendar.month(yy, mm))
@
\section{Result without code}
<<test3, echo=F, comment="", engine='python'>>=
import matplotlib.pyplot as plt
plt.plot([1,2,3,4,10])
@
\end{document}

mwe

Fran
  • 80,769
  • I must be doing something wrong in Rstudio, because your solution doesnt work for me. I am not skilled wtih that workspace however. Could you provide me some guidance? When I hit "Compile pdf" in Rstudio, I get only section heading in pdf (no code output). Same goes for the ganerated TeX file - it does not contain any code chunks or theyre output. What am I doing wrong? I am on Win10, python3 (python.exe in PATH), knitr installed. – Tomáš Kruliš Oct 06 '20 at 06:58
  • @TomášKruliš First, you have used the right extension ? ( it should be .Rmd, not .tex !! ). Second, can you compile \documentclass{article} \begin{document} ¬ <<>>= ¬ 2+3 ¬ @ ¬ \end{document} (where the ¬ are the carriage returns that are deleted is this comment) ? (again, remember, this is not yet a latex document, use the right extension). – Fran Oct 06 '20 at 07:20
  • Thank you for your reply. I have used .Rnw extension. With .Rmd extension, I am getting errors about non-existent sweave.sty (I am on texlive 2020, but never used Sweave before); also generated .tex from pandoc has many issues - \documentclass declared two times, two times defined \begin{document}. I must be doing something basic terribly wrong ... :D I have no issues using pandoc; I have sometimes even run knitr, but from TeXworks. Should I somehow install sweave.sty or anything? – Tomáš Kruliš Oct 06 '20 at 07:32
  • I think .Rmd expects different code-chunk definition (```{.r}), could that be the case? – Tomáš Kruliš Oct 06 '20 at 07:33
  • And, after removing \documentclass and \begin{document} and such, I got error about "texlive.infra" not found. – Tomáš Kruliš Oct 06 '20 at 07:42
  • (1) ```{.r} is (nearly, but wrong) a Rmardown syntax, for a .Rnw file the equivalent is <<>>= (2) seems that you have corrupt texlive instalation. – Fran Oct 06 '20 at 08:31
  • Yes, I am in the process of TeXlive reinstallation. Strange as it seems, until now everything worked for me perfectly :D ... So, afterwards, I should use .Rmd or .Rmw syntax to get your answer running? – Tomáš Kruliš Oct 06 '20 at 08:50
  • After fresh TeXlive installation I am still unable to run Sweave. – Tomáš Kruliš Oct 06 '20 at 13:26
  • @TomášKruliš My code is for the Sweave syntax amd .Rnw files, as stated in the answer. Only now I noticed that I said ."Rmd" instead of .Rnw" in the second comment. Sorry for the mistake !! To clarify: .Rnw for LaTeX+R and .Rmd for markdowm+R. In both cases, the start/end of the R chunk have a different syntax but in both cases can call the python engine and produce a pure LaTeX file and therefore a PDF, but obviously the text syntax and the R chunk syntax must be in agreement with the file extension. – Fran Oct 06 '20 at 17:15