gpoore / minted

minted is a LaTeX package that provides syntax highlighting using the Pygments library. Highlighted source code can be customized using fancyvrb.
1.75k stars 127 forks source link

TEXINPUTS are not followed #25

Closed akimd closed 9 years ago

akimd commented 10 years ago

Hi,

I'm sorry, this bug report is not against the current git version of the package, but against the one I have via TeXlive: 2011/09/17. However, according to what I saw, there is no difference.

I'm using various tools such as texi2pdf to compile my latex files. Such tools, to avoid cluttering the working directory, compile elsewhere. To make sure that \input and companions work properly, the TEXINPUTS envvar is used. It appears that listings handles this properly, but minted does not.

It would be really nice if it did.

Thanks!

gpoore commented 10 years ago

listings can handle environment variables correctly and work with "clean build" tools, because it is completely TeX-based.

Packages like minted have a more difficult time, because they call external tools that aren't necessarily aware of the environment variables or of the build tools. Also, minted and similar packages must write to temporary files, and temporary files can only be created in the document root directory or a subdirectory. This is the default behavior for security reasons, though it is possible to override.

The current development version of minted has an outputdir option that is intended to allow minted to work with LaTeX's -output-directory option. But in this case, the output directory must be specified both in the command used to run LaTeX (-output-directory) and in the document source (minted option outputdir), since the command-line option can't be accessed through macros (at least as far as I know).

It's possible that the outputdir option could work for you. It would be worth trying. If not, there might be a way to do something similar. But In that case I would need detailed information about exactly how texi2pdf compiles in other locations.

akimd commented 10 years ago

Le 26 mars 2014 à 18:11, Geoffrey Poore notifications@github.com a écrit :

listings can handle environment variables correctly and work with "clean build" tools, because it is completely TeX-based.

Yes, I understand that.

It's possible that the outputdir option could work for you.

I don't think so, the problem is the input directory.

It would be worth trying. If not, there might be a way to do something similar. But In that case I would need detailed information about exactly how texi2pdf compiles in other locations.

texi2dvi, when called with --tidy, will create a directory and compile everything in it. To this end, it copies the input file in the directory, and updates TEXINPUTS so that the different \input etc. can find their files. And it chdir into it.

In the case of minted, since texi2dvi changed the current directory, the path it sees are now wrong.

To make sure that TEXINPUTS is accurate (and in order to implement -I, similar to the corresponding compiler flag), texi2dvi ensures that every single directory in there is absolute. And of course it prepends the directory of the original file.

So, I would say that what would be needed, would be to have minted support TEXINPUTS by splitting it, and looking for the files in it.

Alternatively, it might be simpler to use tools such as kpsewhich. It seems to me that this is the most TeX-way to do that (including support for trailing //).

$ ls questions/prog1.cc questions/prog1.cc $ kpsewhich prog1.cc $ TEXINPUTS=.:questions kpsewhich prog1.cc questions/prog1.cc $ TEXINPUTS=.// kpsewhich prog1.cc ./questions/prog1.cc

Thanks for your quick answer!

gpoore commented 10 years ago

LaTeX's -output-directory affects both input and output, and the minted option I've created to work with it does the same.

akimd commented 10 years ago

Le 26 mars 2014 à 18:32, Geoffrey Poore notifications@github.com a écrit :

LaTeX's -output-directory affects both input and output, and the minted option I've created to work with it does the same.

Hi Geoffrey,

Yes, I do understand that it affects the input too, given that LaTeX generates tons of tmp files that it reads afterwards.

However here it is not only a question of directory, but also of path, in the sense of a list of directories.

Changing texi2dvi to use -output-directory, given that not all versions of LaTeX support it, will probably not be accepted by the maintainers, given that texi2dvi must remain as portable as possible. And releases of Texinfo are infrequent and take time to spread.

I would like to emphasize that here the main issue is that minted does not "comply" with the usual TeX interface, and by this I mean TEXINPUTS. As a LaTeX user, you most probably have specific .sty files, and .bib, and figures, that you store in specific directories. I do the same for code snippets when used with listings. These variables (TEXINPUTS and the like) are part of the interface that make this work.

minted is an amazing improvement over listings, but the fact that it does follow the TeX search paths is unique.

I guess that minted fires some shell code to call pygmentize behind the scenes, so, unless I missed something obvious, it seems that it would suffice to replace

pygmentize SOURCE-FILE

by

pygmentize $(kpsewhich SOURCE-FILE 2>/dev/null || echo SOURCE-FILE)

to have it work. Or maybe like this, to be safer on special characters:

s=$(kpsewhich 'SOURCE-FILE' 2>/dev/null) : ${s='SOURCE-FILE'} pygmentize "$s"

Cheers,

Akim
akimd commented 10 years ago

Le 27 mars 2014 à 10:33, Akim Demaille akim.demaille@gmail.com a écrit :

minted is an amazing improvement over listings, but the fact that it does follow the TeX search paths is unique.

I met another instance of that problem right now: I'm write an assignment for my students, and I'm using auto-multiple-choice with LaTeX input (http://home.gna.org/auto-qcm/index.en).

The master file uses a store a existing questions, some of them do use code snippets that I typeset with listings. Again, minted cannot be used as a drop in replacement because it can't find these files.

Akim
akimd commented 9 years ago

Hi,

I have again met that issue, which is really making the use of minted troublesome. Do you have plans to address this? Don't you think the kpsewhich approach would solve cleanly your problem?

Thanks in advance!

gpoore commented 9 years ago

I am working to address this, but I can't give it a high priority relative to other issues, since I need to learn the basics of another build tool and attempt to find a solution that will function under all operating systems with both MikTeX and TeX Live. At this point, I expect to have a solution (assuming one exists) by January.

In the meantime, feel free to experiment with the style file. The command that is used to call Pygments is on lines 324, 339, and 668 of minted.sty. You could modify that to see if the kpsewhich approach will work consistently on your OS.

akimd commented 9 years ago

Thanks for the feedback!

I will try to toy with the style file, indeed. However, when I have given it a shot (because I'm really eager to use \mintinline), a couple of hours ago, I could not get any color in the output. Does that ring a bell? I have not tried to explore any further. (I had just downloaded the .sty file and left it next to my LaTeX file.)

gpoore commented 9 years ago

I'm not sure what would cause a lack of color. Putting the style file in the same directory as the TeX source should work. I've used it that way under Windows and Linux. You might try downloading the file again, using the GitHub raw view, to make sure it came through OK.

akimd commented 9 years ago

Well, I tried harder, and found: passing [draft] to the document class removes the style. This is a difference with the minted that comes with my TeXLive (1.7 I think). How can I avoid that without removing the [draft] from {article}?

gpoore commented 9 years ago

\usepackage[draft=false]{minted}

I wasn't thinking about the new draft mode. It turns off highlighting, which means there are absolutely no slowdowns due to highlighting code (not even the slight overhead involved with caching).

akimd commented 9 years ago

Ah, thanks for the draft=false :) I had tried passing final. Well, FWIW, "draft" is not in the doc (I had checked :)

gpoore commented 9 years ago

I'm just getting back to this. I can't get texi2pdf to produce any errors with minted 1.7 or with the latest development version. I'm using commands similar to

texi2pdf --build=tidy --shell-escape --batch test.tex

with texi2pdf (GNU Texinfo 5.2) 5381. Can you give me some additional information about the commands you are using, the version of texi2pdf, and any custom TEXINPUTS settings or other custom settings that might be relevant?

Also, draft is now in the docs, and I will be adding a final option.

akimd commented 9 years ago

Le 21 janv. 2015 à 18:45, Geoffrey Poore notifications@github.com a écrit :

hi Geoffrey,

I'm just getting back to this. I can't get texi2pdf to produce any errors with minted 1.7 or with the latest development version. I'm using commands similar to

texi2pdf --build=tidy --shell-escape --batch test.tex

with texi2pdf (GNU Texinfo 5.2) 5381. Can you give me some additional information about the commands you are using, the version of texi2pdf, and any custom TEXINPUTS settings or other custom settings that might be relevant?

I did as follows. It fails with texi2pdf, works with pdflatex.

akim@padam /tmp $ cat main.tex \documentclass{article}

\usepackage{minted}

\begin{document}

\inputminted{c}{foo.c}

\end{document} akim@padam /tmp $ cat foo.c int main(){} akim@padam /tmp $ pdflatex -shell-escape main.tex This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014/MacPorts 2014_4) (preloaded format=pdflatex) \write18 enabled. entering extended mode (./main.tex LaTeX2e <2014/05/01> Babel <3.9k> and hyphenation patterns for 4 languages loaded. (/opt/local/share/texmf-texlive/tex/latex/base/article.cls Document Class: article 2007/10/19 v1.4h Standard LaTeX document class (/opt/local/share/texmf-texlive/tex/latex/base/size10.clo)) (/opt/local/share/texmf-texlive/tex/latex/minted/minted.sty (/opt/local/share/texmf-texlive/tex/latex/graphics/keyval.sty) (/opt/local/share/texmf-texlive/tex/latex/fancyvrb/fancyvrb.sty Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix <2008/02/07> (tvz)) (/opt/local/share/texmf-texlive/tex/latex/xcolor/xcolor.sty (/opt/local/share/texmf-texlive/tex/latex/latexconfig/color.cfg) (/opt/local/share/texmf-texlive/tex/latex/pdftex-def/pdftex.def (/opt/local/share/texmf-texlive/tex/generic/oberdiek/infwarerr.sty) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ltxcmds.sty))) (/opt/local/share/texmf-texlive/tex/latex/float/float.sty) (/opt/local/share/texmf-texlive/tex/latex/base/ifthen.sty) (/opt/local/share/texmf-texlive/tex/latex/tools/calc.sty) (/opt/local/share/texmf-texlive/tex/latex/ifplatform/ifplatform.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/pdftexcmds.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ifluatex.sty) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ifpdf.sty)) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/catchfile.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/etexcmds.sty)) (./main.w18 )))/Users/akim/usr/bin/pygmentize (./main.aux) (/opt/local/share/texmf-texlive/tex/context/base/supp-pdf.mkii

[Loading MPS to PDF converter (version 2006.09.02).] ) (./main.pyg) (./main.out.pyg) 1{/opt/local/var/db/texmf/fonts/map/pdftex/upd map/pdftex.map} )</opt/local/share/texmf-texlive/fonts/type1/publ ic/amsfonts/cm/cmr10.pfb></opt/local/share/texmf-texlive/fonts/type1/public/ams fonts/cm/cmtt10.pfb> Output written on main.pdf (1 page, 18201 bytes). Transcript written on main.log.

akim@padam /tmp $ texi2pdf --clean --shell-escape --batch main.tex This is pdfTeX, Version 3.14159265-2.6-1.40.15 (TeX Live 2014/MacPorts 2014_4) (preloaded format=pdflatex) \write18 enabled. entering extended mode LaTeX2e <2014/05/01> Babel <3.9k> and hyphenation patterns for 4 languages loaded.

(/tmp/./main.tex (/opt/local/share/texmf-texlive/tex/latex/base/article.cls Document Class: article 2007/10/19 v1.4h Standard LaTeX document class (/opt/local/share/texmf-texlive/tex/latex/base/size10.clo)) (/opt/local/share/texmf-texlive/tex/latex/minted/minted.sty (/opt/local/share/texmf-texlive/tex/latex/graphics/keyval.sty) (/opt/local/share/texmf-texlive/tex/latex/fancyvrb/fancyvrb.sty Style option: `fancyvrb' v2.7a, with DG/SPQR fixes, and firstline=lastline fix <2008/02/07> (tvz)) (/opt/local/share/texmf-texlive/tex/latex/xcolor/xcolor.sty (/opt/local/share/texmf-texlive/tex/latex/latexconfig/color.cfg) (/opt/local/share/texmf-texlive/tex/latex/pdftex-def/pdftex.def (/opt/local/share/texmf-texlive/tex/generic/oberdiek/infwarerr.sty) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ltxcmds.sty))) (/opt/local/share/texmf-texlive/tex/latex/float/float.sty) (/opt/local/share/texmf-texlive/tex/latex/base/ifthen.sty) (/opt/local/share/texmf-texlive/tex/latex/tools/calc.sty) (/opt/local/share/texmf-texlive/tex/latex/ifplatform/ifplatform.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/pdftexcmds.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ifluatex.sty) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/ifpdf.sty)) (/opt/local/share/texmf-texlive/tex/generic/oberdiek/catchfile.sty (/opt/local/share/texmf-texlive/tex/generic/oberdiek/etexcmds.sty)) (./main.w18 )))/Users/akim/usr/bin/pygmentize (/tmp/main.aux) (/opt/local/share/texmf-texlive/tex/context/base/supp-pdf.mkii [Loading MPS to PDF converter (version 2006.09.02).] ) (./main.pyg)Error: cannot read infile: [Errno 2] No such file or directory: 'foo.c'

! LaTeX Error: File `main.out.pyg' not found.

Type X to quit or to proceed, or enter new name. (Default extension: out.pyg)

Enter file name: /tmp/./main.tex:7: Emergency stop. <read *>

l.7 \inputminted{c}{foo.c} ^^M /tmp/./main.tex:7: ==> Fatal error occurred, no output PDF file produced! Transcript written on main.log. /opt/local/bin/texi2dvi: pdflatex exited with bad status, quitting.

This is:

\ProvidesPackage{minted}[2011/09/17 v1.7 Yet another Pygments shim for LaTeX]

Using --debug and --verbose will help see what texi2dvi does.

gpoore commented 9 years ago

texi2pdf apparently only has trouble with \inputpygments, which comes back to the path issues. Here's a patch for minted 1.7; you can add this to the preamble, right after loading minted. The relevant change was #1 -> \detokenize{$}(kpsewhich "#1" || "#1"). I still need to figure out the best way to deal with this for minted 2.0. This solution only works for texi2pdf and other build tools that rely on TEXINPUTS. This won't help the many build tools that use -output-directory, since there is no documented way to access the changes that that option makes to paths. In particular, it doesn't affect TEXINPUTS.

\makeatletter
\renewcommand\minted@pygmentize[2][\jobname.pyg]{
  \def\minted@cmd{pygmentize -l #2 -f latex -F tokenmerge
    \minted@opt{gobble} \minted@opt{texcl} \minted@opt{mathescape}
    \minted@opt{startinline} \minted@opt{funcnamehighlighting}
    \minted@opt{linenos} -P "verboptions=\minted@opt{extra}"
    -o \jobname.out.pyg \detokenize{$}(kpsewhich "#1" || "#1")}
  \immediate\write18{\minted@cmd}
  % For debugging, uncomment:
  %\immediate\typeout{\minted@cmd}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\begin{minted@colorbg}{\minted@opt@bgcolor}}
  \input{\jobname.out.pyg}
  \ifthenelse{\equal{\minted@opt@bgcolor}{}}
   {}
   {\end{minted@colorbg}}
  \DeleteFile{\jobname.out.pyg}}
\makeatother
gpoore commented 9 years ago

For future reference on this and any related issues in the future, the $(...) substitution syntax is not compatible with tcsh, so it can't be used as a default for all non-Windows systems. The ... syntax seems to have essentially universal support.

akimd commented 9 years ago

Le 23 janv. 2015 à 02:49, Geoffrey Poore notifications@github.com a écrit :

For future reference on this and any related issues in the future, the $(...) substitution syntax is not compatible with tcsh, so it can't be used as a default for all non-Windows systems. The ... syntax seems to have essentially universal support.

That's right. But seriously, shell scripts should never be run by tcsh. It would be extremely bizarre that LaTeX fires tcsh instead of /bin/sh. tcsh is really an (obsolete imho) interactive shell.=

gpoore commented 9 years ago

The latest commit adds a kpsewhich package option. This adds kpsewhich into the pygmentize commands under all operating systems. That should give you everything you need.