cduck / latextools

A collection of tools for building, rendering, and converting Latex documents
MIT License
22 stars 2 forks source link

"! I can't find file" at latex compilation on Windows #4

Open arthur-lebee opened 2 years ago

arthur-lebee commented 2 years ago

Dear Casey,

Thank you for providing latextools and drawSvg.

I have a functionnal install following the readme instructions:

Neverthless, when running the examples, latex compilation fails on my machine. Here is the result with the first example on the readme page:

Traceback (most recent call last):
  File "C:\Users\arthur.lebee\ownCloud\Enseignement\MECST\MECST_Cours\Polycopie\Animations\EssaiLtools1.py", line 24, in <module>
    pad=1,
  File "C:\Users\arthur.lebee\AppData\Local\Continuum\anaconda3\lib\site-packages\latextools\shortcuts.py", line 42, in render_snippet
    '-shell-escape'])
  File "C:\Users\arthur.lebee\AppData\Local\Continuum\anaconda3\lib\site-packages\latextools\project.py", line 97, in compile_pdf
    **pdf_args)[0]
  File "C:\Users\arthur.lebee\AppData\Local\Continuum\anaconda3\lib\site-packages\latextools\project.py", line 108, in compile_pdf_batch
    **pdf_args)
  File "C:\Users\arthur.lebee\AppData\Local\Continuum\anaconda3\lib\site-packages\latextools\project.py", line 117, in compile_pdf_batch
    self.run_pdflatex(fpath, cwd=tmp_dir, options=options)
  File "C:\Users\arthur.lebee\AppData\Local\Continuum\anaconda3\lib\site-packages\latextools\project.py", line 185, in run_pdflatex
    raise LatexError(msg)
latextools.project.LatexError: This is pdfTeX, Version 3.141592653-2.6-1.40.24 (MiKTeX 22.3) (preloaded format=pdflatex.fmt)
 \write18 enabled.
entering extended mode
! I can't find file `C:/Users/ARTHUR'.
<to be read again>
                   \protect
<*> C:/Users/ARTHUR~
                    1.LEB/AppData/Local/Temp/tmpkfvlisip/main.tex
(Press Enter to retry, or Control-C to exit)
Please type another input file name
! Emergency stop.
<to be read again>
                   \protect
<*> C:/Users/ARTHUR~
                    1.LEB/AppData/Local/Temp/tmpkfvlisip/main.tex
!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on texput.log.

I tried my best to find out if the temp directory and its files to be compiled are actually created or not. As everything is wiped out after execution, I could not figure out.

Any suggestion?

Best Regards

Arthur

arthur-lebee commented 2 years ago

Dear Casey,

I dug further. The problems comes from the windows path short format (with ~): this is not supported when the pdflatex command is cast in LatexProject.run_pdflatex

(see also: https://stackoverflow.com/questions/11420689/how-to-get-long-file-system-path-from-python-on-windows )

As usernames are rather long, this bug may affect most of Windows users...

Best Arthur

cduck commented 2 years ago

Thanks for opening this issue. I don't have access to a Windows machine to test this but I can give some pointers. Please let me know how you end up resolving this.

Error messages starting with This is pdfTeX... are the log straight from pdflatex so locating pdflatex seems to be fine. The best way to debug is to use proj.write_src('src-dir') to output the source tex files, then you can read all the intermediate tex files and run pdflatex main.tex manually in src-dir.

This line from the error catches my eye: I can't find file 'C:/Users/ARTHUR'. One possibility is you or latextools needed to escape the . in the path when used in that tex file. That's where I would start investigating.

arthur-lebee commented 2 years ago

Dear Casey,

Thanks for your quick reply.

My quick and dirty fix is inserting at line 102 of project.py:

                from ctypes import create_unicode_buffer, windll
                buffer_size = 500
                buffer = create_unicode_buffer(buffer_size)
                get_long_path_name = windll.kernel32.GetLongPathNameW
                get_long_path_name(tmp_dir, buffer, buffer_size)
                tmp_dir = buffer.value

This forces the Windows path to be in the right format. I don't think my coding is plateform independent. If you find a way... Additionnally, I have not checked if a similar bug may occur elsewhere (I noticed there are several calls to tempfile of fs.tempfs )

Finally, as I am your Windows beta tester... I suggest using pdftocairo instead of pdf2svg which I have not found for Windows (I assume pdftocairo does exactly the same thing?).

This ends up by changing line 269 in convert to: args = ['pdftocairo','-svg', 'image.pdf', 'image.svg']

Hope this helps a bit!

cduck commented 2 years ago

Great. I think the best solution would be to write a helper function that checks the platform type (Windows vs. other) and does the correct conversion. Then it's easy to use for all calls of tempfile.

I believe someone else previously requested pdftocairo but I would need to confirm they have equivalent output (especially with respect to unit scaling) before switching or supporting both.