1

intro.tex (Can be skipped...)

  • I recently switched from Windows to Linux Mint and just began learning Python. I wanted to make notes/reports using $\LaTeX$ and Python. Having used RSweave for a while now, I wanted something similar to that.

  • On looking up, I found Pweave. I wanted to use noweb code chunk format as it has been adopted from Sweave and I'm already accustomed to that style. I couldn't really comprehend the stuff they have written in the Pweave website.

  • On looking up again, I found this webpage which contains details about installing the Pweave noweb syntax that too in TeXstudio, which I have been using from a quite long time, so I was intrigued.

question.tex

  • For installing Pweave in my Linux Mint, I have done what all mentioned in the webpage till conda install -n pweave numpy. Now, stuck at Setting up a build command for editor, mainly at populating with actual command. I shall elaborate the issue.
  • For windows, there is a Scripts folder in anaconda3 folder which contains the activate.bat file, but, in Linux Mint, there is no such Scripts folder in anaconda3 folder. On searching for the file, I found a scripts folder in /home/trivikram/anaconda3/pkgs/python-3.6.13-h12debd9_1/lib/python3.6/venv and it contained two sub folders one named common and other named posix, the former contained text file named activate and the later contained activate.csh and activate.fish files.
  • I've tried all the above mentioned three files for the input field in Options > Configure TeXstudio > Build > Use Commands, as mentioned in the webpage, neither of them worked. Here, I attach the screenshot of the error that shows up on running a dummy check.Pnw file. So, which other file I should use for the activation of Pweave?

So, if someone is using Pweave in TexStudio of Linux or someone who knows the corresponding things that should be done to activate such files in Linux Mint, if they can spend some time and answer, I shall be thankful.

Anyways, thanks for reading. Could not start Default Compiler:PdfLaTeX:

  • Not idea of Pweave or python, but RSweave documents actually are not only for R code using knitr (and not the old Sweave), you can also execute python code just adding the option engine=python to the <<>>= line. – Fran Jun 14 '22 at 10:08
  • That works..!! Thank you very much :D. Actually, I was under assumption that RStudio doesn't support such feature of LaTeX + Python.. So, was searching for TeXstudio. I didn't know about the engine=python .. Thanks again :D – Venkat Trivikram Jun 14 '22 at 11:53

2 Answers2

1

Knitr can also support chunks of python code with the option engine="python".

Test.Rnw:

\documentclass[twocolumn]{article}
\begin{document}
<<engine="python", echo=FALSE, fig.cap="Python plot.">>=
import matplotlib.pyplot as plt
X = range(10)
plt.plot(X, [x*x for x in X])
plt.show()
@
\end{document}

mwe

Fran
  • 80,769
0

Some time ago I wrote a tutorial here about Pweave configuration under Windows. In the tutorial you can find a batch file which does all the jobs to generate a LaTeX document and final PDF file. Here, for convenience, I am going to reproduce it and update it in some places. My initial Anaconda based installation was under Windows.

If we consider that Anaconda software distribution is already installed, the first step is to create a virtual environment (called as pweave in this example):

conda create --name pweave
conda install -n pweave python=3.6 
conda install -n pweave -c conda-forge pweave
conda install -n pweave pygments
conda install -n pweave numpy

The batch file for Windows OS is (build-win.bat):

@echo off

echo "Compile script for pweave noweb documents (.pnw) under Windows OS" echo "Requires a LaTeX installation" echo "Requires a Python installation in form of anaconda/miniconda distribution in user directory" echo "Requires a creation of Python virtual environment with name 'pweave'" echo "Requires conda package installation with name 'pweave' (within the environment)" echo "Usage example, e.g.: build-win.bat test.pnw"

set filename=%1 set filenamenoext=%~n1 REM It is recommended that you install anaconda in user directory REM To adjust to your system setup, in the path string below, replace 'yourusername' with your own REM and make a correction to anaconda path set pathtoanaconda="C:\Users\yourusername\anaconda3"

echo "Compiling ...%filename%"

echo "activativating conda pweave environment and compiling by pweave ..."

CALL "%pathtoanaconda:"=%\Scripts\activate.bat" pweave

"%pathtoanaconda:"=%\envs\pweave\Scripts\pweave.exe" -f tex %filename%

echo "compiling %filenamenoext%.tex by pdflatex ... into %filenamenoext%.pdf"

REM In this script, a pdflatex is called, change it according to your TeX engine pdflatex -synctex=1 -interaction=nonstopmode %filenamenoext%.tex pdflatex -synctex=1 -interaction=nonstopmode %filenamenoext%.tex

CALL "%pathtoanaconda:"=%\condabin\conda.bat" deactivate

echo "Complete"

To build a document we place the batch file in an appropriate location and now we can call it as

build.bat test.pnw

from OS command line or user command in your editor.

In TeXstudio we can create an user command. To setup the user command, go to Options > Configure TeXstudio ... then select Build tab on the left. On the right pane you will see User Commands group which consists of two input fields where the leftmost is for the name of command.

Later I created a similar configuration for Linux Ubuntu and I decided to change the virtual environment to regular Python 3.9 venv (called as upve39-pweave in this example):

sudo apt install python3.9 python3.9-dev python3.9-venv

python3.9 -m venv upve39-pweave

source ./upve39-pweave/bin/activate

pip install pweave pip install pygments pip install numpy

deactivate

For Linux, I created a bash script (build-ubu.sh) to do the same things like in that batch file:

#!/bin/sh

echo "Compile script for pweave noweb documents (.pnw) under Linux OS" echo "Requires a LaTeX installation" echo "Requires a Python installation in form of anaconda/miniconda distribution in user directory" echo "Requires a creation of Python virtual environment with name 'pweave'" echo "Requires conda package installation with name 'pweave' (within the environment)" echo "Usage example, e.g.: build-ubu.sh test.pnw"

fileext=${1##.} # only extension without dot filenamenoext=${1%.} # name without extension

To adjust to your system setup, in the path string below, replace 'yourusername' with your own

pathtopython="/home/yourusername"

echo "Compiling ...$filenamenoext.$fileext"

echo "activating pweave environment and compiling by pweave ..."

$pathtopython/upve39-pweave/bin/deactivate source $pathtopython/upve39-pweave/bin/activate

$pathtopython/upve39-pweave/bin/pweave -f tex $filenamenoext.$fileext

echo "compiling $filenamenoext.tex by *latex ... into $filenamenoext.pdf"

lualatex -synctex=1 -interaction=nonstopmode $filenamenoext.tex lualatex -synctex=1 -interaction=nonstopmode $filenamenoext.tex

$pathtopython/upve39-pweave/bin/deactivate

echo "Complete"

So now you can call it as

build.sh test.pnw

from OS command line or user command in your editor.

Example of Pweave file opening and compiling in the TeXstudio is presented below:

Using Pweave file in TeXstudio