pythontex icon indicating copy to clipboard operation
pythontex copied to clipboard

Request: ability to read .pytxcode file from subdirectory

Open cweedall opened this issue 7 years ago • 7 comments

Currently, there appears to be a disconnect between using \setpythontexworkingdir{<workingdir>} and calling pythontex.exe. Using the TeX command outputs the .pytxcode file into the working directory <workingdir>. This part functions as expected. However, the .pytxcode file cannot be read by pythontex.exe. This requires .pytxcode to be manually copied (or via batch) from <workingdir> to the directory where the .tex file is located.

Looking through the documentation and code, it seems to me there is no way to avoid this manual intervention - creating two copies or moving the .pytxcode file. Therefore, I am requesting either an update to the package, to the pythontex.exe file, or both.

For pythontex.exe, allow a new working directory option - (a couple ideas): -aux-directory <workingdir> (similar to MiKTeX) -working-directory <workingdir>

For python code in pythontex2.py and pythontex3.py, (where appropriate)several updates and options: data['jobname_path'] = raw_jobname[0:raw_jobname.rfind('/')] jobname_path = data['jobname_path'] jobname_path + raw_jobname (when needing to reference the file, rather than just the job name)

For pythontex.sty, one new update, (optional - convenience) two new package options: (update) change \immediate\openout\pytx@codefile=\jobname.pytxcode to \immediate\openout\pytx@codefile=\pytx@workingdir/\jobname.pytxcode (optional) Create package options - \usepackage[workingdir=<workingdir>, outputdir=<outputdir>]{pythontex} (same as \setpythontexworkingdir{<workingdir>} and \setpythontexoutputdir{<outputdir>})

I think I covered most of the things necessary to make this change. Let me know if I can provide more details, clarification, or help in any way!

cweedall avatar Jan 10 '17 02:01 cweedall

After thinking this over...I think a working directory may be the wrong way to go. For example, if your .tex file is in C:\pythontexTest, and you set the working directory to C:\pythontexText\build_files, then trying to reference files (e.g. images) will require you to move down one directory in the python code. For an end-user, this will be counter-intuitive or unexpected. You would be writing TeX and python thinking the files are relative to the .tex file.

Rather than keeping the .pytxcode in the working directory, why not just move it to the output directory while maintaining the working directory as the .tex file directory?

Examples: C:\pythontexTest\ (working directory - no aux/build directory) ---+ JOBNAME.tex ---+ /pythontex-files-JOBNAME (output directory) ------+ JOBNAME.pytxcode ------+ (other related build files)

C:\pythontexTest\ (working directory) ---+ JOBNAME.tex ---+ /build_files (aux/build directory) ------+ /pythontex-files-JOBNAME (output directory) ---------+ JOBNAME.pytxcode ---------+ (other related build files)

As long as running pythontex.exe on the .tex file from the working directory (in both cases), I assume this shouldn't cause a problem, correct?

So, rather than modifying things with the working directory, better to have an -aux-directory or -build-directory OR to just simply allow the .pytxcode be included in the output directory. I'm not completely certain if there are pro's or con's for either and would be happy to discuss further.

cweedall avatar Jan 10 '17 02:01 cweedall

Currently, there is no support for MiKTeX's -aux-directory (similar to TeX Live's -output-directory, except that that also relocates the PDF, not just auxiliary files).

I think the best approach would be to add -aux-directory and -output-directory command-line options to pythontex.py/pythontex.exe. That way, all executables (LaTeX and PythonTeX) could be configured in the same way, and would work as expected. This would involve changing the Python code that reads the .pytxcode, to use the -aux-directory if one is given. It might also involve putting the pythontex-files-* directory in the -aux-directory.

The PythonTeX package currently has an outputdir option to customize the pythontex-files-* name/location. The naming of this goes back to before I was familiar with the -aux-directory/-output-directory options. As part of this process, the package outputdir option should be deprecated, and renamed to cachedir or something similar that is more descriptive and less likely to cause confusion.

The location of the .pytxcode doesn't need to be controlled. If there is no -aux-directory, it will be in the directory with the .tex file. If there is an -aux-directory, the .pytxcode will be there. The .pytxcode can't be put in PythonTeX's pythontex-files-* directory, because that directory is created by running pythontex.exe, so it won't exist the first time that LaTeX runs.

I think it makes the most sense to leave the default working directory as the location of the .tex file. That's what most people seem to expect. Ideally, the -aux-directory should only relate to temporary or cache files.


Summary: It should be possible to get basic functionality just by adding command-line options -aux-directory and -output-directory, and having the Python code read the .pytxcode from one of those locations if it is given. I'm currently in the midst of creating low-level libraries for the next version of PythonTeX, but I can probably look into getting the basics working in the next few days.

gpoore avatar Jan 19 '17 15:01 gpoore

Just getting back from vacation, so apologies for the delay.

Yes, including command line options (a la MiKTeX or TeXLive) would be suitable. My discussion on code changes was somewhat dependent upon whether it was preferable to have command line options or to incorporate it into the package itself. I didn't notice any of the .exe code here on github (perhaps I just missed it?), but I'm hoping those low-level changes are not too drastic.

I understand your points about where to include the pythontex-files-* folder and .pytxcode. Having the flexibility to keep these in a subfolder for users definitely will help some people. Thank you for considering and looking into implementing this!

cweedall avatar Feb 15 '17 02:02 cweedall

I just wanted to say that I was met with the same problem.

I tried to pick up PythonTeX and struggled for a couple of hours, because by default, I use a separate build subdirectory for all my output and temporary files.

I'll have to break the habit when working with PythonTeX.

Georacer avatar Mar 09 '18 22:03 Georacer

For anyone interested, you need not stop using a subdirectory to use PythonTeX. If you use a batch file (located in MiKTeX's bin folder), you can do the following:

@copy /Y "PATH_TO_BUILD_FILES\%1.pytxcode" "%1.pytxcode" @powershell -command "pythontex.exe '%1.tex' | tee 'PATH_TO_BUILD_FILES\%1-pythontex.log'"

You will note that I wrap the pythontex command in Power Shell. I do this to make sure I see the errors in a file - they get drowned out in the console output for long TeX documents. You can also write:

@pythontex.exe '%1.tex'

if you have no need for that log file.

cweedall avatar Jul 20 '18 19:07 cweedall

I would like to have the -output-directory option as well. I was in the process of making the build cleaner by adding that argument to pdflatex only to find out that pythontex can't do it at all. A workaround was to symlink the .pytxcode from the root directory, but that's rather hackish.

There exists \setpythontexworkingdir and \setpythontexoutputdir but they don't seem to be meant to solve this issue.

bleetsheep avatar Jun 02 '20 08:06 bleetsheep

I created a bit of a hacky implementation to solve this here. All it does is copy the input tex file to the give --output-directory and change the TEXNAME to point to it. It solved my problem, so that's why I'm sharing. I don't think it works without the file ending (yet).

davystrong avatar Jan 23 '21 17:01 davystrong